ZetCode

Basic Linux commands

last modified July 24, 2026

This article 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 directory.

Throughout the article, we work with two files: words.txt containing a list of English words, and thermopylae.txt containing a short historical text.

$ cat words.txt
sky
blue
falcon
rock
wood
forest
book
small
tension
war
water
warm
cup
cloud
snow
bear
pen
purple
bow
rock
falcon
owl
bear
wolf
fox
storm
$ 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.

Shell basics

Before we dive in, here are a few essential shell features that will make your terminal experience smoother.

Tab completion — press the Tab key to autocomplete commands, file names, and directory paths. For instance, type pwd and press Tab to see it completed. This saves time and prevents typos.

Ctrl+C — interrupts and cancels a running command. Use it when a command is stuck or taking too long.

Ctrl+D — sends an end-of-file signal. It exits the current shell or closes input to a command like cat.

Pipes — the vertical bar | sends the output of one command as input to another. For example, ls | head lists files but shows only the first few lines. Pipes are used extensively throughout this tutorial.

Redirection> redirects a command's output to a file (overwriting it), while >> appends to an existing file. For instance, echo "hello" > file.txt writes hello to file.txt.

Table of contents

System information & navigation

Commands to find out who you are, where you are, and what is happening on the system.

pwd command

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

$ pwd
/home/jano/Documents/prog/linux/basic-commands

cd command

The cd command changes the current working directory. Without arguments it changes to the home directory. The .. argument changes to the parent directory.

$ cd /tmp
$ pwd
/tmp
$ cd
$ pwd
/home/jano
$ cd -
/tmp

We change to /tmp, go back home, and return to /tmp with the dash argument.

clear command

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

$ clear

whoami command

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

$ whoami
jano

hostname command

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

$ hostname
andromeda

date command

The date command prints the current local date.

$ date
Fri Jul 24 12:46:30 CEST 2026

The command prints the current local datetime. The output language and date format depend on the system locale, controlled by the $LANG environment variable.

$ date -u
Fri Jul 24 10:48:20 UTC 2026

This command prints the current datetime in UTC.

$ date +%Y
2026
$ date +%F
2026-07-24

We print the date in the specified format.

$ date +%s
1784898431

This is the Unix time--the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970.

uptime command

$ 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 command

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

$ man date

cal command

The cal command gives us the calendar.

$ cal
     July 2026
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
$ cal -3
     June 2026             July 2026            August 2026
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  6            1  2  3  4                     1
 7  8  9 10 11 12 13   5  6  7  8  9 10 11   2  3  4  5  6  7  8
14 15 16 17 18 19 20  12 13 14 15 16 17 18   9 10 11 12 13 14 15
21 22 23 24 25 26 27  19 20 21 22 23 24 25  16 17 18 19 20 21 22
28 29 30              26 27 28 29 30 31     23 24 25 26 27 28 29
                                            30 31

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

arch command

The arch command gives the machine architecture.

$ arch
x86_64
$ 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 command

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

$ uname
Linux
$ uname -a
Linux universum 6.8.0-134-generic #134~22.04.1-Ubuntu SMP ...

who command

The who command lists users who are currently logged in.

$ who
jano     tty7         2026-07-24 10:02 (:0)

We have one logged user.

w command

$ w
12:56:24 up  2:55,  1 user,  load average: 0.54, 0.50, 0.46
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
jano     tty7     :0               10:04    2:55m  4:41   0.18s xfce4-session

The w command shows who is logged in and what the users are doing. Note that the decimal separator in the load average (0.54, 0.50) is a period in the English locale. Users with different locales may see a comma (0,54, 0,50) instead.

ps command

The ps command displays information about running processes.

$ ps
    PID TTY          TIME CMD
  10166 pts/0    00:00:00 bash
  10321 pts/0    00:00:00 ps

The output lists the process ID, the terminal, the cumulated CPU time, and the command name for each process owned by the current user.

$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
...

With the aux options, we list all processes on the system with extended information.

history command

The history shows the history of the user commands typed.

$ history
...
2017  man date
2018  date %F
2019  man date
2020  date -u
2021  man date
2022  date +%Y
2023  date +%F
2024  date +%s
2025  cal
2026  cal -3
2027  uname -a
2028  who
2029  w
2030  ping webcode.me
2031  df -h
2032  ping
2033  man ping
2034  ping -c 4 webcode.me
2035  man ping
2036  history
...

We show a partial list of executed commands.

Text output & processing

Commands for printing text to the terminal and manipulating text content.

echo command

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

$ echo an old falcon
an old falcon

We print a small message.

Intermediate: The following examples use the sed and awk commands, which are powerful text-processing tools covered in more depth in separate tutorials.
$ 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
en_US.UTF-8 /home/jano/Documents/prog/linux/basic-commands
$ echo $SHELL $RANDOM $HOSTNAME
/bin/bash 15604 andromeda

Here we print a couple of environment variables.

printf command

With the printf command, we can output formatted strings.

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

We build a message with printf.

sort command

The sort command sorts lines of text files.

$ 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.

type command

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

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

We check echo and who commands.

grep command

The grep command searches for patterns in text files. It prints lines that match the given pattern.

$ grep wood words.txt
wood

We search for the wood string in the words.txt file.

$ grep -i 'the' 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.

With the -i option, we perform a case-insensitive search.

File operations

Commands to create, list, copy, move, remove, and organize files and directories.

ls command

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

$ ls -l
total 8
-rw-rw-r-- 1 jano jano 225 Jul 24 15:23 thermopylae.txt
-rw-rw-r-- 1 jano jano 138 Jul 24 15:23 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.

$ ls -a
.  ..  docs  thermopylae.txt  words.txt

The -a option shows all entries, including hidden files starting with a dot (such as . for the current directory and .. for the parent directory).

cp command

The cp command is used to copy files and directories.

$ cp thermopylae.txt thermopylae2.txt

With the cp command, we create a copy of the thermopylae.txt file. The new file is called thermopylae2.txt.

$ 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.

mv command

The mv command moves or renames files and directories.

$ mv thermopylae2.txt docs/

The mv command moves the given file to the directory.

Note: Some commands may not be 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.

rm command

The rm command removes files or directories.

$ 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 cautiously.

$ ls

The directory is empty.

In this article we have presented basic Linux shell commands.

mkdir command

The mkdir command creates a new directory.

$ mkdir docs

We create a new directory called docs.

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

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

touch command

The touch command updates the access and modification times of each file to the current time. If the file does not exist, it is created.

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

With touch, we create an empty file called vals.txt. 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 evaluates backslash escapes.

$ ls -l vals.txt 
-rw-rw-r-- 1 jano jano 24 jan  7 13:24 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.

tree command

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

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

1 directory, 3 files

We can see the files and directories that we have created so far.

chmod command

The chmod command changes file permissions. In Linux, each file has three permission sets: owner, group, and others. Permissions can be expressed symbolically or as an octal number.

$ ./script.sh
bash: ./script.sh: Permission denied

If we try to run a script, we get a Permission denied error because the execute bit is not set.

$ chmod +x script.sh
$ ./script.sh
Hello from script

With chmod +x, we add the execute permission for everyone. The script now runs.

$ chmod 755 script.sh
$ ls -l script.sh
-rwxr-xr-x 1 jano jano 42 jan  7 14:00 script.sh

The octal notation 755 sets read, write, and execute for the owner (7), and read and execute for the group and others (55). This is a common permission setting for executable files.

Viewing & comparing files

Commands to display file contents, count words and lines, and find differences between files.

cat command

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

$ 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.

We show the contents of the thermopylae.txt file.

less command

The less command is a pager used to view text files one screen at a time. Use the arrow keys to scroll and q to quit.

$ less thermopylae.txt

The head command prints the first n lines (defaults to ten) of each file to standard output.

$ head words.txt
war
sky
blue
fork
portal
cup
blue
cup
word
work

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

$ head -n 3 words.txt 
war
sky
blue

With the -n option, we can specify the number of lines to display.

$ head -n 3 words.txt thermopylae.txt 
==> words.txt <==
war
sky
blue

==> 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.

We can specify multiple files.

tail command

The tail command prints the last n lines (defaults to ten) of each file to standard output.

$ tail words.txt
blue
cup
word
work
war
war
water
pencil
pen
war

The tail command prints the last ten lines.

$ head -4 words.txt
war
sky
blue
fork
$ tail -3 words.txt
pencil
pen
war

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

wc command

The wc command counts the number of lines, words, and bytes in the file.

$ wc thermopylae.txt
3  38 225 thermopylae.txt

The thermopylae.txt file has 3 lines, 38 words, and 225 bytes.

$ wc -l thermopylae.txt
3 thermopylae.txt
$ wc -w thermopylae.txt
38 thermopylae.txt
$ wc -c thermopylae.txt
225 thermopylae.txt

We can get the information separately with the corresponding options.

$ wc thermopylae.txt
  3  38 225 thermopylae.txt

Here we get the number of lines, words, and bytes in one step.

diff command

The diff command compares the two text files line by line.

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

The output tells that the 4th line from the second file must be appended to the 3rd 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).

Networking

Commands to transfer data over the network and check network connectivity.

ping command

The ping command is used to check the network connectivity.

$ 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 continuously sends requests until we terminate it with Ctrl + C.

$ ping -c 4 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=32.1 ms
64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=2 ttl=54 time=34.6 ms
64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=3 ttl=54 time=33.4 ms
64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=4 ttl=54 time=37.9 ms

--- webcode.me ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 32.100/34.497/37.876/2.145 ms

With the -c option, we specify the amount of requests sent.

curl command

The curl command can be used to generate HTTP requests.

$ curl example.com
<!doctype html><html lang="en"><head><title>Example Domain</title>...

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

$ curl -I example.com
HTTP/1.1 200 OK
Date: Fri, 24 Jul 2026 13:13:47 GMT
Content-Type: text/html
Connection: keep-alive
Server: cloudflare
Last-Modified: Mon, 20 Jul 2026 07:16:20 GMT
Allow: GET, HEAD
Accept-Ranges: bytes
Age: 12101
cf-cache-status: HIT
CF-RAY: a2032f294fc8a3c7-BTS

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

wget command

The wget is a non-interactive network retriever.

$ wget -q https://raw.githubusercontent.com/janbodnar/Python-Skolenie/refs/heads/master/data/unix-words.txt
$ wget -q https://raw.githubusercontent.com/janbodnar/Python-Skolenie/refs/heads/master/data/thermopylae.txt

With the wget command, we download two text files from a GitHub repository. The -q (quiet) option suppresses the output of the command.

$ ls
thermopylae.txt  unix-words.txt

Disk & packages

Commands to check disk usage and manage software packages.

df command

The df command reports the file system disk space usage.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           1,6G  3,5M  1,6G   1% /run
/dev/nvme0n1p2  468G  194G  251G  44% /
tmpfs           7,8G   14M  7,7G   1% /dev/shm
tmpfs           5,0M  4,0K  5,0M   1% /run/lock
tmpfs           7,8G     0  7,8G   0% /run/qemu
/dev/nvme0n1p1  511M  5,3M  506M   2% /boot/efi
tmpfs           1,6G  140K  1,6G   1% /run/user/1000

The -h option shows the size in human-readable format, that is in powers of 1024.

apt command

The apt provides a high-level commandline interface for the package management system.

$ 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.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Linux tutorials.