ZetCode

Basic Linux commands

last modified July 26, 2021

Basic Linux commands tutorial is an introduction to basic Linux commands on the shell. The tutorial presents a variety of basic Linux commands.

At the beginning, we assume an empty home directory.

$ pwd
/home/janbodnar

We start in our home directory. The pwd command prints the current working directory of the user.

$ clear

The clear command clears the terminal screen. Instead of this command, we can use the Ctrl + L shortcut.

$ whoami
janbodnar

The whoami command prints the user name. This command is useful when administrators are logged into multiple boxes.

$ hostname
debian

The hostname gives the name of the host. The hostname is the name of the machine on then network.

$ date
Mon 26 Jul 2021 04:39:05 AM CDT

The date command prints the current local date.

$ uptime
04:39:18 up 45 min,  1 user,  load average: 0.00, 0.01, 0.00

The uptime command tells how long the system is up.

$ man date

The man command gives us the manual for the command. There we can find all the options for the command.

$ cal
    July 2021
Su Mo Tu We Th Fr Sa
            1  2  3
4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

The cal command gives us the calendar.

$ cal -3
    June 2021             July 2021            August 2021
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
      1  2  3  4  5               1  2  3   1  2  3  4  5  6  7
6  7  8  9 10 11 12   4  5  6  7  8  9 10   8  9 10 11 12 13 14
13 14 15 16 17 18 19  11 12 13 14 15 16 17  15 16 17 18 19 20 21
20 21 22 23 24 25 26  18 19 20 21 22 23 24  22 23 24 25 26 27 28
27 28 29 30           25 26 27 28 29 30 31  29 30 31

With the -3 option, we get three months: the current, previous, and next.

$ arch
x86_64

The arch command gives the machine architecture.

$ arch --help
Usage: arch [OPTION]...
Print machine architecture.

        --help     display this help and exit
        --version  output version information and exit
...

Most commands have the --help option which gives a short help information about a command.

$ uname
Linux
$ uname -a
Linux debian 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64 GNU/Linux

The uname command gives system information. The -a option lists all information.

$ who
janbodnar pts/0        2021-07-26 04:34 (192.168.0.20)

The who command lists users who are currently logged in.

$ w
04:40:09 up 46 min,  1 user,  load average: 0.00, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
janbodna pts/0    192.168.0.20     04:34    0.00s  0.08s  0.00s w

The w command shows who is logeed in and what the users are doing.

$ echo an old falcon
an old falcon

The echo command prints a line of text to the terminal.

$ echo "an old falcon" | sed 's/falcon/wolf/'
an old wolf

The sed command is used to transform text. We change the falcon word to wolf in the text string.

$ echo $LANG $PWD $SHELL $RANDOM $HOSTNAME
en_US.UTF-8 /home/janbodnar /bin/bash 23575 debian

Here we print a couple of environment variables.

$ printf "%s is %d years old\n" Jane 17
Jane is 17 years old

With the printf command, we can output formatted strings.

$ ping webcode.me
PING webcode.me (46.101.248.126) 56(84) bytes of data.
64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=1 ttl=54 time=29.2 ms
64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=2 ttl=54 time=27.9 ms
...

The ping command is used to check the network connectivity.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           383M   11M  373M   3% /run
/dev/sda1       292G  2.0G  275G   1% /
tmpfs           1.9G   12K  1.9G   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           383M     0  383M   0% /run/user/0
tmpfs           383M     0  383M   0% /run/user/1000

The df command reports the file system disk space usage. The -h option shows the size in human-readable format, that is in in powers of 1024.

$ type echo who
echo is a shell builtin
who is hashed (/usr/bin/who)

There are two kinds of commands: shell builtins and external commands. The type command can be used to gives the command type.

$ history
1  ls
2  who
...
13  ls
14  date
15  ls -a
16  ping webcode.me
17  uptime
18  ls
19  who
20  whoami
..

The history gives the history of the user commands typed.

The curl command can be used to generate HTTP requests.

$ curl webcode.me
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My html page</title>
</head>
<body>

    <p>
        Today is a beautiful day. We go swimming and fishing.
    </p>

    <p>
         Hello there. How are you?
    </p>

</body>
</html>

We generate a GET request to a web page; we get a HTML document printed to the console.

$ curl -I webcode.me
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 26 Jul 2021 09:49:36 GMT
Content-Type: text/html
Content-Length: 348
Last-Modified: Sat, 20 Jul 2019 11:49:25 GMT
Connection: keep-alive
ETag: "5d32ffc5-15c"
Accept-Ranges: bytes

With the -I option, we generate a HEAD request, which returns the header of the document containing some metadata.

$ wget -q webcode.me/words.txt
$ wget -q webcode.me/thermopylae.txt

With the wget command, we dowload two small text files. The -q (quiet) option supresses the output of the command.

$ ls
thermopylae.txt  words.txt

The ls command lists the contents of the current directory; we have two files.

$ ls -l
total 8
-rw-r--r-- 1 janbodnar janbodnar 226 Jul 26 04:37 thermopylae.txt
-rw-r--r-- 1 janbodnar janbodnar 140 Jul 26 04:43 words.txt

With the -l we get a long listing format. We get additional information including file permissions, ownership, size, and date of last modification.

$ wc thermopylae.txt
4  38 226 thermopylae.txt

The wc command counts the number of lines, words, and bytes in the file. The thermopylae.txt file has 4 lines, 38 words, and 226 bytes.

$ wc -l thermopylae.txt
4 thermopylae.txt
$ wc -w thermopylae.txt
38 thermopylae.txt
$ wc -c thermopylae.txt
226 thermopylae.txt

We can get the information separately with the corresponding options.

$ cat thermopylae.txt
The Battle of Thermopylae was fought between an alliance of Greek city-states,
led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the
course of three days, during the second Persian invasion of Greece.

The cat command concatenates files and prints them on the console. The command is often used to display the contents of small files.

$ cp thermopylae.txt thermopylae2.txt

With the cp command, we create a copy of the file.

$ echo "The battle took place simultaneously with the naval battle at Artemisium." >> thermopylae2.txt

With the echo command and the >> operator, we append a new line to the thermopylae2.txt file.

$ cat thermopylae2.txt
The Battle of Thermopylae was fought between an alliance of Greek city-states,
led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the
course of three days, during the second Persian invasion of Greece.

The battle took place simultaneously with the naval battle at Artemisium.

We check the contents of the file with cat; it contains the new line.

$ diff thermopylae.txt thermopylae2.txt
4a5
> The battle took place simultaneously with the naval battle at Artemisium.

The diff command compares the two text files line by line. The output tells that the 5th line from the second file must be appended to the 4th line to make the files identical. Lines preceded by a < are lines from the first file. Lines preceded by > are lines from the second file. The special characters are a (add), d (delete), and c (change).

$ head words.txt
sky
blue
falcon
rock
wood
forest
book
small
tension
war

The head command prints the first ten lines of a file.

$ tail words.txt
pen
purple
bow
rock
falcon
owl
bear
wolf
fox
storm

The tail command prints the last ten lines.

$ head -4 words.txt
sky
blue
falcon
rock
$ tail -3 words.txt
wolf
fox
storm

We can specify how many first/last lines to output.

$ mkdir docs

The mkdir command creates a new directory.

$ ls -F
docs/  thermopylae2.txt  thermopylae.txt  words.txt

The -F option appends indicators to the files; the / tells that docs is a directory.

$ mv thermopylae2.txt docs/

The mv command moves the given file to the directory.

Some commands may not installed on our machine by default. For instance on Debian Linux, we need to install the tree command explicitly via the apt command.

$ ls docs/
thermopylae2.txt

We list the contents of the docs directory; it contains one file.

Some commands may not installed on our machine by default. For instance on Debian Linux, we need to install the tree command explicitly via the apt command.

$ sudo apt install tree

The apt is a Debian-specific package manager. Here we also assume that the sudo command is installed on the system and the user can run sudo command.

$ tree
.
├── docs
│   └── thermopylae2.txt
├── thermopylae.txt
└── words.txt

1 directory, 3 files

The tree command lists the contents of the current directory in a tree-like format.

$ touch vals.txt
$ ls -s vals.txt 
0 vals.txt

With touch, we create an empty file. The ls -s command prints the size of the file. We can see that it is empty.

$ echo -e "8\n9\n11\n12\n7\n6\n3\n2\n4\n1\n5" >> vals.txt
$ cat vals.txt 
8
9
11
12
7
6
3
2
4
1
5

We insert 11 numbers into the vals.txt file with echo. The -e option of echo evalueates backslash escapes.

$ ls -l vals.txt 
-rw-r--r-- 1 janbodnar janbodnar 24 Jul 26 07:14 vals.txt

Now the file has 24 bytes.

$ cat -E vals.txt 
8$
9$
11$
12$
7$
6$
3$
2$
4$
1$
5$

There are 13 characters in the file and 11 newline characters; 13 and 11 is 24. The -E option of cat prints the $ at newline characters.

$ ls -l vals.txt | awk '{print $5}'
24

We can use the awk command to print only the size information; we output the fifth column of the row.

$ sort words.txt | head
bear
blue
book
bow
cloud
cup
falcon
falcon
forest
fox

The sort command sorts the lines of text files. In our example, we sort the words and print the first ten lines.

$ sort -r words.txt | head
wood
wolf
water
warm
war
tension
storm
snow
small
sky

With the -r option, we sort in reverse order.

$ sort -n vals.txt 
1
2
3
4
5
6
7
8
9
11
12

With the -n option, we sort the values numerically.

$ awk '{ print $0, "has", length($0), "chars"}' words.txt 
sky has 3 chars
blue has 4 chars
falcon has 6 chars
rock has 4 chars
wood has 4 chars
forest has 6 chars
book has 4 chars
small has 5 chars
tension has 7 chars
war has 3 chars
water has 5 chars
warm has 4 chars
cup has 3 chars
...

The awk is a very complex command. Here we print the length of each of the words in the words.txt file. The $0 is the entire input record, one line from the file. The length function gives the size of the given record.

$ ls
docs  thermopylae.txt  vals.txt  words.txt

At this moment, we have these files.

$ rm *.txt

We remove all text files with the rm command.

$ rm docs/thermopylae2.txt 
$ rmdir docs/

We remove a directory with rmdir command. The directory must be empty before being deleted. We can also use the rm -rf command to delete a directory in one go. Note that this command is dangerous and should be used cautiosly.

$ ls 

The directory is empty.


In this tutorial we have presented basic Linux shell commands.