1.2. Bash Basics

If you have access to a Mac Terminal or Bash shell, open it up to practice these basic commands. If not, you can use an online emulator like this one, but your capabilities may be slightly limited. There is also a bash cheat sheet which you may find helpful to have up while working in the shell.

1.2.1. Hello World

Type the following code into your shell and press enter:

echo "Hello World"

Did you type it out? Seriously try it!

1.2.3. Making files and directories

If this is your first time on this Linux machine, your home directory might look pretty empty.

cd ~
ls

1.2.3.1. Make directory

Let’s make a directory that we can practice in. We do this with the mkdir (make directory) command. I will call mine test.

mkdir test

You can check if you were successful with the ls command.

ls

Now navigate to your new directory.

cd test
pwd

1.2.3.2. Making an empty file

Now let’s make a new file. The easiest way to make a file is with the touch command.

touch myfile.txt

Touch makes an empty file with the name that you gave it. Let’s see if it’s there:

ls

1.2.3.3. Making a file with text

Great! Now let’s say we want to quickly make a file with a line of text in it. Bash has a handy trick for this which combines two parts. This first is the echo command.

echo 'Hello World!'

Echo simply prints out the string of characters that you pass to it. The second part is the > (redirection) command. Redirection takes the output of the command on the left and puts it into the command or file on the right.

Using echo and > together allows us to make a simple text file quickly! Let’s make a new file called test_file.txt containing one line with the words Hello World.

echo 'Hello World!' > test_file.txt

Use the list command to see if the file was created:

ls

1.2.3.4. Viewing text in files - less is more

Now that we have a text file, we can quickly check its contents with the less command. Press q to exit less (and most command line dialogs you get trapped in).

less test_file.txt

Less allows us to view text interactively on the command line, but what if we just want to print out the contents of a file? Try the more command.

more test_file.txt

Although less is a new and improved version of more (confusingly), more can be used to quickly dump files to the shell or even to redirect output into a new file.

more test_file.txt > test_file2.txt
ls

And to see the contents of our new file:

more test_file2.txt

This is a simple way we can copy files with what we already know, but bash make it even easier for us.

1.2.4. Manipulating files

Let’s learn some commands for manipulating files.

1.2.4.1. Copy

To copy a file, we use the cp command. Let’s give our copy a creative new name like copy_file.txt.

cp test_file.txt copy_file.txt
ls

Check the contents of the copied file with more.

more copy_file.txt

1.2.4.2. Move

We can move files using the mv (move) command. The move command takes a file or directory and moves it to a new path like so: mv file_or_dir /new/path.

If you’ve been following along this tutorial, move your test_file.txt to the home directory.

mv test_file2.txt ~

To see if we were successful, we can use ls without changing directories. Just give the path to the directory you want to list, in this case the home directory:

ls ~

1.2.4.3. Rename

Great! The mv command also has another fun trick which is renaming files. To do this, you “move” the file to the same directory, but with a new name. Rename test_file2.txt to awesome_file.txt.

mv test_file2.txt awesome_file.txt
ls

1.2.4.4. Remove

What if we want to delete a file or directory? We do this with the rm (remove) command.

rm awesome_file.txt
ls

We deleted our file, now let’s delete our directory. First navigate up to the parent directory then remove the test directory with rm.

cd ..
rm test/

Got an error? Here, rm is making sure we don’t accidentally delete a directory full of files we may need. To proceed with removing a directory we need to add an option to the rm command.

1.2.4.5. Info

Options are used to modify the behavior of bash commands. To see what options are available, we can use the info (information) command - you can also use the unnecessarily gendered man (manual page) command.

info rm

Remember to hit q to leave most scary dialogs in the shell. Did you notice the -d or --dir option for removing directories?

Adding -d or --dir to rm do the same thing, the -d is just shorter for us lazy folks who can’t bear to type 3 extra characters.

rm -d test
ls

Looks like rm is looking out for us again because the directory is not yet empty. Let’s see what is still in there.

ls

1.2.4.6. Wildcard

Inserting a wildcard into a path will match any path with any number of characters replaced by the *.

For example, if a directory called data/ has a.txt b.png c.txt in it, the path /data/*.txt will match only a.txt and c.txt. Alternatively, the path data/* will match all three files.

Let’s use the wildcard to delete all files in the test/ directory:

rm test/*
ls test

Now test should be empty. Finally, let’s remove test/ with the rm -d command.

rm -d test

And it’s gone! Like most things in bash that take a few steps, they can be shortened with options.

Remove has a -f (force) option and a -r (recursive) option. Combining these will remove all file and folders at the given path. The equivalent to clearing the directory and deleting it would have been:

rm -f test

And if test had sub-directories, we would have needed to use:

rm -rf test

Caution: Unlike the delete button, moves your files into the trash can, rm will permanently wipe them out from your computer. So you can delete a lot of files very quickly using rm -rf and never get them back. Make sure you know what is at the path you are deleting before you use this command. That being said, everyone makes mistakes, which is why we encourage everyone to backup their files with version control, but more on that later.

1.2.5. Learning more

Bookmark this cheat sheet for all of the basics we covered here and more. The next section covers permissions, which determines who can read, write and run the different files on your computer.