The Linux Commands HandBook
The Linux Commands HandBook
The Linux Commands HandBook
com/
Table of Contents
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
passwd
ping
traceroute
clear
history
export
crontab
uname
env
printenv
Conclusion
3 https://cyberpublicschool:com/
Preface
The Linux Commands Handbook follows the 80/20
rule: learn in 20% of the time the 80% of a topic.
4 https://cyberpublicschool:com/
Introduction to Linux and
shells
Linux is an operating system, like macOS or Windows.
5 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
man
The first command I want to introduce is a command
that will help you understand all the other commands.
9 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
ls
Inside a folder you can list all the files that the folder
contains using the ts command:
ts
ts /bin
ts –at /bin
11 https://cyberpublicschool:com/
compared to the plain ts , this returns much more
information.
12 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
cd /etc
14 https://cyberpublicschool:com/
pwd
Whenever you feel lost in the filesystem, call the pwd
pwd
15 https://cyberpublicschool:com/
mkdir
You create folders using the mkdir command:
mkdir fruits
mkdir –p fruits/apptes
16 https://cyberpublicschool:com/
rmdir
Just as you can create a folder using mkdir , you can
delete a folder using rmdir :
mkdir fruits
rmdir fruits
17 https://cyberpublicschool:com/
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 appte
mkdir fruits
mv pear appte fruits #pear and appte moved to the f
18 https://cyberpublicschool:com/
cp
You can copy a file using the cp command:
touch test
cp appte another_appte
mkdir fruits
cp –r fruits cars
19 https://cyberpublicschool:com/
open
The open command lets you open a file using this
syntax:
open <fitename>
open .
20 https://cyberpublicschool:com/
touch
You can create an empty file using the touch
command:
touch appte
21 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
Find directories under the current tree matching the
name "node_modules" or 'public':
23 https://cyberpublicschool:com/
You can execute a command on each result of the
search. In this example we run cat to print the file
content:
24 https://cyberpublicschool:com/
ln
The tn 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).
tn <originat> <tink>
tn recipes.txt newrecipes.txt
25 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
tn –s <originat> <tink>
tn –s recipes.txt newrecipes.txt
27 https://cyberpublicschool:com/
gzip
You can compress a file using the gzip compression
protocol named LZ77 using the gzip command.
gzip fitename
gzip –k fitename
option:
28 https://cyberpublicschool:com/
gzip –1 fitename
gzip –r a_fotder
gzip –d fitename.gz
29 https://cyberpublicschool:com/
gunzip
The gunzip command is basically equivalent to the
gzip command, except the –d option is always
enabled by default.
gunzip fitename.gz
30 https://cyberpublicschool:com/
tar
The tar command is used to create an archive,
grouping multiple files in a single file.
31 https://cyberpublicschool:com/
tar is often used to create a compressed archive,
gzipping the archive.
32 https://cyberpublicschool:com/
alias
It's common to always run a program with a set of
options you like using.
33 https://cyberpublicschool:com/
atias tt='ts –at'
34 https://cyberpublicschool:com/
atias tsthis="ts $PWD"
atias tscurrent='ts $PWD'
lists the files in the new folder, tsthis still lists the
files in the folder you were when you defined the alias.
35 https://cyberpublicschool:com/
cat
Similar to tait in some way, we have cat . Except
cat can also add content to a file, and this makes it
super powerful.
cat fite
cat –n fite1
36 https://cyberpublicschool:com/
You can only add a number to non-blank lines using –
37 https://cyberpublicschool:com/
less
The tess command is one I use a lot. It shows you
the content stored inside a file, in a nice and
interactive UI.
38 https://cyberpublicschool:com/
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 .
<fitename> command.
39 https://cyberpublicschool:com/
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:
tait –f /var/tog/system.tog
tait –n 1θ <fitename>
40 https://cyberpublicschool:com/
wc
The wc command gives us useful information about
a file or input it receives via pipes.
ts –at | wc
6 47 284
wc –t test.txt
wc –w test.txt
wc –c test.txt
41 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
grep
The grep command is a very useful tool, that when
you master will help you tremendously in your day to
day.
43 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
sort
Suppose you have a text file which contains the
names of dogs:
46 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 ts with:
ts | sort
48 https://cyberpublicschool:com/
49 https://cyberpublicschool:com/
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
ts | uniq
This implies that you will most likely use it along with
sort :
50 https://cyberpublicschool:com/
You can tell it to only display duplicate lines, for
example, with the –d option:
51 https://cyberpublicschool:com/
You can use the –u option to only display non-
duplicate lines:
c option:
52 https://cyberpublicschool:com/
Use the special combination:
53 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
If you invert the order of the files, it will tell you that the
second file is missing line 3, whose content is
Vanitte :
55 https://cyberpublicschool:com/
In case you're interested in which files differ, rather
than the content, use the r and q options:
56 https://cyberpublicschool:com/
57 https://cyberpublicschool:com/
echo
The echo command does one simple job: it prints to
the output the argument passed to it.
This example:
echo "hetto"
58 https://cyberpublicschool:com/
This is just the start. We can do some nice things
when it comes to interacting with the shell features.
echo C
echo oC
Any valid Bash (or any shell you are using) command
and feature can be used here.
echo ~
59 https://cyberpublicschool:com/
Note that whitespace is not preserved by default. You
need to wrap the command in double quotes to do so:
echo {1..5}
60 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
The third set represents the permissions of the
everyone else
64 https://cyberpublicschool:com/
You can apply the same permissions to multiple
personas by adding multiple letters before the + / – :
θ 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 https://cyberpublicschool:com/
chmod 777 fitename
chmod 755 fitename
chmod 644 fitename
66 https://cyberpublicschool:com/
umask
When you create a file, you don't have to decide
permissions up front. Permissions have defaults.
67 https://cyberpublicschool:com/
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 θθ2
umask g+r
68 https://cyberpublicschool:com/
du
The du command will calculate the size of a directory
as a whole:
du
69 https://cyberpublicschool:com/
The –h option will show a human-readable notation
for sizes, adapting to the size:
70 https://cyberpublicschool:com/
71 https://cyberpublicschool:com/
df
The df command is used to get disk usage
information.
72 https://cyberpublicschool:com/
basename
Suppose you have a path to a file, for example
/Users/ftavio/test.txt .
Running
basename /Users/ftavio/test.txt
73 https://cyberpublicschool:com/
dirname
Suppose you have a path to a file, for example
/Users/ftavio/test.txt .
Running
dirname /Users/ftavio/test.txt
74 https://cyberpublicschool:com/
ps
Your computer is running, at all times, tons of different
processes.
75 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
You can search for a specific process combining
grep with a pipe, like this:
77 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
top
A quick guide to the top command, used to list the
processes running in real time
79 https://cyberpublicschool:com/
Below, the list of processes taking the most memory
and CPU is constantly updated.
top –o mem
80 https://cyberpublicschool:com/
kill
Linux processes can receive signals and react to
them.
kitt <PID>
81 https://cyberpublicschool:com/
INT means interrupt, and it sends the same signal
used when we press ctrt–C in the terminal, which
usually terminates the process.
82 https://cyberpublicschool:com/
killall
Similar to the kitt command, kittatt instead of
sending a signal to a specific process id will send the
signal to multiple processes at once.
kittatt <name>
You can specify the signal, like with kitt (and check
the kitt tutorial to read more about the specific
kinds of signals we can send), for example:
83 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
bg
When a command is running you can suspend it using
ctrt–Z .
85 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
87 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
This is Fish:
89 https://cyberpublicschool:com/
One of the most interesting things here is that for
aliases it will tell you what is aliasing to. You can see
the tt 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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
nohup
Sometimes you have to run a long-lived process on a
remote machine, and then you need to disconnect.
92 https://cyberpublicschool:com/
xargs
The xargs command is used in a UNIX shell to
convert input from standard input into arguments to a
command.
93 https://cyberpublicschool:com/
We will channel the output of cat todetete.txt to the
rm command, through xargs .
In this way:
94 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
You have to know that Vim has 2 main modes:
Now you can start typing and filling the screen with the
file contents:
97 https://cyberpublicschool:com/
You can move around the file with the arrow keys, or
using the h - j - k - t keys. h–t 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 https://cyberpublicschool:com/
You can save and quit pressing : then w and q :
:wq
and ! : :q!
99 https://cyberpublicschool:com/
will greatly help you start your vim explorations.
100 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
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 instatt emacs and running emacs will
use the new version from Homebrew (make sure
you have Homebrew installed)
<fitename> :
You can start editing and once you are done, press
ctrt–x followed by ctrt–w . You confirm the folder:
102 https://cyberpublicschool:com/
and Emacs tell you the file exists, asking you if it
should overwrite it:
103 https://cyberpublicschool:com/
You can exit Emacs pressing ctrt–x followed by
ctrt–c . Or ctrt–x followed by c (keep ctrt
pressed).
104 https://cyberpublicschool:com/
nano
nano is a beginner friendly editor.
105 https://cyberpublicschool:com/
whoami
Type whoami to print the user name currently logged
in to the terminal session:
106 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
The special who am i command will list the current
terminal session details:
108 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
sudo
sudo is commonly used to run a command as root.
110 https://cyberpublicschool:com/
sudo –u ftavio ts /Users/ftavio
111 https://cyberpublicschool:com/
passwd
Users in Linux have a password assigned. You can
change the password using the passwd command.
passwd
112 https://cyberpublicschool:com/
ping
The ping command pings a specific network host, on
the local network or on the Internet.
113 https://cyberpublicschool:com/
Not all servers support pinging, in case the requests
times out:
114 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
Not every router travelled returns us information. In
this case, traceroute prints C C C . Otherwise, we
can see the hostname, the IP address, and some
performance indicator.
traceroute –q 1 ftaviocopes.com
116 https://cyberpublicschool:com/
clear
Type ctear 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 https://cyberpublicschool:com/
history
Every time we run a command, that's memorized in
the history.
history
command.
118 https://cyberpublicschool:com/
To clear the history, run history –c
119 https://cyberpublicschool:com/
export
The export command is used to export variables to
child processes.
TEST="test"
120 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
It's common to use export when you create new
variables in this way, but also when you create
variables in the .bash_profite or .bashrc
export –n TEST
122 https://cyberpublicschool:com/
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 –t
Run
crontab –e
123 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
I chose to run a script located in
/Users/ftavio/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 –t
125 https://cyberpublicschool:com/
You can remove a cron job running crontab –e again,
removing the line and exiting the editor:
126 https://cyberpublicschool:com/
uname
Calling uname without any options will return the
Operating System codename:
127 https://cyberpublicschool:com/
The n option prints the node network name:
128 https://cyberpublicschool:com/
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.θ .
129 https://cyberpublicschool:com/
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 https://cyberpublicschool:com/
Try with a simple app.js file with this content:
consote.tog(process.env.NAME)
consote.tog(process.env.PATH)
undefined
undefined
ftavio
undefined
env
131 https://cyberpublicschool:com/
it will return a list of the environment variables set, for
example:
HOME=/Users/ftavio
LOGNAME=ftavio
PATH=/usr/tocat/bin:/usr/bin:/bin:/usr/sbin:/sbin:/
PWD=/Users/ftavio
SHELL=/usr/tocat/bin/fish
132 https://cyberpublicschool:com/
printenv
A quick guide to the printenv command, used to
print the values of environment variables
HOME=/Users/ftavio
LOGNAME=ftavio
PATH=/usr/tocat/bin:/usr/bin:/bin:/usr/sbin:/sbin:/
PWD=/Users/ftavio
SHELL=/usr/tocat/bin/fish
printenv PATH
133 https://cyberpublicschool:com/
Conclusion
Thanks a lot for reading this book.
134 https://cyberpublicschool:com/
Contact us.
https://lnkd.in/dUYHqXmT
https://cyberpublicschool.com/
https://www.instagram.com/cyberpublicschool
https://www.youtube.com/@cyberpublicschool.3575
Email:anand@cyberpublicschool.com
Phone no: - +91 9631750498 (IND)
+91 7304708634 (IND)
135 https://cyberpublicschool:com/