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!
Table of Contents
- 1 pwd: βreturn working directory name
- 2 ls command in Linux: βList directory contents
- 3 cd command in Linux: βChange directory
- 4 cp Linux command: βCopy a file
- 5 mv command in Linux: βMove
- 6 mkdir Linux command: βMake directory
- 7 rmdir command: Remove directory
- 8 rm command: remove stuff
- 9 cat, less, tail, head commands: View contents
- 10 control+r: Find your previous command
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 screenless
β:βallows you to scroll through the file and even search inside ittail
: 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.