Guide Area

Linux Commands Cheat Sheet – 48 Commands You Should Know

Does it always happen to you that you forget what the exact command you used last time was? Of course you can’t find it in history, because it was a long time ago. There are many Linux commands cheat sheet on the internet, but if you don’t like the subset of commands in any of them, here I provide you with my own set which get me through my daily tasks. Save this website to bookmarks – you’ll need it again!

There is thousands of linux commands out there. Many commands have different variations and plenty of attributes, so of course I won’t go through every single one of them. I will give examples along the way, so you can test it easily.

For all provided commands, I assume you have sudo privileges.

Navigating through the system

pwd
See what path you’re currently located in
cd my_folder/
Enter specific directory located in your current directory
cd /home/vlad/my_folder/
Enter specific directory by specifiyng full path
cd ..
Move one directory up
cd ../../..
Move three directories up

Listing files and directories

ls
List files and directories (in-line)
ls -l
List files and directories (newline)
ls -a
Show hidden files while listing files and directories
ls -lt
List files and directories and sort by time (newest on bottom)
ls -lrt
List files and directories and sort by time in reverse order (newest on top)
du -sh /home/vlad/
Shows size of file/directory /home/vlad/
du -h /home/vlad/
Shows size of files and directories under /home/vlad/

Viewing and editing files

cat testfile.txt
Prints out contents of the file testfile.txt
less testfile.txt
Opens contents of the file testfile.txt in “new window”
nano testfile.txt
Opens nano editor to modify contents of the file (Ctrl+O to save, Ctrl+X to close)
sed -i 'vladmarton/a newname' testfile.txt
Finds line with string “vladmarton” and appends “newname” to the next line in file testfile.txt
sed -i 'vladmarton/d' testfile.txt
Finds and deletes line containing “vladmarton” in file testfile.txt
unzip testfile.jar
Unzips file testfile.jar into current directory (also works with zip files)
tar xvf testfile.tar
Unzips file testfile.tar into current directory (also works with tar.gz files)

Moving files and directories

mkdir testdir/
Creates new directory called “testdir”
cp testfile.txt /tmp/
Copies file “testfile.txt” into /tmp/ directory
cp -r testdir/ /tmp/
Copies directory testdir under /tmp/ directory (recursively)
mv testfile.txt /tmp/
Moves file “testfile.txt” into /tmp/ directory
mv -r testdir/ /tmp/
Moves directory testdir under /tmp/ directory
tar xvf testfile.tar
Unzips file testfile.tar into current directory (also works with tar.gz files)

System operations

source ~/.bashrc
Load functions from file /home/vlad/.bashrc (~ means home directory for current logged-in user) into current terminal window
apt-get install apache2
Installs apache2 system package
apt-get remove apache2
Removes apache2 from system packages
apt-get purge apache2
Removes apache2 configuration and cached files
service apache2 start/stop/restart
Start/Stop/Restart service (choose one of the options) (in this case, service is apache2)
alias
Show all available aliases
alias myalias="ssh user@hostname"
Creates alias that connects to server using SSH after writing ‘myalias’ to terminal
man ls
Show documentation for command ‘ls’
ps aux | grep apache2
Show running processes and filter them, so only apache2 is shown
dpkg -l | grep apache2
Show installed system packages and filter them, so only apache2 is shown
ifconfig
Show network configuration

Networking and file transfer

ping google.com
Ping google.com and show response
traceroute google.com
Find all stops (routers, firewalls, switches, etc.) between your host and google.com
ssh user@hostname
Connect to a server using SSH (replace user and hostname with your own)
scp /home/vlad/testfile.txt user@hostname:/var/www/
Transfer file testfile.txt from localhost to remote server directory /var/www/ (replace user and hostname)
scp user@hostname:/var/www/testfile.txt /home/vlad/
Transfer file testfile.txt from remote directory /var/www/ to local directory /home/vlad/ (replace user and hostname)
scp -r /home/vlad/ user@hostname:/var/www/
Transfer folder /home/vlad/ recursively (all it’s subfolders too) from localhost to remote server directory /var/www/ (replace user and hostname)
rsync
Tool for transferring files – see full documentation here

Package repository operations

apt-get update
Parses list of system packages available to install
add-apt-repository ppa:rebuntu16/avidemux+unofficial
Ads a new repository for package “avidemux”
add-apt-repository --remove ppa:rebuntu16/avidemux+unofficial
Removes repository for package “avidemux”
apt-key list
Show keys used for connection to repositories
apt-key adv --keyserver keys.gnupg.net --recv-keys [KEYID]
Renews a key with specific ID (use apt-key list) using server keys.gnupg.net

Conclusion

These are the commands I use the most when working with my projects at home. Don’t forget to bookmark this page, so you don’t lose access to them. Good luck with Linux!

Vladimir Marton

DevOps Engineer focused on cloud infrastructure, automation, CI/CD and programming in Javascript, Python, PHP and SQL. Guidearea is my oldest project where I write articles about programming, marketing, SEO and others.