Posts Shell 101
Post
Cancel

Shell 101

Sometimes when I talk to people about Linux, they tell me that they have problems with “this black box where you have to type stuff”. Most likely they just mean a terminal emulator.

There is this believe that you need to be Hackerman to understand how to use a terminal.

Hackerman (Kung Fury): Hackerman (Kung Fury, awesome movie)

But fear no more! I’m here to tell you that using a terminal is not only extremely easy but can also save you a lot of time. After reading this post you’ll be able to do the most common day-to-day tasks on your computer without having to open some annoying GUI application.

I’ll go through the most important commands that you probably need on a daily basis and give you some examples of how to use them. These commands are available on every Linux distribution and should be available on MacOS devices as well.

The basics

First of all let’s look at the general syntax of a command line program:

command [--option1 --option2] argument

Most commands have options that can change the output in some way. Often the options have a abbreviation that is then given with just one - sign, e.g -v instead of --verbose.

Everything that is not an option is an argument. Arguments are separated with spaces.

But what if you want to give a single argument than includes a space? In that case you should wrap the argument into quotation marks:

echo "Hello there!"

This command will print Hello there! to the command line (without the quotation marks of course).


man (MANual)

Theoretically this is the only command you need. It’ll open the documentation for whatever other command you give as input. It is always better to look at the man-page instead of copying a command from the internet. This way you learn to build commands yourself and can be more productive in the future.

man ls

You can use this whenever you are not sure what the options for a command are or how to use them. The manual pages often give some examples as well.

The man-pages are opened with the program less by default. Here you can scroll up and down with your mousewheel and quit by pressing q.


ls (List)

This command will list all the files and directories of your current directory. If you give it a directory name as input, it will list the content of that directory instead.

Some useful options are:

  • -l to get a more list-like output with additional information about permissions and modification date
  • -a to see hidden files

cd (Change Directory)

This is the command that you use to move around in your file system. You can give either a relative path (starting with ./) or an absolute path (starting with /) as input and it will take you there.

cd ~/Documents/

The ~ is an abbreviation for your home directory. So writing ~/Documents/ is the same as /home/[username]/Documents/.

Another trick is to use cd ../. This will move you to the directory above your current directory.


pwd (Print working directory)

Like the name suggests, this command prints the directory that you are currently working in, e.g. from ~/Documents/uni/ to ~/Documents/ .


mv (Move)

Now we get to the actually useful commands. mv lets you move files or directories. It can also be used to rename files. This works because renaming a file is the same as moving it to the same location but with a different file name.

They syntax is very easy:

mv [source] [target]

Moving or renaming would look like this:

Moving:

mv ./hello.txt ./test_directory/hello.txt

Renaming

mv ./hello.txt ./hello_world.txt

cp (Copy)

Just like the mv-command the cp-command behaves like you would expect it from other graphical file managers. The syntax is also exactly the same:

cp [source] [target]

mkdir (Make directory)

This command will create a new directory (folder).

mkdir ./new_directory/

Often you want to create multiple nested directories, e.g. ./new/nested/directory/. If one of the parent directories does not exist, you will get an error because mkdir can’t find the location of the most nested directory. In that case you need to use the -p, --parents flag. This way all the needed subdirectories will get created without creating an error.

mkdir -p ./new/nested/directory/

touch (Create file)

I’m not exactly sure why this command is called touch and not mkfile because that is what it’s used for most of the time. It’s original purpose is to set the modification time of a file to the current time but when the file doesn’t exist it will just create an empty file.

touch ./new_file.txt

This will create a new empty file called new_file.txt in the current directory.


rm (Remove)

This command will remove a file or directory.

rm ./new_file.txt
rm ./new_directory/

If you want to remove a directory that contains files or other directories you need to use the -r (recursive) flag. I urge you to be very, very careful with that command. Sometimes you forget that there are important files in a subdirectory and then there is no way to get them back. If you want to have another layer of protection you could use the -i (interactive) flag. This way you have to confirm each file you delete.

A word of warning: There is a meme on the internet that tells people to write sudo rm -rf /* into their terminal. This would delete every single file on your PC, even the system relevant ones. So better don’t to this unless you want to install a new system anyway. In that case please contact me, because I would love to hear how the system would react in such a case.

This post is licensed under CC BY 4.0 by the author.