Linux Commands Documentation
Oshan Upreti
Posted on February 15, 2021
I was preparing for an interview for a position where I was supposed to be tested for LINUX commands. So, as I was preparing and testing my commands knowledge on the terminal, I realized it would be better if I created documentation that might be helpful for others. I have also created documentation of this in GitHub, so, you can view it there, and if you'd like to contribute, you are free to do so.
You can use this to learn commands, practice for interviews, or whatever you want to do. I just hope you find this useful.
Table of Contents
1. Linux Network Commands
2. Linux File Commands
3. Linux Date Commands
4. Linux Search Commands
5. Linux Package Manager Commands
6. Linux User Commands
7. Linux Process Management Commands
8. Linux Hardware Commands
9. Linux Remote Commands
10. Linux File Permission Commands
Linux Network Commands
whois
Shows whois information about the given domain such as registrar info, registrant info, admin info if available.
## Displays available information of apple.com
whois apple.com
ping
Sends ICMP packets to the given address to check connectivity.
## Sends ICMP packets to google.com to check connectivity
ping google.com
ifconfig
Shows IP addresses of all interfaces
ifconfig
iwconfig
Shows wireless properties of the interfaces such as ESSID, Encryption keys.
iwconifg
host
Performs IP lookup for the domain name or vice-versa.
## Displays IP address of apple.com
host apple.com
## Displays domain name of 17.253.144.10
host 17.253.144.10
ss
Displays the information on all connections (TCP, UDP, unix Sockets). Previously accomplished by netstat
command.
## Displays information on all connections
ss
## Only currently listening sockets
ss -l
## Displays UDP sockets
ss -u
## Displays TCP sockets
ss -t
## Displays Unix sockets
ss -x
## Displays TCP sockets that are listening. Works with -u and -x as well
ss -t -a
wget
Download the file from a given source.
## Saves the index.html file of the google homepage
wget google.com
##Downloads the D5100 manual pdf at the given location
wget https://cdn-10.nikon-cdn.com/pdf/manuals/dslr/D5100_EN.pdf
curl
Transfer data to or from a server using supported protocols like HTTP, FTP, SMTP, IMAP.
## displays the content of the URL on the terminal
curl https://www.google.com
## download and saves the pdf in your local machine
curl -O https://cdn-10.nikon-cdn.com/pdf/manuals/dslr/D5100_EN.pdf
## download files from FTP servers. You don't need -u parameter if it's not authenticated.
curl -u <username>:<password> ftp://oshan.net/index.js
## display the definition of the word using DICT protocol. The following line displays the dictionary definition of 'aversion'
curl dict://dict.org/d:aversion
EXTRA TIP: You can view manual for curl
using your terminal with man curl
command.
Linux File Commands
pwd
Displays the path of the current directory.
## Outputs the path of the directory you are in
pwd
ls
Shows the content of your directory.
## Shows the content of current directory
ls
## Shows the content including . files or hidden files of current directory
ls -a
## Shows the content with permission and other details
ls -l
ls -al
cd
Change your current directory to given path.
## Takes you to the downloads folder if there is one
cd Downloads
## Toggle between your last two working directories
cd -
## Takes you to the parent directory
cd ..
## Takes you to the HOME directory
cd
mkdir
Create a new directory in a given filepath.
## Creates a directory named Projects in current directory
mkdir Projects
rm
Delete a file or directory.
## Deletes the file.txt in working directory or specified path
rm file.txt
## Deletes the directory dir1 iff it's empty
rmdir dir1
## Deletes the directory dir1 along with its contents
rm -r dir1
mv
Move file/directory from one place to other. You can also use this to rename files.
## Move file.txt to directory dir1
mv file.txt dir1
## Rename file.txt to index.txt
mv file.txt index.txt
## Move directory dir1 to directory dir2
mv dir1 dir2
## To avoid overwriting index.txt if it already exists
mv -n file.txt index.txt
You can also move multiple files such as mv file1 file2 file3 dir
.
cp
Copy file/directory from one place to another.
Basic format:
cp [Options] sourceFile/Dir destinationFile/Dir
## Copies file.txt to dir1
cp file.txt dir1
## Copies directory dir1 to dir2 along with the contents
cp -R dir1 dir2
## Copies content of file.txt to file2.txt. You can use -i or -b or -f options according to your requirement. -i asks for confirmation if the file needs to be overwritten. -b creates a backup before overwriting file. -f forces the overwriting without any warning/backup.
cp file.txt file2.txt
You can also move multiple files such as cp file1 file2 file3 dir
.
touch
Create or update a file on a specified path.
## Creates file file1.txt in the current director
touch file1.txt
## Creates multiple files file1.txt and index.js in the current working directory
touch file1.txt index.js
cat
- Displays the content of the file
- Create a new file
- Copy the content of one file to other
- Merge files
## Display the content of file.txt
cat file.txt
## Create a file with the text "This is the first line"
cat > file2.txt
This is the first line.
## Copy the content of file2.txt to file.txt
cat file2.txt > file.txt
## Append "This is the second line" in the existing file file2.txt
cat >> file2.txt
This is the second line.
## Merge contents of file1 and file2 as merged_file
cat file1.txt file2.txt > merged_file.txt
head/tail
Displays the first 10 lines of file or last 10 lines of file. Helps when you are viewing a large file.
## Displays the first 10 lines of file.txt
head file.txt
## Displays the last 10 lines of file.txt
tail file.txt
Linux Date Commands
date
Displays date and time of the system.
## Displays date and time of the system
date
## Displays date and time of 5 days ago
date --date="5 days ago"
## Similarly, displays date and time of 10 seconds ago
date --date="10 seconds ago"
cal
Displays calendar.
## Displays the current month with day highlighted
cal
## Displays the third month of 1997
cal 03 1997
Linux Search Commands
grep
Search for pattern in given file.
## Searches and displays lines containing string "linux" in file.txt
grep "linux" file.txt
## Searches and displays lines containing string "linux" in files in directory dir
grep -r "linux" dir
## Searches for the exact word "linux" in file.txt
grep -w "linux" file.txt
## Ignores the case while searching
grep -i "linux" file.txt
find
Searches for files in given directory with given criteria.
Generally the pattern is:
find [starting_directory] [criteria]
## displays the path of all files in the current directory
find .
## displays the path of files with name "index.js" in current directory
find . -name index.js
## displays the path of files greater than 500kb in size
find . -size +500k
You can use +/- with -atime
or -mtime
to get the files with last accessed time or modified time.
You can also execute commands on the result with -exec
command or with prompt using -ok
command.
## finds the file named index.js and delete it after displaying prompt
find . -name index.js -ok rm {} \;
Package Manager Commands
rpm
Manages rpm packages.
## Install package named "oshan.rpm"
rpm -ivh oshan.rpm
## Update if the package is already installed "oshan.rpm"
rpm -Uvh oshan.rpm
## Remove package named "oshan.rpm"
rpm -e oshan.rpm
## Verify package named "oshan.rpm"
rpm -V oshan.rpm
## List all the RPM packages
rpm -qa
Generally, most of the distros use their own package manager, and it's prefereable to use package manager like yum
or dnf
instead of rpm because it handles all the dependencies.
I use OpenSUSE, and I have zypper
as my package manager.
apt
is used commonly for Debian family systems to handle dpkg.
User Commands
useradd
Add new users. (needs superuser privileges)
## Add new user named friends
sudo useradd friends
## Set password for friends
sudo passwd friends
## Specify userid 3001, group as users while adding user
sudo useradd -u 3001 -g users oshan
## Create user with expiry date as 2021-02-30
sudo useradd -e 2021-02-30 oshan
## Check Account/Password expiry dates for oshan
sudo chage -l oshan
userdel
Delete users. (need superuser privileges)
## Delete user named friends
sudo userdel friends
## Delete user friends and it's home directory
sudo userdel -r friends
last
Displays the last logins on the system.
Tips:
If you want to check if anyone is using your computer when you are not home this is the command you need.
#Outputs the info on last logins
last
Similar commands:
## Shows the active user id and group id
id
## Shows the username and login time of active user
who
Process Management Commands
ps
Displays process status.
## returns all the process running under the current user
ps ux
## returns the status of process with process ID 5266
ps 5266
top
Displays in real time all the running process along with CPU usage, Memory usage, Priority, CPU time.
## You can press q to exit from the display
top
kill
kills the process with given process ID
## Terminates the process with PID 5766
kill 5766
## Kills all process named code
killall code
pidof
returns the Process ID of given process name
## returns the process ID of process name code
pidof code
## returns the process ID of process name containing code. Here code is the pattern instead of process name.
pgrep code
pkill
kills the process with the process name of the given pattern
## kills all the processes whose names has pattern code
pkill code
jobs
returns the list of background jobs
## returns the background jobs with status
jobs
## send stopped processes to background jobs
bg
## bring the background job code to foreground
fg code
free
returns the memory status (RAM)
## Outputs the memory status in kilobytes
free
## Outputs the memory status in gigabytes or you can use -m for megabyte, -b for byte
free -g
Hardware Commands
lsusb
Lists USB buses and the devices connected to them.
## Displays USB buses and the devices connected
lsusb
## Displays detailed information about USB buses and connected devices
lsusb -v
lspci
Lists PCI buses and the devices connected ot them. You might need root privileges to run this command.
## Displays PCI buses and the devices connected
lspci
## Displays detailed information of PCI buses. The amount of details increase with the number of v in the options
lspci -v
lspci -vv
lspci -vvv
lshw
Lists the hardware configuration of the machine. Device information, Motherboarad configuration, CPU version, Memory configuration, etc. You might need root privileges to run this command.
## Displays the hardware configuration of the machine
lshw
## Displays the information in html format with tree structure
lshw -html
EXTRA TIPS: lshw -html > index.html
to save the output as index.html and view it in your browser.
lsblk
Lists the information about block devices.
## Lists the block devices
lsblk
Using different options you can modify your result. Some of the useful ones are:
lsblk -all
list all devices including empty ones
lsblk -O
list all devices with all the availables columns
lsblk -o ColumnName
list devices with given column information
fdisk
List out partition information and lets you modify it as well. You might need root privileges to run this command.
## List the partition and their sizes, number of sectors
fdisk -l
## lets you create/delete partition or other commands in given disk
fdisk /dev/sdb1
dmidecode
List hardware information from the BIOS. You might need root privileges to run this command.
## List the hardware information from the BIOS
dmidecode
## List the hardware information with little detail only. It's more readable.
dmidecode -q
dmesg
List detected hardware and boot messages.
## List the boot messages with detected hardware
dmesg
## List the messages in human readable format
dmesg -H
## Read the information and clear it
dmesg -c
EXTRA TIP: dmesg -H > file.txt
saves the output in file.txt.
badblocks
Used to check for badblocks in disk partition. You might need root privileges to run this command.
## Check for bad blocks in partition /dev/sda with progress of the scan. -s parameter is optional.
badblocks -s /dev/sda
EXTRA TIP: badblocks -s /dev/sda -o file.txt
saves the list of badblocks found in file.txt.
hdparms
Get/Set hard disk parameters.
## Check hard disk drive speed. I got 999.36 MB/sec
hdparm -t /dev/sda1
## Check hard disk cache read speed. I got 6458.49 MB/sec
hdparm -T /dev/sda1
## you can see all other things you can do with this command
hdparm -h
Linux Remote Commands
ssh (Secure Shell)
SSH lets you connect remote systems and enables secured transfer of files over insecure networks. It is used widely over its predecessors telnet
which isn't as secured as SSH, and there are possible problems like eavesdropping.
## Install ssh server (On debian systems )
sudo apt-get install openssh-server
## Install ssh server on OpenSUSE or you can use your Package Manager
sudo zypper install openssh-server
## Start ssh server
sudo service ssh start
## Check ssh server status
sudo service ssh status
## Connect to remote server
ssh <username>@<host_ip_address>
## Connect to host using specific port
ssh -p <port> <username>@<host_ip_address>
telnet
Telnet is a client-server protocol based on TCP connections. It dates back to almost 50 years ago. SSH is preferred over, but still it doesn't hurt to learn about this as it is still in use.
## Install ssh server (On debian systems )
sudo apt-get install openssh-server
## Install ssh server on OpenSUSE or you can use your Package Manager
sudo zypper install openssh-server
## start the connection setup
telnet <hostname>
## start using IP address and port number
telnet <IP_ADDRESS> <PORT>
Linux File Permission Commands
chmod
Change the file access rights. The permission is expressed in three sets of three characters to denote permission for file owner, group owner, and all other users.
For files:
- 1st set of rwx - Read, write, and execute permission for file owner
- 2nd set of rwx - Read, write and execute permission for group owner
- 3rd set of rwx - Read, write, and execute permission for all other users
Here, we will use octal notation to make changes to file permissions.
rwx rwx rwx 111 111 111
--- --- --- 000 000 000
rw- rw- rw- 110 110 110
111 => 7
110 => 6
101 => 5
100 => 4
011 => 3
010 => 2
001 => 1
000 => 0
So, it's easy to give permission using octal notation to files.
## Give read, write, and execution permission to everybody
chmod 777 index.js
## Give read, write, and execution permission to nobody
chmod 000 index.js
## Give all permission to directory owner and no permission to anybody else
chmod 700 dir
sudo
Get superuser (root) privileges temporarily. You need to enter root password for that. There are some commands that need superuser privileges because the file containing them don't have permission for other users.
sudo fdisk -l
chown
Change ownership of files. You need sudo privileges for executing this command.
## Change ownership of index.js from user to root
sudo chown root index.js
chgrp
Change group owner of file. You need sudo privileges for executing this command.
## Change group ownership of index.js from user to root
sudo chgrp root index.js
Posted on February 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.