Despite the beautiful interfaces found on most modern distros, knowledge of Linux command lines is a useful tool to have. Knowing these basic shortcuts can make you more efficient and supercharge your productivity, not to mention make coding at the terminal that much easier. Regardless of your current background knowledge of Linux, you can start using this list of useful Linux commands right away.
Download this Cheatsheet
Enter your email below to receive this PDF cheatsheet in your Inbox.
Command | Description |
---|---|
File Commands | |
ls | List the content of the current directory. |
ls -R | List the subdirectories recursively. |
ls -a | List all the content, including hidden files. |
ls -l | List the content and its information. |
ls -lh | List all the files and folders with file sizes in human-readable format. |
pwd | Output the current directory that you are in. |
cd folder1 | Change the working directory to folder1. |
cd | Return to the $HOME directory. |
cd .. | Go up a directory. |
cd – | Return to the previous directory. |
cp source destination | Copy source to destination. |
cp -r source destination | Copy a folder recursively from source to destination. |
mv source destination | Move (or rename) a file from source to destination. |
rm file1 | Remove file1. |
rm -f file1 | Remove file1 without prompt. |
rm -r folder | Remove a folder and its content recursively. |
mkdir foldername | Create a new folder foldername. |
rmdir foldername | Remove an empty folder. |
touch file1 | Create a file named “file1” in the current working directory. |
file file1 | Show the file type of file1. |
cat file1 file2 | Concatenate file1 to file 2. |
cat > file1 | Concatenate standard input to file1. |
less file1 | View and paginate file1. |
head file1 | Show first 10 lines of file1. |
tail file1 | Show last 10 lines of file1. |
chmod 777 file | Change file permission of file to 777. |
chown user:group file | Change ownership of file to user and group group. |
chmod -R +rwx /path/to/file | Change the read, write and execute permission bits for all files and folders in the path directory. |
ln -s source destination | Create a symbolic link from source to destination. |
ln -f source destination | Create a hard link from source to destination and remove and existing links between the two files. |
File Encryption and Compression | |
gpg2 -c file | Encrypt file. |
gpg2 file.gpg | Decrypt file.gpg. |
tar -cf archive.tar foo bar | Create archive.tar from files foo and bar. |
tar -xf archive.tar | Extract all files from archive.tar. |
gzip file1 | Compress file1 and rename it to file1.gz. |
gzip -d file1.gz | Decompress file1.gz. |
tar cvzf archive.tar.gz foo bar | Create a gunzip archive named “archive.tar.gz” using the files’ foo and bar. |
tar xvzf archive.tar.gz | Extract a gunzip archive named “archive.tar.gz” and place the contents in the current working directory. |
GPG Key Management | |
gpg2 –full-gen-key | Create a new GPG keypair. |
gpg2 –list-keys | List all the available GPG keys in the system. |
gpg2 –edit-key [gpg email] | Modify the properties of a GPG key that you currently use. |
gpg2 –export –armor –output filename.asc [gpg email] | Create an importable ASCII public key for a GPG key that you use. |
gpg2 –export-secret-keys –armor –output filename.asc [gpg email] | Create an importable ASCII private key for a GPG key that you use. |
gpg2 –import filename.asc | Import a keyfile to your machine’s keyring. |
gpg2 –gen-revoke –armor –output filename.asc [gpg email] | Generate a revocation certificate for a GPG key that you currently use. |
User and Group Management | |
useradd -G email -s /bin/bash linux | Create a new user named “linux”, include it to the group “email” and set its default shell to bash. |
groupadd -g 2222 extra | Create a new system group named “extra” with a Group ID of 2222. |
usermod -G extra linux | Add the existing “linux” user to the “extra” group. |
groupmod -n super extra | Rename the group “extra” to “super”. |
userdel linux | Delete the user “linux” in the system. |
groupdel super | Delete the group “super” in the system. |
Text Manipulation | |
sed 11q sample.txt | Print the first 11 lines of the file “sample.txt”. |
sed -i s/string1/string2/g sample.txt | Replace every instance of the word “string1” with “string2” in the “sample.txt” file. |
sed -i y/string1/strings2/g sample.txt | Transpose the word “strings2” to every instance of the word “string1” in the “sample.txt” file. |
uniq input.txt | Print all the identical lines of text in the “input.txt” file. |
sort -r sample.txt | Reverse the line order of the “sample.txt” file. |
sort -R sample.txt | Randomize the line order of the “sample.txt” file. |
sort -g sample.txt | Arrange the lines of the “sample.txt” file by absolute numerical order. |
sort -d sample.txt | Arrange the lines of the “sample.txt” file by alphabetical order. |
sort -M sample.txt | Arrange the lines of the “sample.txt” file by month order. |
cut -c a,e,i,o,u sample.txt | Remove all the vowel characters in the “sample.txt” file and print the result to the terminal. |
cut -f field1,field2 sample.txt | Remove the first two Tab-separated columns in the “sample.txt” file and print the result to the terminal. |
tr “A-Za-z” “A-Z” < sample.txt | Translate all the letters in the “sample.txt” file to uppercase. |
tr -cd “[:print:]” < sample.txt | Delete all the control characters in the “sample.txt” file. |
System Commands | |
uname -a | Show system and kernel. |
uname | Show distribution. |
mount | Show mounted filesystems. |
lsblk | List all currently attached block storage devices. |
lsmod | List all currently installed kernel modules in the system. |
lspci | List all currently attached PCI and PCI-E devices in the machine. |
lsusb | List all currently attached USB-type devices in the system. |
date | Show system date. |
uptime | Show system uptime. |
whoami | Show your username. |
users | Display who is online. |
groups | Print all the names of the groups that the current user is a part of. |
apropos command | Print all the matching manpages for the string “command”. |
man command | Show manual for command. |
info command | Display the appropriate info page for that particular command. |
!! | Repeat the last command. |
exit | Log out of current session. |
File Searching Commands | |
grep pattern files | Search for pattern in files. |
grep -i | Case insensitive search. |
grep -r | Recursive search. |
grep -v | Inverted search. |
grep -o | Show matched part of file only. |
locate file1 | Find file1. |
whereis command | Find binary/source/manual for command. |
which app | Locate a command for the app. |
look string file1 | Display any lines in file1 which contain string as a prefix. |
Process Management | |
ps | Show snapshot of processes. |
top | Show real time processes. |
kill pid | Kill process with id pid. |
pkill name | Kill process with name name. |
killall name | Kill all processes with names beginning name. |
time command | Run the “command” program and print the amount of time the program ran to standard output. |
Disk Space | |
df -h | Show free space on mounted filesystems. |
du -h folder | Show file usage of each folder in folder. |
du -hc folder | Show the file usage for each folder in folder and print the total file size. |
du -sh folder | Show the total file size of folder. |
fdisk -l | Show disks partitions sizes and types (run as root). |
free | Show memory and swap usage. |
Image credit: Christina Morillo via Pexels
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox