ZetCode

Basic Linux commands

last modified October 23, 2026

This article is an introduction to basic Linux commands on the shell. It presents the commands that are used every day for finding information about the system, working with text and files, and accessing the network. Each section links to a detailed tutorial where one exists.

We work in a directory called basic-commands in our home directory, which contains two sample files: words.txt with a list of English words and thermopylae.txt with a short historical text.

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

Most examples were run in a Debian container. The uptime, cal, who, w, ps, and df examples were run on an Ubuntu desktop, since they need a real login session and disks.

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

The pwd command prints the current working directory.

pwd

We are in the basic-commands directory inside the home directory /home/jano.

$ pwd
/home/jano/basic-commands

cd command

The cd command changes the current working directory.

cd /tmp
cd
cd -

We change to /tmp. Without arguments, cd goes to the home directory, and the dash argument returns to the previous directory and prints its name. See the cd command tutorial for more.

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

clear command

The clear command clears the terminal screen.

clear

The command produces no output; it moves the prompt to the top of an empty screen. Instead of the command, we can press Ctrl + L.

whoami command

The whoami command prints the name of the current user.

whoami

This is useful when switching between accounts with su or sudo, or when administrators are logged into multiple machines.

$ whoami
jano

hostname command

The hostname command prints the name of the machine.

hostname

The host name identifies the machine on the network. Our container is called box. The hostname -I option prints the IP addresses of the host.

$ hostname
box

date command

The date command prints the current date and time.

date
date -u
date +%F
date +%s

The first command prints the local date and time; its format depends on the locale set in the LANG variable. The -u option prints the time in UTC. An argument starting with + is a format: %F is the ISO date, and %s is Unix time, the number of seconds since 00:00:00 UTC on 1 January 1970. See the date command tutorial for more.

$ date
Fri Oct 23 12:57:16 PM CEST 2026
$ date -u
Fri Oct 23 10:57:16 AM UTC 2026
$ date +%F
2026-10-23
$ date +%s
1792753036

uptime command

The uptime command tells how long the system has been running.

uptime
uptime -p

The output contains the current time, the uptime, the number of logged-in users, and the load averages for the last 1, 5, and 15 minutes. The -p option prints the uptime in a readable form.

$ uptime
 12:57:21 up  2:49,  1 user,  load average: 0.67, 0.85, 0.71
$ uptime -p
up 2 hours, 49 minutes

man command

The man command displays the manual page of a command, which describes all its options.

man date

The manual opens in a pager: we scroll with the arrow keys or Space, search with /, and quit with q. The man -k command searches the manual descriptions for a keyword. Most commands also print a short summary with the --help option.

cal command

The cal command displays a calendar.

cal
cal -3

Without options, it shows the current month. With -3, we get the previous, current, and next month. See the cal command tutorial for more.

$ cal
    October 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
   September 2026         October 2026         November 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               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

arch command

The arch command prints the machine architecture.

arch
arch --help

x86_64 is the 64-bit Intel and AMD architecture; ARM machines report aarch64. Like most commands, arch has the --help option, which prints a short usage summary.

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

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

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/arch>
or available locally via: info '(coreutils) arch invocation'

uname command

The uname command prints system information.

uname
uname -r
uname -a

Without options, it prints the kernel name. The -r option shows the kernel release, and -a prints all information, including the host name, the kernel version, and the architecture. A container shares the kernel of its host, which is an Ubuntu kernel here.

$ uname
Linux
$ uname -r
6.8.0-138-generic
$ uname -a
Linux box 6.8.0-138-generic #138~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug  7 13:43:15 UTC  x86_64 GNU/Linux

who command

The who command lists the users who are currently logged in.

who

One user is logged in on the graphical session tty7 since 10:09. Remote SSH logins appear with a pts terminal and the address they come from.

$ who
jano     tty7         2026-10-23 10:09 (:0)

w command

The w command shows who is logged in and what they are doing.

w

The header line is the same as the output of uptime. For each user, w shows the terminal, login time, idle time, CPU time, and the current program. In locales such as Slovak or German, the load averages use a comma as the decimal separator.

$ w
 12:57:21 up  2:49,  1 user,  load average: 0.67, 0.85, 0.71
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
jano     tty7     :0               10:09    2:49m  1:36   0.16s xfce4-session

ps command

The ps command displays information about running processes.

ps
ps aux | head -n 5

Without options, ps lists the processes of the current terminal: the shell and ps itself, with the process ID, the terminal, the CPU time, and the command name. With aux, it lists all processes on the system with the user, CPU and memory usage, and the full command line. See the ps command tutorial for more.

$ ps
    PID TTY          TIME CMD
  32713 pts/3    00:00:00 bash
  32714 pts/3    00:00:00 ps
$ ps aux | head -n 5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 168212 13820 ?        Ss   10:07   0:03 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    10:07   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        S    10:07   0:00 [pool_workqueue_release]
root           4  0.0  0.0      0     0 ?        I<   10:07   0:00 [kworker/R-rcu_g]

history command

The history command shows the commands typed in the shell.

history 5

With a number, only the last entries are shown. A command can be run again with ! and its number, for example !3, and Ctrl + R searches the history interactively. Bash saves the history in ~/.bash_history.

$ history 5
    2  cal -3
    3  uname -a
    4  df -h
    5  ping -c 3 example.com
    6  history 5

Text output & processing

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

echo command

The echo command prints a line of text.

echo an old falcon
echo "an old falcon" | sed "s/falcon/wolf/"
echo $LANG $PWD
echo $SHELL $HOME

The second command sends the text through a pipe to sed, which replaces a word. echo is also used to show the values of variables. See the echo command tutorial for more.

$ echo an old falcon
an old falcon
$ echo "an old falcon" | sed "s/falcon/wolf/"
an old wolf
$ echo $LANG $PWD
en_US.UTF-8 /home/jano/basic-commands
$ echo $SHELL $HOME
/bin/bash /home/jano

printf command

The printf command prints formatted output.

printf "%s is %d years old\n" Jane 17
printf "%-8s|%6.2f|\n" apple 1.5 orange 12

The format string contains specifiers such as %s for strings, %d for integers, and %f for floating-point numbers. Unlike echo, printf does not add a newline, so we write \n. If there are more arguments than specifiers, the format is reused, which makes simple tables easy.

$ printf "%s is %d years old\n" Jane 17
Jane is 17 years old
$ printf "%-8s|%6.2f|\n" apple 1.5 orange 12
apple   |  1.50|
orange  | 12.00|

sort command

The sort command sorts lines of text.

sort words.txt | head -n 5
sort -r words.txt | head -n 5
sort -u words.txt | wc -l

We print the first five words in alphabetical and reverse order. The -u option removes duplicates; the file has 23 distinct words. See the sort command tutorial for more.

$ sort words.txt | head -n 5
bear
bear
blue
book
bow
$ sort -r words.txt | head -n 5
wood
wolf
water
warm
war
$ sort -u words.txt | wc -l
23

type command

The type command tells how the shell interprets a name.

type echo cd ls

echo and cd are built into the shell, while ls is a program in /usr/bin. In an interactive shell, ls is often an alias, which type reports as well.

$ type echo cd ls
echo is a shell builtin
cd is a shell builtin
ls is /usr/bin/ls

grep command

The grep command prints lines that match a pattern.

grep wood words.txt
grep -i "the" thermopylae.txt
grep -c "^b" words.txt
grep -n "fox" words.txt

The -i option ignores case, so The matches as well. -c counts the matching lines; the pattern ^b matches words starting with b. -n prints line numbers. See the grep command tutorial for more.

$ grep wood words.txt
wood
$ 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.
$ grep -c "^b" words.txt
5
$ grep -n "fox" words.txt
25:fox

File operations

Commands for listing, copying, moving, creating, and deleting files and directories.

ls command

The ls command lists directory contents.

ls
ls -l
ls -a
ls -lh /usr/bin/bash

The -l option shows a long listing with permissions, owner, size, and modification time. -a includes hidden entries, whose names start with a dot, such as . (the current directory) and .. (the parent). -h prints sizes with units. When the output goes to a terminal, ls arranges names in columns. See the ls command tutorial for more.

$ ls
thermopylae.txt
words.txt
$ ls -l
total 8
-rw-rw-r-- 1 jano jano 223 Oct 23 12:56 thermopylae.txt
-rw-rw-r-- 1 jano jano 138 Oct 23 12:56 words.txt
$ ls -a
.
..
thermopylae.txt
words.txt
$ ls -lh /usr/bin/bash
-rwxr-xr-x 1 root root 1.3M Jun  6  2025 /usr/bin/bash

cp command

The cp command copies files and directories.

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

We copy the file and append a line to the copy with the >> operator. See the cp command tutorial for more.

$ cp thermopylae.txt thermopylae2.txt
$ echo "The battle took place simultaneously with the naval battle at Artemisium." >> thermopylae2.txt
$ 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.

mv command

The mv command moves and renames files.

mkdir docs
mv thermopylae2.txt docs/
mv docs/thermopylae2.txt docs/battle.txt

We move the file into a new directory and then rename it. See the mv command tutorial for more.

$ mkdir docs
$ mv thermopylae2.txt docs/
$ ls docs
thermopylae2.txt
$ mv docs/thermopylae2.txt docs/battle.txt
$ ls docs
battle.txt

rm command

The rm command removes files, and rmdir removes empty directories.

rm docs/battle.txt
rmdir docs

Deleted files do not go to a trash bin, so rm must be used with care. A directory with contents is removed with rm -r. See the rm command tutorial for more.

$ rm docs/battle.txt
$ rmdir docs
$ ls
thermopylae.txt
words.txt

mkdir command

The mkdir command creates directories.

mkdir docs
mkdir -p projects/web/css
ls -F

The -p option creates missing parent directories. The -F option of ls marks directories with a slash. See the mkdir command tutorial for more.

$ mkdir docs
$ mkdir -p projects/web/css
$ ls -F
docs/
projects/
thermopylae.txt
words.txt

touch command

The touch command creates empty files and changes file timestamps.

touch vals.txt
ls -s vals.txt
printf "%s\n" 8 9 11 12 7 6 3 2 4 1 5 > vals.txt
sort -n vals.txt | tr "\n" " "; echo
touch -d "2026-01-01 10:00" vals.txt
ls -l vals.txt

A new file created by touch occupies no blocks. We write numbers to the file and sort them numerically. With -d, touch sets the modification time to the given date; without it, it sets the current time.

$ touch vals.txt
$ ls -s vals.txt
0 vals.txt
$ printf "%s\n" 8 9 11 12 7 6 3 2 4 1 5 > vals.txt
$ sort -n vals.txt | tr "\n" " "; echo
1 2 3 4 5 6 7 8 9 11 12
$ touch -d "2026-01-01 10:00" vals.txt
$ ls -l vals.txt
-rw-r--r-- 1 jano jano 24 Jan  1  2026 vals.txt

tree command

The tree command displays the contents of a directory as a tree.

tree

The command may need to be installed first, for example with sudo apt install tree. The -d option shows only directories and -L limits the depth.

$ tree
.
├── docs
├── projects
│   └── web
│       └── css
├── thermopylae.txt
├── vals.txt
└── words.txt

5 directories, 3 files

chmod command

The chmod command changes file permissions.

echo 'echo "Hello from script"' > script.sh
./script.sh
chmod +x script.sh
./script.sh
ls -l script.sh

A new file is not executable, so the script cannot be run. After chmod +x adds the execute permission, it works. See the chmod command and file permissions tutorials for more.

$ echo 'echo "Hello from script"' > script.sh
$ ./script.sh
bash: ./script.sh: Permission denied
$ chmod +x script.sh
$ ./script.sh
Hello from script
$ ls -l script.sh
-rwxr-xr-x 1 jano jano 25 Oct 23 12:57 script.sh

Viewing & comparing files

Commands for displaying file contents and comparing files.

cat command

The cat command prints the contents of files.

cat thermopylae.txt
cat -n thermopylae.txt

The -n option numbers the lines. See the cat command tutorial for more.

$ 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.
$ cat -n thermopylae.txt
     1	The Battle of Thermopylae was fought between an alliance of Greek city-states,
     2	led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the
     3	course of three days, during the second Persian invasion of Greece.

less command

The less command displays a file one screen at a time.

less thermopylae.txt

less is interactive: we scroll with the arrow keys, Space, and b, search with /, jump to the end with G, and quit with q. It is the right tool for long files and is also used by man.

The head command prints the first lines of files.

head words.txt
head -n 3 words.txt
head -n 2 words.txt thermopylae.txt

By default, head prints ten lines; -n sets the number. With several files, each part gets a header with the file name. See the head and tail commands tutorial.

$ head words.txt
sky
blue
falcon
rock
wood
forest
book
small
tension
war
$ head -n 3 words.txt
sky
blue
falcon
$ head -n 2 words.txt thermopylae.txt
==> words.txt <==
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

tail command

The tail command prints the last lines of files.

tail words.txt
tail -n 3 words.txt

Like head, it prints ten lines by default. The tail -f command keeps the file open and prints new lines as they are added, which is used to watch log files.

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

wc command

The wc command counts lines, words, and bytes.

wc thermopylae.txt
wc -l thermopylae.txt
wc -w thermopylae.txt
wc -c thermopylae.txt

Without options, all three counts are printed; the options select one of them. See the wc command tutorial for more.

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

diff command

The diff command compares files line by line.

cp thermopylae.txt thermopylae2.txt
sed -i 's/three days/three long days/' thermopylae2.txt
echo "The battle took place simultaneously with the naval battle at Artemisium." >> thermopylae2.txt
diff thermopylae.txt thermopylae2.txt
diff -u thermopylae.txt thermopylae2.txt

We change a word in the third line of a copy and append a line. In the default output, 3c3,4 means that line 3 of the first file was changed into lines 3 and 4 of the second; lines starting with < come from the first file and those with > from the second. The -u option produces the unified format used by Git and patches, with removed lines marked - and added lines +.

$ cp thermopylae.txt thermopylae2.txt
$ sed -i 's/three days/three long days/' thermopylae2.txt
$ echo "The battle took place simultaneously with the naval battle at Artemisium." >> thermopylae2.txt
$ diff thermopylae.txt thermopylae2.txt
3c3,4
< course of three days, during the second Persian invasion of Greece.
---
> course of three long days, during the second Persian invasion of Greece.
> The battle took place simultaneously with the naval battle at Artemisium.
$ diff -u thermopylae.txt thermopylae2.txt
--- thermopylae.txt	2026-10-23 12:56:46.188960220 +0200
+++ thermopylae2.txt	2026-10-23 12:57:16.217729130 +0200
@@ -1,3 +1,4 @@
 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.
+course of three long days, during the second Persian invasion of Greece.
+The battle took place simultaneously with the naval battle at Artemisium.

Networking

Commands for testing connectivity and transferring data over the network.

ping command

The ping command checks whether a host is reachable.

ping -c 3 example.com

ping sends ICMP echo requests and prints the round-trip time of each reply. Without -c, it runs until we press Ctrl + C. The summary shows the packet loss and the minimum, average, and maximum times. Some hosts and firewalls block ICMP, so a failed ping does not always mean the host is down.

$ ping -c 3 example.com
PING example.com (104.20.23.154) 56(84) bytes of data.
64 bytes from 104.20.23.154 (104.20.23.154): icmp_seq=1 ttl=58 time=2.25 ms
64 bytes from 104.20.23.154 (104.20.23.154): icmp_seq=2 ttl=58 time=4.19 ms
64 bytes from 104.20.23.154 (104.20.23.154): icmp_seq=3 ttl=58 time=4.11 ms

--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 2.248/3.513/4.187/0.895 ms

curl command

The curl command transfers data from or to a server.

curl -s https://example.com | grep -o "<title>.*</title>"
curl -sI https://example.com | head -n 3

The first command downloads the page and extracts its title. The -I option fetches only the response headers. The -s option hides the progress meter. See the curl command tutorial for more.

$ curl -s https://example.com | grep -o "<title>.*</title>"
<title>Example Domain</title>
$ curl -sI https://example.com | head -n 3
HTTP/2 200
date: Fri, 23 Oct 2026 10:57:18 GMT
content-type: text/html; charset=utf-8

wget command

The wget command downloads files from the web.

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

We download two data files into the data directory, which is set with -P. The -q option suppresses the progress output. See the wget command tutorial for more.

$ mkdir data
$ wget -q -P data https://raw.githubusercontent.com/janbodnar/Python-Skolenie/refs/heads/master/data/unix-words.txt
$ wget -q -P data https://raw.githubusercontent.com/janbodnar/Python-Skolenie/refs/heads/master/data/thermopylae.txt
$ ls -lh data
total 2.4M
-rw-r--r-- 1 jano jano  225 Oct 23 12:57 thermopylae.txt
-rw-r--r-- 1 jano jano 2.4M Oct 23 12:57 unix-words.txt

Disk & packages

Commands for checking disk space and managing software.

df command

The df command reports the disk space usage of file systems.

df -h
df -h -x tmpfs -x efivarfs

The -h option prints sizes with units. The tmpfs entries are file systems in memory; excluding them with -x leaves the real disk partitions. See the df command tutorial for more.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           1.6G  2.1M  1.6G   1% /run
/dev/nvme0n1p2  457G  368G   67G  85% /
tmpfs           7.8G   62M  7.7G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
efivarfs        184K  181K     0 100% /sys/firmware/efi/efivars
/dev/nvme0n1p1  511M  6.1M  505M   2% /boot/efi
tmpfs           1.6G  124K  1.6G   1% /run/user/1000
$ df -h -x tmpfs -x efivarfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2  457G  368G   67G  85% /
/dev/nvme0n1p1  511M  6.1M  505M   2% /boot/efi

apt command

The apt command manages software packages on Debian, Ubuntu, and related distributions.

apt search --names-only "^cowsay$"
apt show tree
sudo apt install cowsay
/usr/games/cowsay "Hello Linux"

apt search searches the package lists, and apt show prints details about a package. Installing software requires root privileges, so we use sudo. In the output, we hide the warning that apt prints when not run in a terminal and show only the last lines of the installation. Before installing, refresh the package lists with sudo apt update; sudo apt upgrade installs updates. Fedora and RHEL use dnf instead.

$ apt search --names-only "^cowsay$" 2>/dev/null
Sorting...
Full Text Search...
cowsay/oldstable 3.03+dfsg2-8 all
  configurable talking cow

$ apt show tree 2>/dev/null | head -n 4
Package: tree
Version: 2.1.0-1
Priority: optional
Section: utils
$ sudo apt install -y cowsay 2>/dev/null | tail -n 3
Unpacking cowsay (3.03+dfsg2-8) ...
Setting up libtext-charwidth-perl:amd64 (0.04-11) ...
Setting up cowsay (3.03+dfsg2-8) ...
$ /usr/games/cowsay "Hello Linux"
 _____________
< Hello Linux >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Source

GNU Coreutils manual

In this article, we have introduced basic Linux commands for system information, text processing, file operations, viewing files, networking, and package management.

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.