Linux Commands Handbook
Linux Commands Handbook
Preface
Introduction to Linux and shells
man
ls
cd
pwd
mkdir
rmdir
mv
cp
open
touch
find
ln
gzip
gunzip
tar
alias
cat
less
tail
wc
grep
sort
1
Linux Training @karthi
uniq
diff
echo
chown
chmod
umask
du
df
basename
dirname
ps
top
kill
killall
jobs
bg
fg
type
which
nohup
xargs
vim
emacs
nano
whoami
who
su
sudo
2
Linux Training @karthi
passwd
ping
traceroute
clear
history
export
crontab
uname
env
printenv
Conclusion
3
Linux Training @karthi
Preface
The Linux Commands Handbook follows the 80/20
rule: learn in 20% of the time the 80% of a topic.
Enjoy!
4
Linux Training @karthi
Introduction to Linux and
shells
Linux is an operating system, like macOS or Windows.
5
Linux Training @karthi
Linux can also be used as your day to day computer. I
use macOS because I really enjoy the applications,
the design and I also used to be an iOS and Mac apps
developer, but before using it I used Linux as my main
computer Operating System.
6
Linux Training @karthi
If you use a Mac you need to know that under the
hood macOS is a UNIX Operating System, and it
shares a lot of the same ideas and software that a
GNU/Linux system uses, because GNU/Linux is a free
alternative to UNIX.
But the vast majority of the time you will run a Linux
computer in the cloud via a VPS (Virtual Private
Server) like DigitalOcean.
7
Linux Training @karthi
There are many different kind of shells. This post
focuses on Unix shells, the ones that you will find
commonly on Linux and macOS computers.
8
Linux Training @karthi
man
The first command I want to introduce is a command
that will help you understand all the other commands.
9
Linux Training @karthi
This is not a substitute for man , but a handy tool to
avoid losing yourself in the huge amount of
information present in a man page. Then you can use
the man page to explore all the different options and
parameters you can use on a command.
10
Linux Training @karthi
ls
Inside a folder you can list all the files that the folder
contains using the ls command:
ls
ls /bin
ls -al /bin
11
Linux Training @karthi
compared to the plain ls , this returns much more
information.
12
Linux Training @karthi
cd
Once you have a folder, you can move into it using the
cd command. cd means change directory. You
invoke it specifying a folder to move into. You can
specify a folder name, or an entire path.
Example:
mkdir fruits
cd fruits
mkdir fruits
mkdir cars
cd fruits
cd ../cars
You can also use absolute paths, which start from the
root folder / :
13
Linux Training @karthi
cd /etc
14
Linux Training @karthi
pwd
Whenever you feel lost in the filesystem, call the pwd
pwd
15
Linux Training @karthi
mkdir
You create folders using the mkdir command:
mkdir fruits
mkdir -p fruits/apples
16
Linux Training @karthi
rmdir
Just as you can create a folder using mkdir , you can
delete a folder using rmdir :
mkdir fruits
rmdir fruits
17
Linux Training @karthi
mv
Once you have a file, you can move it around using
the mv command. You specify the file current path,
and its new path:
touch test
mv pear new_pear
touch pear
touch apple
mkdir fruits
mv pear apple fruits #pear and apple moved to the fr
18
Linux Training @karthi
cp
You can copy a file using the cp command:
touch test
cp apple another_apple
mkdir fruits
cp -r fruits cars
19
Linux Training @karthi
open
The open command lets you open a file using this
syntax:
open <filename>
open .
20
Linux Training @karthi
touch
You can create an empty file using the touch
command:
touch apple
21
Linux Training @karthi
find
The find command can be used to find files or
folders matching a particular search pattern. It
searches recursively.
Find all the files under the current tree that have the
.js extension and print the relative path of each file
matching:
22
Linux Training @karthi
Find directories under the current tree matching the
name "node_modules" or 'public':
23
Linux Training @karthi
You can execute a command on each result of the
search. In this example we run cat to print the file
content:
24
Linux Training @karthi
ln
The ln command is part of the Linux file system
commands.
Hard links
Hard links are rarely used. They have a few
limitations: you can't link to directories, and you can't
link to external filesystems (disks).
ln <original> <link>
ln recipes.txt newrecipes.txt
25
Linux Training @karthi
Now any time you edit any of those files, the content
will be updated for both.
If you delete the original file, the link will still contain
the original file content, as that's not removed until
there is one hard link pointing to it.
Soft links
Soft links are different. They are more powerful as you
can link to other filesystems and to directories, but
when the original is removed, the link will be broken.
26
Linux Training @karthi
ln -s <original> <link>
ln -s recipes.txt newrecipes.txt
27
Linux Training @karthi
gzip
You can compress a file using the gzip compression
protocol named LZ77 using the gzip command.
gzip filename
gzip -k filename
option:
28
Linux Training @karthi
gzip -1 filename
gzip -r a_folder
gzip -d filename.gz
29
Linux Training @karthi
gunzip
The gunzip command is basically equivalent to the
gzip command, except the -d option is always
enabled by default.
gunzip filename.gz
30
Linux Training @karthi
tar
The tar command is used to create an archive,
grouping multiple files in a single file.
31
Linux Training @karthi
tar is often used to create a compressed archive,
gzipping the archive.
32
Linux Training @karthi
alias
It's common to always run a program with a set of
options you like using.
33
Linux Training @karthi
alias ll='ls -al'
34
Linux Training @karthi
alias lsthis="ls $PWD"
alias lscurrent='ls $PWD'
lists the files in the new folder, lsthis still lists the
files in the folder you were when you defined the alias.
35
Linux Training @karthi
cat
Similar to tail in some way, we have cat . Except
cat can also add content to a file, and this makes it
super powerful.
cat file
cat -n file1
36
Linux Training @karthi
You can only add a number to non-blank lines using -
37
Linux Training @karthi
less
The less command is one I use a lot. It shows you
the content stored inside a file, in a nice and
interactive UI.
38
Linux Training @karthi
This command just visualises the file's content. You
can directly open an editor by pressing v . It will use
the system editor, which in most cases is vim .
<filename> command.
39
Linux Training @karthi
tail
The best use case of tail in my opinion is when called
with the -f option. It opens the file at the end, and
watches for file changes. Any time there is new
content in the file, it is printed in the window. This is
great for watching log files, for example:
tail -f /var/log/system.log
tail -n 10 <filename>
40
Linux Training @karthi
wc
The wc command gives us useful information about
a file or input it receives via pipes.
ls -al | wc
6 47 284
wc -l test.txt
wc -w test.txt
wc -c test.txt
41
Linux Training @karthi
Bytes in ASCII charsets equate to characters, but with
non-ASCII charsets, the number of characters might
differ because some characters might take multiple
bytes, for example this happens in Unicode.
wc -m test.txt
42
Linux Training @karthi
grep
The grep command is a very useful tool, that when
you master will help you tremendously in your day to
day.
43
Linux Training @karthi
One very useful thing is to tell grep to print 2 lines
before, and 2 lines after the matched line, to give us
more context. That's done using the -C option, which
accepts a number of lines:
44
Linux Training @karthi
Another thing you might find very useful is to invert the
result, excluding the lines that match a particular
string, using the -v option:
45
Linux Training @karthi
sort
Suppose you have a text file which contains the
names of dogs:
46
Linux Training @karthi
Sorting by default is case sensitive, and alphabetic.
Use the --ignore-case option to sort case insensitive,
and the -n option to sort using a numeric order.
47
Linux Training @karthi
sort does not just works on files, as many UNIX
commands it also works with pipes, so you can use on
the output of another command, for example you can
order the files returned by ls with:
ls | sort
48
Linux Training @karthi
49
Linux Training @karthi
uniq
uniq is a command useful to sort lines of text.
You can get those lines from a file, or using pipes from
the output of another command:
uniq dogs.txt
ls | uniq
This implies that you will most likely use it along with
sort :
50
Linux Training @karthi
You can tell it to only display duplicate lines, for
example, with the -d option:
51
Linux Training @karthi
You can use the -u option to only display non-
duplicate lines:
c option:
52
Linux Training @karthi
Use the special combination:
53
Linux Training @karthi
diff
diff is a handy command. Suppose you have 2
files, which contain almost the same information, but
you can't find the difference between the two.
diff will process the files and will tell you what's the
difference.
54
Linux Training @karthi
If you invert the order of the files, it will tell you that the
second file is missing line 3, whose content is
Vanille :
55
Linux Training @karthi
In case you're interested in which files differ, rather
than the content, use the r and q options:
56
Linux Training @karthi
57
Linux Training @karthi
echo
The echo command does one simple job: it prints to
the output the argument passed to it.
This example:
echo "hello"
58
Linux Training @karthi
This is just the start. We can do some nice things
when it comes to interacting with the shell features.
echo *
echo o*
Any valid Bash (or any shell you are using) command
and feature can be used here.
echo ~
59
Linux Training @karthi
Note that whitespace is not preserved by default. You
need to wrap the command in double quotes to do so:
echo {1..5}
60
Linux Training @karthi
chown
Every file/directory in an Operating System like Linux
or macOS (and every UNIX systems in general) has
an owner.
command:
Like this:
61
Linux Training @karthi
It's rather common to have the need to change the
ownership of a directory, and recursively all the files
contained, plus all the subdirectories and the files
contained in them, too.
Example:
You can also just change the group of a file using the
chgrp command:
62
Linux Training @karthi
chmod
Every file in the Linux / macOS Operating Systems
(and UNIX systems in general) has 3 permissions:
Read, write, execute.
The weird strings you see on each file line, like drwxr-
63
Linux Training @karthi
The third set represents the permissions of the
everyone else
64
Linux Training @karthi
You can apply the same permissions to multiple
personas by adding multiple letters before the + / - :
0 no permissions
1 can execute
2 can write
3 can write, execute
4 can read
5 can read, execute
6 can read, write
7 can read, write and execute
65
Linux Training @karthi
chmod 777 filename
chmod 755 filename
chmod 644 filename
66
Linux Training @karthi
umask
When you create a file, you don't have to decide
permissions up front. Permissions have defaults.
67
Linux Training @karthi
Other users belonging to the same group ( g ) have
read and execution permission, same as all the other
users ( o ).
We can set a new value for the mask setting the value
in numeric format:
umask 002
umask g+r
68
Linux Training @karthi
du
The du command will calculate the size of a directory
as a whole:
du
69
Linux Training @karthi
The -h option will show a human-readable notation
for sizes, adapting to the size:
70
Linux Training @karthi
71
Linux Training @karthi
df
The df command is used to get disk usage
information.
72
Linux Training @karthi
basename
Suppose you have a path to a file, for example
/Users/flavio/test.txt .
Running
basename /Users/flavio/test.txt
73
Linux Training @karthi
dirname
Suppose you have a path to a file, for example
/Users/flavio/test.txt .
Running
dirname /Users/flavio/test.txt
74
Linux Training @karthi
ps
Your computer is running, at all times, tons of different
processes.
75
Linux Training @karthi
The a option is used to also list other users
processes, not just our own. x shows processes
not linked to any terminal (not initiated by users
through a terminal).
76
Linux Training @karthi
You can search for a specific process combining
grep with a pipe, like this:
77
Linux Training @karthi
It's common to have + which indicates the process is
in the foreground in its terminal. s means the
process is a session leader.
78
Linux Training @karthi
top
A quick guide to the top command, used to list the
processes running in real time
79
Linux Training @karthi
Below, the list of processes taking the most memory
and CPU is constantly updated.
top -o mem
80
Linux Training @karthi
kill
Linux processes can receive signals and react to
them.
kill <PID>
81
Linux Training @karthi
INT means interrupt, and it sends the same signal
used when we press ctrl-C in the terminal, which
usually terminates the process.
82
Linux Training @karthi
killall
Similar to the kill command, killall instead of
sending a signal to a specific process id will send the
signal to multiple processes at once.
killall <name>
You can specify the signal, like with kill (and check
the kill tutorial to read more about the specific
kinds of signals we can send), for example:
83
Linux Training @karthi
jobs
When we run a command in Linux / macOS, we can
set it to run in the background using the & symbol
after the command. For example we can run top in
the background:
top &
Z .
84
Linux Training @karthi
bg
When a command is running you can suspend it using
ctrl-Z .
85
Linux Training @karthi
fg
When a command is running in the background,
because you started it with & at the end (example:
top & or because you put it in the background with
the bg command, you can put it to the foreground
using fg .
Running
fg
86
Linux Training @karthi
87
Linux Training @karthi
type
A command can be one of those 4 types:
an executable
a shell built-in program
a shell function
an alias
This is Zsh:
88
Linux Training @karthi
This is Fish:
89
Linux Training @karthi
One of the most interesting things here is that for
aliases it will tell you what is aliasing to. You can see
the ll alias, in the case of Bash and Zsh, but Fish
provides it by default, so it will tell you it's a built-in
shell function.
90
Linux Training @karthi
which
Suppose you have a command you can execute,
because it's in the shell path, but you want to know
where it is located.
91
Linux Training @karthi
nohup
Sometimes you have to run a long-lived process on a
remote machine, and then you need to disconnect.
92
Linux Training @karthi
xargs
The xargs command is used in a UNIX shell to
convert input from standard input into arguments to a
command.
93
Linux Training @karthi
We will channel the output of cat todelete.txt to the
rm command, through xargs .
In this way:
94
Linux Training @karthi
The -n option lets you tell xargs to perform one
iteration at a time, so you can individually confirm
them with -p . Here we tell xargs to perform one
iteration at a time with -n1 :
95
Linux Training @karthi
vim
vim is a very popular file editor, especially among
programmers. It's actively developed and frequently
updated, and there's a very big community around it.
There's even a Vim conference!
vi test.txt
96
Linux Training @karthi
You have to know that Vim has 2 main modes:
Now you can start typing and filling the screen with the
file contents:
97
Linux Training @karthi
You can move around the file with the arrow keys, or
using the h - j - k - l keys. h-l for left-right,
j-k for down-up.
Once you are done editing you can press the esc
At this point you can navigate the file, but you can't
add content to it (and be careful which keys you press
as they might be commands).
98
Linux Training @karthi
You can save and quit pressing : then w and q :
:wq
and ! : :q!
99
Linux Training @karthi
will greatly help you start your vim explorations.
100
Linux Training @karthi
emacs
emacs is an awesome editor and it's historically
regarded as the editor for UNIX systems. Famously
vi vs emacs flame wars and heated discussions
caused many unproductive hours for developers
around the world.
101
Linux Training @karthi
macOS users, stop a second now. If you are on
Linux there are no problems, but macOS does not
ship applications using GPLv3, and every built-in
UNIX command that has been updated to GPLv3
has not been updated. While there is a little
problem with the commands I listed up to now, in
this case using an emacs version from 2007 is not
exactly the same as using a version with 12 years
of improvements and change. This is not a
problem with Vim, which is up to date. To fix this,
run brew install emacs and running emacs will
use the new version from Homebrew (make sure
you have Homebrew installed)
<filename> :
You can start editing and once you are done, press
ctrl-x followed by ctrl-w . You confirm the folder:
102
Linux Training @karthi
and Emacs tell you the file exists, asking you if it
should overwrite it:
103
Linux Training @karthi
You can exit Emacs pressing ctrl-x followed by
ctrl-c . Or ctrl-x followed by c (keep ctrl
pressed).
104
Linux Training @karthi
nano
nano is a beginner friendly editor.
105
Linux Training @karthi
whoami
Type whoami to print the user name currently logged
in to the terminal session:
106
Linux Training @karthi
who
The who command displays the users logged in to
the system.
You can see the name of the terminal used, and the
time/day the session was started.
107
Linux Training @karthi
The special who am i command will list the current
terminal session details:
108
Linux Training @karthi
su
While you're logged in to the terminal shell with one
user, you might have the need to switch to another
user.
su <username>
109
Linux Training @karthi
sudo
sudo is commonly used to run a command as root.
110
Linux Training @karthi
sudo -u flavio ls /Users/flavio
111
Linux Training @karthi
passwd
Users in Linux have a password assigned. You can
change the password using the passwd command.
passwd
112
Linux Training @karthi
ping
The ping command pings a specific network host, on
the local network or on the Internet.
113
Linux Training @karthi
Not all servers support pinging, in case the requests
times out:
114
Linux Training @karthi
traceroute
When you try to reach a host on the Internet, you go
through your home router, then you reach your ISP
network, which in turn goes through its own upstream
network router, and so on, until you finally reach the
host.
Have you ever wanted to know what are the steps that
your packets go through to do that?
You invoke
traceroute <host>
115
Linux Training @karthi
Not every router travelled returns us information. In
this case, traceroute prints * * * . Otherwise, we
can see the hostname, the IP address, and some
performance indicator.
traceroute -q 1 flaviocopes.com
116
Linux Training @karthi
clear
Type clear to clear all the previous commands that
were ran in the current terminal.
The screen will clear and you will just see the prompt
at the top:
117
Linux Training @karthi
history
Every time we run a command, that's memorized in
the history.
history
command.
118
Linux Training @karthi
To clear the history, run history -c
119
Linux Training @karthi
export
The export command is used to export variables to
child processes.
TEST="test"
120
Linux Training @karthi
Then you set chmod u+x script.sh and you execute
this script with ./script.sh , the echo $TEST line will
print nothing!
TEST="test"
export TEST="test"
export PATH=$PATH:/new/path
121
Linux Training @karthi
It's common to use export when you create new
variables in this way, but also when you create
variables in the .bash_profile or .bashrc
export -n TEST
122
Linux Training @karthi
crontab
Cron jobs are jobs that are scheduled to run at specific
intervals. You might have a command perform
something every hour, or every day, or every 2 weeks.
Or on weekends. They are very powerful, especially
on servers to perform maintenance and automations.
crontab -l
Run
crontab -e
123
Linux Training @karthi
By default this opens with the default editor, which is
usually vim . I like nano more, you can use this line
to use a different editor:
EDITOR=nano crontab -e
Now you can add one line for each cron job.
You pick a time interval for the cron job, and you type
the command to execute.
124
Linux Training @karthi
I chose to run a script located in
/Users/flavio/test.sh every 12 hours. This is the
crontab line I need to run:
I run crontab -e :
EDITOR=nano crontab -e
to save.
Once this is done, you can see the list of active cron
jobs by running:
crontab -l
125
Linux Training @karthi
You can remove a cron job running crontab -e again,
removing the line and exiting the editor:
126
Linux Training @karthi
uname
Calling uname without any options will return the
Operating System codename:
127
Linux Training @karthi
The n option prints the node network name:
128
Linux Training @karthi
On macOS you can also use the sw_vers command
to print more information about the macOS Operating
System. Note that this differs from the Darwin (the
Kernel) version, which above is 19.6.0 .
129
Linux Training @karthi
env
The env command can be used to pass environment
variables without setting them on the outer
environment (the current shell).
interface.
option:
program:
130
Linux Training @karthi
Try with a simple app.js file with this content:
console.log(process.env.NAME)
console.log(process.env.PATH)
undefined
undefined
flavio
undefined
env
131
Linux Training @karthi
it will return a list of the environment variables set, for
example:
HOME=/Users/flavio
LOGNAME=flavio
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/L
PWD=/Users/flavio
SHELL=/usr/local/bin/fish
132
Linux Training @karthi
printenv
A quick guide to the printenv command, used to
print the values of environment variables
HOME=/Users/flavio
LOGNAME=flavio
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/L
PWD=/Users/flavio
SHELL=/usr/local/bin/fish
printenv PATH
133
Linux Training @karthi