Back to blogging in 2020!

BASH (command line) for beginners

If you Google "beginner bash", you apparently get a bunch of long, scary, unstyled walls of text that don't look like they'd be that helpful to beginners.

Ugh, words. I thought we were learning about using the command line.
I use bash a lot (apparently I'm in the command line about 8 hours a week according to rescue time) so maybe I have some useful tips to provide. Try using each of the commands! Make dummy files and folders (see the 'touch' command... 'foo' and 'bar' are standard fake names, but I like cat-themed names) and move and copy and delete them!

What is "bash"?

I don't remember what it stands for. Bad-Ass Shell? Whoops, no, Bourne-again shell. Is Bourne a person? Apparently. A shell is just an interactive prompt, and before there was bash, there was just the regular "Borne shell" called sh. Bash is a unix thing, so it's used for the terminal in both OSX and Linux. On windows, you can get a unix emulator called Cygwin, but the normal windows command line is DOS. I met the guys who made Cygwin! One of them taught me to walk on stilts. Anyway.

If you've used the interactive mode of something like python, you know you can type one line or multi-line code and things run. Same exact thing in your terminal, but the language is bash! 

Make your terminal sexy/pretty/aesthetically pleasing AND readable!

Colors! I wanted to ramble about colors and just having fun customizing your terminal, but additionally, you should pay attention to the color of your cursor and highlight colors and make sure you can actually SEE your cursor all the time. Please don't handicap yourself by making your potentially most powerful tool hard to read. And make your scrollback hella big (in Preferences > Window on OSX).


Tab completion and a couple other shortcuts you should start using asap:

Start typing and the hit tab to complete a file name or path or even an executable name! It'll complete as much as it can and then show you hints. Saves you from typing it all out yourself and is faster and less error-prone.

command-a (to go to beginning of line)
command-e (to go to the end of a line)
command-u (to delete everything you've typed on that line and start over)
up arrow to redo commands you did recently

Tell your friends to do these things. Superpowers++

"Where am I and what am I doing?" commands

The command line can be disorienting. If I forget what I'm doing or need a twitchy thing to do while my brain processes something, I'll type ls and pwd

ls

List the stuff in this directory! I usually tell where I am by what is in a directory... and to remind myself what files I might be trying to edit.

Other variations:
ls -l (to show details)
ls -lh (to show details in Human-readable format)
ls -lht (to sort things by Time modified)
ls *.jpg (list only files of a certain type)
list prefix_* (list only files starting with a certain prefix)

I actually put 'a's in my ls commands, like ls -lah but I don't remember what the a does. OH! It shows hidden files (see very end).

pwd

Print working directory!



cd <some other directory path>

Change directory!
cd ../ to go up one level
cd ../../ to go up two levels
cd / to go to the root of your entire computer's directory structure
cd (with no argument) to go to your default home directory
cd ~ to also go to your home directory
cd ~/some_folder (to go from anywhere to a folder in your home directory)

mkdir <new_directory_name>

Make a directory!

cp <file1> <file2>

Copy a file! 

mv <file1> <file2>

Move (or rename) a file, without leaving a copy behind

rm <file>

Remove/delete a file... and it's gone forever!
rm -r <directory_name> (to Recursively delete a directory and everything in it. BE CAREFUL WITH THIS ONE...)

touch <new_filename>

This just makes a new file... or updates an existing file's last-modified time. Okay, touch is only kinda useful, but you can use it to make empty files to practice copying and moving around.

echo "some strings"

Print out some strings or variables! Variables, mentioned below, have $ dollar signs in front of them.
echo $cat_var

cat <filename>

Meow! The name comes from "concatenate" in that you can use it to print out multiple files at one in one long concatenated string, but you can also use it to print just one file. By "print" I mean show the contents of that file in the command line.

writing things to a file with > or append to end of file with >>

For example...
echo "my cat is named MPEG" > catnames.txt
echo "my other cat is named JPEG" >> catnames.txt

EXAMPLE!!!


I used a bunch of these commands to make a dummy file called cats so I could write "touch cats", and then I used echo to add text to another file and cat to show the contents of that other file. What commands did I actually type? Lets look at my history below!

history

Seeing what you've been up to is kind of handy, but being able to re-run hairy commands you previously wrote is way better. Those #s are ways for you to return certain commands.
!515 will rerun the 'cat catnames.txt' command.
!! will rerun the last command
!<letter> will rerun the last command that started with that letter

Also, the ! is called "bang"! Bang-five-one-five, Bang-bang!

grep

history | grep <part of command I ran recently but forgot>
cat <file> | grep <thing you're looking for on some line of a file>

grep helps you search through text file and history for lines containing certain words. Honest-to-god example of how I use grep (and history) every single day because I'm too lazy to remember how to get to my MySQL database server. I also use it to figure out which file contains some particular method that I'm in search of... And other things, but I will have to thing longer/harder about those other things.
How I log into my database... I'm sure there's a more efficient way.

Okay, we're finally getting to the heavier-duty bash commands I use a lot, but maybe I should just write a second post with fun bash commands encountered throughout the day on a day when I'm deeply immersed in it. 

variables 

They're kind of awkward because you assign them one way (with an = sign and no spaces) and use them another way (with a dollar sign in front)

catSound="meow"
echo $catSound 

for loops

When I was making a web photo album of my photos when I was 15, I really wanted thumbnails of all the pictures. BUT I HAD NO IDEA HOW TO DO IT AUTOMATICALLY, so I did it by hand and it was a pain in the ass. These days, I would type

for i in *.jpg; do convert -resize 200x200 $i tiny_$i; done

You can also do it on multiple lines and not have the semi-colons. the "for / do / done" syntax is what is important. If you investigate bash for loops specifically, you'll get more guidance.


making pictures! 

See one of my very first blog posts on imagemagick and doing command line image fun times!! 

a bash script .sh file

Another real-life story. I write C++ code. Then I write Python code. The python code usually runs a bash script to run the C++ code, where the bash script does some setup and additional file mumbo jumbo that I don't want to do in C++.

A bash script of mine that converts an image to a different "pgm" format, runs another program 'sift' to compute sift features on that image, save those features, and delete the pgm version of the image.

You can run these bad boys by saying "sh myFunScript.sh"

.bashrc and .bash_profile

This is your personal bash configuration. It's basically a script that will run when your machine starts up to set everything up JUST THE WAY YOU LIKE IT. Like Goldilocks and those three bears.  Unfortunately, I don't remember the difference between .bash_profile and .bashrc ... only that I seem to have both and sometimes one references the other.

~/.bash_profile and ~/.bashrc


One thing to know: the . in front means it's a seeecret hidden file and it wont show them in 'ls' unless you add the -a flag. It also wont show when you're browsing files. If you use git, it writes things in .git folders and files. Use ls -a to look for them!

flags and 'man'

All these bash commands are powerful because of all the different options and flags they come with. To learn what flags are available, type 'man <command>' to get the manual or man page.

aliases, and how I made 'lady' and alias for 'man'

If you look in my bash profile, you'll see some fine aliases I set up...  including this tweet-inspired one:



Okay, everything I wanted to say about bash off the top of my head is out there. Now it's time to touch (pet) some real cats.

Comments

  1. I like to use ls -lrt. Gives you details, orders by time and reverses the order so if you have a lot of files the most recently changed ones are at the bottom and haven't scrolled out of sight.

    ReplyDelete
  2. Yes! That was really helpful. A lot of that mirrored the stuff we learned the first day (which means that it is super relevant for people who are new to bash) but also it was a really good refresher for me. I had forgotten everything about grep, and maybe need a little more about that, but the examples are super helpful. I think the .st files are beyond me right now, and I wasn't quite clear on what they did. Additionally, I think if I didn't already know about aliases I wouldn't be clear on how to set them up. That's for part 2 though, maybe?

    Also, I am going to be the coolest kid in my class knowing "cd" instead of "cd ~". That just increased my home directory going-to efficiency by 100%

    ReplyDelete
    Replies
    1. Also, I am super into seeing for loops in bash. Did not know they were there, but it's like being at a party that's a little overwhelming and running into an old friend.

      Delete
  3. I like "less", "head", and "tail".

    In particular,
    ls | less
    will give me a list of the files in the current directory that I can view one page at a time.

    ls | head
    will just give me the first ten files.

    ls -t | head
    will give me the last ten files I've modified, and

    ls -t | tail
    will give me the first ten files I've modified.

    I use ctrl-r, command history search, a lot too. If I've recently run a long long command and all I know is that it had the word "herk" in it somewhere, I can press ctrl-r and type "herk" and it will complete to the last command that had "herk" in it. I could press up-arrow a lot too, but searching is often faster for me.

    ReplyDelete

Post a Comment