Linux Commands: 10 Commands You Must Know By Heart

You can significantly increase your productivity and understanding of your operating system by learning a few basic shell Linux commands. The Linux commands in this tutorial work in Bash, the most common shell on Linux and MacOS. However, alternative shells are mostly compatible with Bash, so they should work everywhere!

If these linux commands are well known to you, please skip to the next sections to learn some more advanced commands and shell scripting. E.g., learn how to create Bash scripts, or do multiprocessing in Bash!

pwd:  return working directory name

Let’s start by knowing where you are once you opened your terminal. This is done with the pwd Linux command:

$ pwd
/Users/erik

I’m in my home directory (on a Mac), where are you?

ls command in Linux:  List directory contents

Now let’s see what is in the current directory:

$ ls
AndroidStudioProjects     VirtualBox VMs
Applications              Desktop
Documents                 flutter
Downloads...

There’s a lot here because I’m a messy guy, so I truncated it a little. The ls command has many convenient options; these three I use all the time:

  • -l: (The lowercase letter “ell”.) List in long format. (See below.) A total sum for all the file sizes is output on a line before the long listing
  • -h: used with the -l option, this changes the file sizes into something more readable, like 10G, 42M. In my head, I translate this to ‘human-readable’
  • -a: Short for ‘all’; include ‘hidden’ directory entries whose names begin with a dot (.)

You can combine these any way you need, like this:

$ ls -lha
total: 32
drwxr-xr-x+ 85 erik staff 2.7K May 27 11:11 .
drwxr-xr-x   6 root admin 193B Sep 29  2019 ..
-rw-r--r--   1 erik staff 2.1K Apr 15 11:06 .gitignore
-rw-r--r--   1 erik staff  25M May 26 15:44 image.zip
...

What you see here are the access rights, the owner and group, the file size, the file’s last modified data, and the file name itself.

On the first two rows, you might notice something odd: two files named . and ..:

  • . is the current directory. It even has a size, which is dependant on the number of files in the directory
  • .. is the underlying directory, which the current directory is part of

These handy shortcuts can be used in every command. If you want to view the contents of the underlying directory, use ls ../. You can also repeat this, so if you want to go down two directories, you can use ls ../../.

cd command in Linux:  Change directory

Now that you know where you are and what directories there are, you want to navigate around. Let’s say your projects are stored in the Projects folder. You can go there by typing the following Linux command:

$ cd Projects

Most shells support auto-completion. Just try it by typing:

$ cd Pro<TAB key>

Your shell will auto-complete the word or, if multiple directories match what you typed, it will usually show you all the options.

cp Linux command:  Copy a file

You may want to copy or duplicate some files. The cp command in Linux does just that: it copies a file. It does so by creating a new file and copying all the contents to that file. To copy file1.txt to a second file, e.g., to back it up, use:

$ cp file1.txt file1.txt.bak

To copy a file to another directory, you can use:

$ cp file1.txt ./backups/

The ./ means ‘the current directory,’ so ./backups is located in the current working directory.

mv command in Linux:  Move

If you need to move a file, use the mv command. Note that moving a file is the same as renaming a file. In fact, there is no rename command on the Bash command line. Using mv is not much different from cp. To rename/move a file:

$ mv file1.txt file1.txt.bak

To move a file to another directory, e.g. to the backups folder in the current working directory:

$ mv file1.txt backups/

mkdir Linux command:  Make directory

This Linux command will simply create a directory. To create a directory to store your projects in, use:

$ mkdir Projects

There’s one very convenient argument to this command, -p. When you use it, any intermediate directories that don’t exist yet will also be created. If you want to create your first project inside of a directory called Projects, you can do so in one sweep with:

$ mkdir -p Projects/my_first_project

rmdir command: Remove directory

The opposite of mkdir is rmdir: it removes a directory. It even allows the same -p argument.

$ rm Projects

To remove the Projects directory and the first project we just created:

$ rmdir -p Projects/my_first_project

Removing a directory will only work when that directory is empty. If you want to remove a directory, including its contents, see the rm command which is up next!

rm command: remove stuff

Now we’re getting into dangerous territory. This Linux command is powerful and has the potential to destroy your file system. Use it with care.

Let’s start by removing a file in Linux:

$ rm file1.txt

Poof! It’s gone. There’s no ‘Trash Can’ as you might have grown accustomed to in other OSes.

Now let’s remove all files in our Projects directory that end with .txt. You need a so-called glob pattern that matches multiple files:

$ rm Projects/*.txt

If you want to delete all files, use:

$ rm Projects/*

The glob pattern * matches everything!

If you want to err on the side of caution, use the -i option, which forces you to confirm the delete operation.

Now, for the grand finale, let’s delete an entire tree of directories and files. For this, you need the argument -r, short for recursive. rm -r will sometimes refuse to remove special files, or constantly ask for confirmation. You can use the force option -f to remove everything it can; no questions asked. Let’s remove all our projects:

$ rm -rf Projects

Poof! All your work is  gone forever. Like I said, handle with care!

cat, less, tail, head commands: View contents

You often want to quickly inspect the contents of a file. There are several Linux commands to help you with this. Pick one that matches your use case:

  • cat: print everything on your screen
  • less : allows you to scroll through the file and even search inside it
  • tail: like cat, but only print the last 10 lines (by default)
  • head: like cat, but only shows the first 10 lines (by default)

Both tail and head take the -n <num> option to change the number of lines it shows.

control+r: Find your previous command

It’s easy to forget, so wouldn’t it be nice to quickly find that Linux command you used yesterday or last week? Well, you can! Just hit control + r on your keyboard and start typing the part of the command you do remember. This can be any part! If what you found is not entirely what you were looking for, keep searching by repeatedly hitting control + r. It will keep giving you matches from your command history.

About Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

Subscribe
Notify of
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments