Linux 1
Linux 1
1 Shell commands 1
1.1 Terminal emulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 date command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 cal command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 whoami command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 id command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 pwd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 cd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 . directory and .. directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.9 ls command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 mkdir command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.11 rm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.12 Copying a file using cp command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.13 Renaming or moving a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.14 tree command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.15 wc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.16 less command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.17 echo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.18 Redirecting the command output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.19 Using > to redirect output to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.20 Moving around in the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.21 man pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.22 Counting files in a directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.23 Editing longer commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 File system 11
2.1 FHS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Useful commands 13
3.1 Creating soft link to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Creating hard links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3 Extracting a tar file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Creating a tar file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5 Vim editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.6 :q to exit vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.7 Open a new file or edit an existing file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.8 Different modes of vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
i
3.9 :w to save a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.10 :q! to quit without saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.11 Becoming root user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.12 Using sudo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.13 !! trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.14 Setting up hostname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.15 Environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.16 Setting up environment variable values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.17 locate command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.18 Finding date/time in different timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.19 Bash history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.20 Sort files by size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6 File permissions 33
6.1 chmod command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.2 PATH variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.3 ~/.bash_profile file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.4 .bashrc file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.5 /etc/profile file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.6 which command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.7 Use which command to see how $PATH variable works . . . . . . . . . . . . . . . . . . . . . . . . 36
6.8 she-bang or sha-bang in executable files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7 Processes in Linux 37
7.1 How to view all running processes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.2 How to find a particular process? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.3 How to kill/stop a particular process? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.4 Finding out list of open files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
ii
7.5 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.6 top command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.7 Load average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.8 htop tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.9 More about Linux processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.10 /proc directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.11 /proc/cpuinfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.12 /proc/cmdline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.13 /proc/meminfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.14 /proc/uptime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.15 /proc/sys/ & sysctl command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.16 Enabling IP forward with sysctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
8 Linux Services 45
8.1 What is a service? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.2 What is a daemon? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.3 What is the init system? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.4 Units in systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.5 .service units in systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.6 How to find all the systemd units in the system? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.7 Working with a particular service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.8 Enabling or disabling a service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.9 Shutdown or reboot the system using systemctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.10 journalctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.11 Finding the logs of a service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.12 To view only the last N entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.13 Continuous stream of logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.14 Listing of previous boots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.15 Time-based log viewing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.16 Total size of the journal logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.17 Writing your own service file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
10 Package management 63
10.1 dnf command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.2 Searching for a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.3 Finding more information about a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.4 Installing a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.5 To list the available updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
10.6 To list all security updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
10.7 Update the packages via dnf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
10.8 Find out the services & applications need restart after update in Fedora/CentOS/RHEL . . . . . . . . 66
iii
10.9 Automatic updates in dnf systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10.10 apt command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10.11 apt update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.12 Installing a package via apt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.13 apt-cache search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.14 Listing upgrades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.15 Upgrading packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.16 Figuring out which services/processes need restart after package upgrade in Debian . . . . . . . . . . 70
10.17 Listing available security updates in Debian systems . . . . . . . . . . . . . . . . . . . . . . . . . . 71
10.18 Unattended upgrades in Debian systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
11 SELinux 73
11.1 SELinux Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.2 getenforce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.3 setenforce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.4 Labels/Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.5 Checking contexts of files/directories or processes . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.6 SELinux booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
13 Networking commands 81
13.1 Finding the IP address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
13.2 Finding ARP table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
13.3 ping command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
13.4 Short note about DNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
13.5 /etc/hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.6 /etc/resolv.conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.7 systemd-resolved controlled name resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.8 resolvectl command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
13.9 host command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.10 dig command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.11 ss command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13.12 traceroute command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.13 tracepath command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.14 Remote login to a computer using ssh tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.15 ssh key generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.16 ssh-copy-id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.17 Stop and disable the sshd service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.18 Disable password based login for ssh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.19 How to find active (open) network connections from your computer? . . . . . . . . . . . . . . . . . 93
13.20 To know about ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
14 Linux Firewall 95
14.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.2 Tables, chains and rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.3 filter table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.4 nat table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5 iptables command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.6 View the existing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
iv
14.7 Appending rules to INPUT chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.8 Flushing all rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.9 Example of a series of rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.10 Delete a rule based on rule number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.11 Delete a rule directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.12 Saving the rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.13 A blog post from Major Hayden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.14 Debugging firewall rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
17 Workbook 107
17.1 How to use this workbook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
17.2 copy paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.3 Find your user id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.4 Creating softlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.5 Basic vim usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.6 Adding a new user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.7 Deleting an existing user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.8 Finding the IP address of dgplug.org . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.9 Change the local timezone of the system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.10 Add sudo access to an user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
19 Containers 113
20 Team 115
Index 119
v
vi
CHAPTER 1
Shell commands
In Linux the shell (or terminal) is the lifeline of the developer, and of any power user. Things which can be done on
the GUI (by clicking on different buttons), can be done much more efficiently on the terminal by using commands.
Maybe one can not remember all the commands, but with regular usage one can easily remember the most useful ones.
The following guide will introduce you to a minimal set of basic commands required to use your Linux computer
efficiently.
The above is the screenshot of the Gnome terminal application. As you can see the command prompt contains the
following information:
[username@hostname directoryname]
In our case the username is babai, hostname is kdas-laptop, and directory is mentioned as ~. This ~ is a special
character in our case. It means the home directory of the user. In our case the home directory path is /home/babai/.
The Gnome terminal is one of many implementations of terminal emulators. Different Linux environments may come
pre-installed with different terminals.
Read the articles on Wikipedia to learn about computer terminals, terminal emulators and shell.
$ date
Sun Jun 25 10:13:44 IST 2017
In case you want to know the current date/time in UTC, use the following command. (I added this in 2018, so please
do not get confused at the date.)
1
Linux command line for you and me Documentation, Release 0.1
$ date -u
Mon May 21 01:43:47 UTC 2018
If you want to see yesterday’s date, or a 10 days ago, you can even do that.
$ date --date="yesterday"
Fri Apr 9 07:09:01 PM IST 2021
cal command is used to display a calendar in your shell, by default it will display the current month.
$ cal
June 2017
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
$ cal 07 2017
(continues on next page)
whoami command will tell you which user account you are using in this system.
$ whoami
fedora
1.5 id command
id prints real user id, and various other details related to the account.
$ id
uid=1000(fedora) gid=1000(fedora) groups=1000(fedora),4(adm),10(wheel),190(systemd-
˓→journal) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
pwd command, short for print working directory, will help you to find out the absolute path of the current directory.
Let us see an example below:
1.7 cd command
The next command we will learn is cd, short for change directory. This command will help you to change your current
directory. We will move to /tmp directory in our example.
Here you can see that first we moved to /tmp directory, and then we moved back to the home directory by using ~
character.
. and .. has special meaning in the Linux. . means the current directory and .. means the parent directory. We can use
these in various situations for daily activities.
$ cd ..
The above command changes the current directory to the parent directory.
1.9 ls command
We use ls command to list the files and directories inside any given directory. If you use ls command without any
argument, then it will work on the current directory. We will see few examples of the command below.
[babai@kdas-laptop ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[babai@kdas-laptop ~]$ ls /tmp/
cpython systemd-private-759094c89c594c07a90156139ec4b969-colord.service-
˓→hwU1hR
hogsuspend systemd-private-759094c89c594c07a90156139ec4b969-rtkit-daemon.
˓→service-AwylGa
hsperfdata_babai tracker-extract-files.1000
plugtmp tracker-extract-files.1002
[babai@kdas-laptop ~]$ ls /
bin cpython etc lib lost+found mnt proc run srv sysroot usr
boot dev home lib64 media opt root sbin sys tmp var
In the last two commands we provided a path as the argument to the ls command. / is a special directory, which
represents root directory in Linux filesystem. You will learn more about that in the next chapter.
We can create new directories using mkdir command. For our example we will create a code directory inside our home
directory.
[babai@kdas-laptop ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[babai@kdas-laptop ~]$ mkdir code
[babai@kdas-laptop ~]$ ls
code Desktop Documents Downloads Music Pictures Public Templates Videos
We can also create nested directories in a single command using the -p option.
dir1/dir2/:
dir3
1.11 rm command
rm command is used to remove a file, or directory. The -r option is being used to remove in a recursive way. With -f
you force the removal, ignoring errors and never prompt. You can chain the flags, so instead of rm -r -f you can as
well type rm -rf. But, always double check before you use rm -rf command, if you by mistake give this command in
your home directory, or any other important directory, it will not ask to confirm, but it will delete everything there. So,
please be careful and read twice before pressing enter key.
dir1/dir2/:
We use the cp command to copy a file in the Linux shell. To copy a folder with its contents recursively use the cp
command with the -r flag. We use the cp file_to_copy new_location format. In the example below, we are copying the
hello.txt to hello2.txt.
$ cp hello.txt hello2.txt
$ ls -l
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:47 hello2.txt
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:33 hello.txt
In another example, I will copy the file passwordauthno.png from the Pictures directory in my home directory to the
current directory.
$ cp ~/Pictures/passwordauthno.png .
In the following example, I will be copying the images directory (and everything inside it) from the Downloads
directory under home to the /tmp/ directory.
$ cp -r ~/Downloads/images /tmp/
The mv command is used to rename or move a file or directory. In the following example, the file hello.txt is renamed
to nothello.txt.
$ mv hello.txt nothello.txt
$ ls -l
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:33 nothello.txt
tree command prints the directory structure in a nice visual tree design way.
1.11. rm command 5
Linux command line for you and me Documentation, Release 0.1
Note: The tree command may not be installed on your system by default, you can install it later after reading the
package management chapter.
1.15 wc command
wc, short for word count, is an useful command which can help us to count newlines, words and bytes of a file.
$ cat hello.txt
HI that is a file.
This is the second line.
And we also have a third line.
$ wc -l hello.txt
3 hello.txt
$ wc -w hello.txt
17 hello.txt
The -l flag finds the number of lines in a file, -w counts the number of words in the file.
less command helps us to view the content of any file, it allows both forward and backward movement. It does not
require to read the full file, so it helps to read faster any big text file.
$ less ~/.bash_history
$ echo "Hello"
Hello
In Linux shells, we can redirect the command output to a file, or as input to another command. The pipe operator | is
the most common way to do so. Using this we can now count the number of directories in the root (/ ) directory very
easily.
$ ls /
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run
˓→sbin srv sys tmp usr var
$ ls / | wc -w
20
The | is known as pipe. To know more about this, watch this video.
We can use > to redirect the output of one command to a file, if the file exists this will remove the old content and only
keep the input. We can use >> to append to a file, means it will keep all the old content, and it will add the new input
to the end of the file.
$ ls / > details.txt
$ cat details.txt
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
$ ls /usr/ > details.txt
$ cat details.txt
bin
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
(continues on next page)
There are key shortcuts available in Bash which will help you to move around faster. They are by the way very similar
to the standard emacs keybindings, a number of key combinations that you will discover in many places and therefore
are very handy to memorize and internalize. The following table is a good starting point.
man shows the system’s manual pages. This is the command we use to view the help document (manual page) for
any command. The man pages are organized based on sections, and if the same command is found in many different
sections, only the first one is shown.
The general syntax is man section command. Example man 7 signal.
You can know about different sections below. Press q to quit the program.
Normally ls commands shows all the files and directories in multiple column. But if you pipe the output to any another
command, then it prints one name in a line. We can combine that with wc -l to count the number of files in a directory.
ls | wc -l
73
If you are typing a long command or something multi-line, then you can type Ctrl-x-e, press Control button, and then
x and then e key. This will open up a temporary editor using the $EDITOR.
File system
Now you know a few really basic, Linux commands. Before we can learn anything else, we should look into how files
and directories are structured inside a Linux system.
2.1 FHS
$ ls /
bin boot dev etc home lib lib64 lost+found mc media mnt opt output proc
˓→root run sbin srv sys tmp usr var
/ is the root directory of your file system. It’s under this directory, that all the other files and directories reside. There’s a
Filesystem Hierarchy Standard(FHS), which talks about these different directories, and what kinds of files are located
in which directory.
11
Linux command line for you and me Documentation, Release 0.1