mag72

Linux Commands Cheat Sheet

Linux Commands Cheat Sheet

Hitesh J

There are hundreds – possibly thousands –  commands available in Linux. Remembering every command is not possible and it can be quite daunting for a novice user. The good news is that you don't need to remember each command. Only a very small subset of those commands are used on a day-to-day basis.

This cheat sheet offers a set of commands that you can use for quick reference. I have prepared this Linux Commands Cheat Sheet as quick reference for both experienced and basic users.

Basic Linux Commands

In this section, we will show you some basic Linux commands with examples.

To get system information including, operating system, kernel, and release version, run the following command:

hostnamectl

To display the current system date and time, run the following command:

date

To display the hostname of the system, run the following command:

hostname

To display the IP and Mac Address of the system, run the following command:

ifconfig

To displays currently logged in users in the system, run the following command:

w

To displays free and used memory in the system, run the following command:

free -m

To display all running processes, run the following command:

top

To list all files and directories in the current working directory, run the following command:

ls

To list all files and directories including, hidden files and other information like permissions, size and owner, run the following command:

ls -al

To change the directory to the home directory, run the following command:

cd

To change the directory to one level up, run the following command:

cd ..

To display the content of the file, run the following command:

cat filename

To combine two files named file1 and file2 and store the output in a new file file3, run the following command:

cat file1 file2 > file3

To display the last 10 lines of a file, run the following command:

tail filename

To display the first 10 lines of a file, run the following command:

head filename

To rename a file, run the following command:

mv oldfile newfile

To delete a file, run the following command:

rm filename

To create a directory, run the following command:

mkdir dirname

To remove a directory, run the following command:

rm -rf dirname

To print a history list of all commands, run the following command:

history

To clear the terminal, run the following command:

clear

To shut down the system, run the following command:

shutdown -h now

To restart the system, run the following command:

reboot

Networking Commands

To list all IP addresses and network interfaces, run the following command:

ip addr show

Or

ifconfig

To add a temporary IP address to interface eth1, run the following command:

ip addr add IP-Address dev eth1

To display all listening port, run the following command:

netstat -pnltu

To display more information about any domain, run the following command:

whois domainname

To display DNS information of any domain, run the following command:

dig domainname

To perform an IP lookup for a domain, run the following command:

host domainname

To perform a reverse lookup of an IP address, run the following command:

dig -x IP-Address

To perform a reverse lookup on domain, run the following command:

dig -x domainame

To check connectivity between two hosts, run the following command:

ping host-ip

File Permission Commands

To check the current permission of any file, run the following command:

ls -l filename

To assign full(read, write, and execute) permission to everyone, run the following command:

chmod 777 filename

To assign full permission to the directory and all sub-directories, run the following command:

chmod -R 777 dirname

To assign full permission to the owner, and read and write permission to group and others, run the following command:

chmod 766 filename

To remove the execution permission of any file, run the following command:

chmod -x filename

To change the ownership of a file, run the following command:

chown username filename

To change the owner and group ownership of a file, run the following command:

chown user:group filename

To change the owner and group ownership of the directory and all sub-directories, run the following command:

chown -R user:group dirname

User and Group Management Commands

Linux is a multi-user operating system. So multiple users can log in to the system and work on the system at the same time. In some cases, two or more users may need to share access to system resources like files and directories. In that case, user and group management allows you to complete your objectives.

To display all login users, run the following command:

w

To add a new user account, run the following command:

useradd username

To delete a user account, run the following command:

userdel -r username

To change the user account information including, group, home directory, shell, expire date, run the following command:

usermod [option] username

To add a user to a specific group, run the following command:

usermod -aG groupname username

To create a new group, run the following command:

groupadd groupname

To remove a group, run the following command:

groupdel groupname

To display information of the last login user, run the following command:

last

To display UID and GID of the current user, run the following command:

id

Process Management Commands

When you run any application in Linux. The application will get a process ID or PID. Process Management helps you to monitor and manage your application.

To display all active processes, run the following command:

ps

Display information of specific process, run the following command:

ps -ef | grep processname

To manage and display all processes in realtime, run the following command:

top

To display processes in the tree-like diagram, run the following command:

pstree

To list all files opened by running processes, run the following command:

lsof

To kill a specific process using process ID, run the following command:

kill pid

To kill all processes by name, run the following command:

killall processname

To display stopped or background jobs, run the following command:

bg

Get the PID of any process, run the following command:

pidof processname

Disk Management Commands

In this section, we will show you disk management commands including, add and remove partitions, mount a partition, check disk space, format partition, etc.

To list all disk partitions, run the following command:

fdisk -l

To create a new partition on /dev/sda device, run the following command:

fdisk /dev/sda

To format the partition named /dev/sda1, run the following command:

mkfs.ext4 /dev/sda1

To check and repair a filesystem for any error, run the following command:

fsck.ext4 /dev/sda1

To mount any partition to any directory, run the following command:

mount /dev/sda1 /mnt

To display free space of mounted file system, run the following command:

df -h

To display free inodes on the filesystem, run the following command:

df -i

To display the size of your current directory, run the following command:

du -hs

To display information about block devices, run the following command:

lsblk

To display all USB devices, run the following command:

lsusb -tv

To perform a read speed test on disk /dev/sda, run the following command:

hdparm -tT /dev/sda

To test for unreadable blocks on disk /dev/sda, run the following command:

badblocks -s /dev/sda

Package Management Command

In this section, we will show a list of all commands to install, remove and manage packages in Linux.

To install the package on Debian based distributions, run the following command:

apt-get install packagename

To remove a package on Debian based distributions, run the following command:

apt-get remove packagename

To get a list of all packages on Debian based distributions, run the following command:

dpkg -l | grep -i installed

To install .deb package, run the following command:

dpkg -i packagename.deb

To update the repository on Debian based distributions, run the following command:

apt-get update

To upgrade a specific package on Debian based distributions, run the following command:

apt-get upgrade packagename

To remove all unwanted packages on Debian based distributions, run the following command:

apt-get autoremove

To install the package on RPM-based distributions, run the following command:

yum install packagename

To remove a package on RPM-based distributions, run the following command:

yum remove packagename

To update all system packages to the latest version on RPM-based distributions, run the following command:

yum update

To list all installed packages on RPM-based distributions, run the following command:

yum list --installed

To list all available packages on RPM-based distributions, run the following command:

yum list --available

Compress and Uncompress Commands

Tar, Zip, and Unzip are the most popular command-line utility in Linux used to compress and uncompress files and directories.

To compress a file in the Tar archive, run the following command:

tar -cvf filename.tar filename

To uncompress a Tar file, run the following command:

tar -xvf filename.tar

To list the content of the Tar file, run the following command:

tar -tvf filename.tar

Untar a single file from Tar file, run the following command:

tar -xvf filename.tar file1.txt

Add a file to the Tar file, run the following command:

tar -rvf filename.tar file2.txt

To compress a single file to a zip, run the following command:

zip filename.zip filename

To compress multiple files to a zip, run the following command:

zip filename.zip file1.txt file2.txt file3.txt

To add a file to a zip file, run the following command:

zip -u filename.zip file4.txt

To delete a file from a zip file, run the following command:

zip -d filename.zip file4.txt

To display the content of zip archive file, run the following command:

unzip -l filename.zip

To unzip a file, run the following command:

unzip filename.zip

Unzip a file to a specific directory, run the following command:

unzip filename.zip -d /dirname

Conclusion

The above examples are the most commonly used Linux commands. I hope this will help you to perform your day-to-day system administration task.