Linux Commands Cheat Sheet

Linux Cheatsheet

Man pages are the authoritative source of information for specific Linux commands. However, seldom do these man pages include practical examples for the commands that might save some time for the user. Here, in this article, I give you the cheat sheet of Linux commands. Let us know in the comments which ones you already knew and if you discovered something new.

Cheat Sheet for Linux commands

For brevity, I have divided this Linux commands cheat sheet into sections. All the commands in a section are often used in conjunction with the other Linux commands in the section. That said, you might also find yourself using a combination of commands in the different sections. That illustrates the power of Linux and the composability of these programs.

Files and directories

cd /Change directory to the root of the filesystem.
cd targetChange directory to the target directory.
cd ~Change directory to the home directory of the user.
ls -lLong listing, displays file ownership and permissions.
ls -ltrLong listing, but in reverse order of time. Gives the newest files at the bottom.
cat /etc/system-releaseDisplays the contents of the system-release file – which distro and flavor you are running.
cat longfile.txt | moreView a long file page by page.
cp source_file.txt target_file.txtCopies a specific file
cp -r ~/source_dir ~/target_dirCopies all files and sub-directories recursively.
cp /etc/apt/sources.{d,d.backup}Create a backup of sources.d file without having to type the full path of the file twice!
mkdir my_directoryMake a directory with the name of my_directory.
touch my_fileCreate a file with the name of my_file.
mv source_file target_fileMove a file or a directory.
mv my_document.{txt,file}Rename a file from my_document.txt to my_document.file
grep -i doctor $(find . -name *.txt -print)Find the string “doctor” in all the .txt files.

Variables and environment

echo $PATHDisplays the PATH environment variable
envDisplays all ENV variables
export PATH=$PATH:/anotherdirAdds “anotherdir” to your PATH, just for your current session.
source ~/.bashrcReloads .bashrc file. Required if new variables were added or old ones modified in .bashrc file and it is required that changes take effect without restarting the shell.

Execution and processes

./runmyprogramExecute a program or shell script in your current working directory (pwd).
./runmyprogram &Execute a program or shell script as a background task.
ps -efDisplays information about a selection of the running processes.
ps -ef | grep thisprogramFind a particular process by name. Here, find thisprogram.
top or htopDisplays a real-time interactive list of running processes.
kill -9 pidKill the process with PID as pid.
kill -9 -1Kill all processes it can kill. Forces a logout.
whereis executableWhere all places can the executable be found in the PATH?
which executableWhich executable among all in PATH is executed?

Networking

ip -4 aShows the IPv4 address for all NICs.
curl https://ifconfig.me/Fetch your external IP Address.
curl https://www.linuxfordevices.comRetrieve the content (HTML) from https://www.linuxfordevices.com
wget https://someurl.com/somefile.tar.gzwget is a useful utility for downloading files from any website.
sudo netstat -pant | grep LISTENLists the various in-use TCP ports and the processes using it.
sudo netstat -pan | grep 80Lists the process listening on port 80.

System Administration

journalctl -b -p errDisplay all errors since the last boot.
systemctl status foo.serviceCheck the status of foo.service
systemctl start/restart/stop foo.serviceStart/restart/stop foo.service
systemctl enable/disable foo.serviceEnable/disable a service to startup on boot.
shutdown nowShuts down the system immediately.
shutdown +4Shut down the system but after 4 minutes.
rebootPerforms a soft reboot.

Package Manangement

sudo apt install package-nameInstall a package on Debian/Ubuntu based systems.
sudo dnf install package-nameInstall a package on RedHat based(Fedora, CentOS) systems.

Further …

Although this Linux commands cheat sheet provides you with the most used commands, it’s worthwhile to sit down and read the man pages of individual commands. You might have an obscure problem but going through the man pages, you might stumble upon your required solution. So, do read the man pages in conjunction with this cheatsheet. They’re extremely useful.

I hope you learned some new commands or a new way of doing a particular thing through this cheatsheet. Let us know which commands you already knew and which ones you didn’t know at all in the comments.