Linux Command Line Cheatsheet

People At Computer Using Linux Command Lines

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.

Linux Command Line Cheatsheet Download

Download this Cheatsheet

Enter your email below to receive this PDF cheatsheet in your Inbox.

CommandDescription
File Commands
lsList the content of the current directory.
ls -RList the subdirectories recursively.
ls -aList all the content, including hidden files.
ls -lList the content and its information.
ls -lhList all the files and folders with file sizes in human-readable format.
pwdOutput the current directory that you are in.
cd folder1Change the working directory to folder1.
cdReturn to the $HOME directory.
cd ..Go up a directory.
cd –Return to the previous directory.
cp source destinationCopy source to destination.
cp -r source destinationCopy a folder recursively from source to destination.
mv source destinationMove (or rename) a file from source to destination.
rm file1Remove file1.
rm -f file1Remove file1 without prompt.
rm -r folderRemove a folder and its content recursively.
mkdir foldernameCreate a new folder foldername.
rmdir foldernameRemove an empty folder.
touch file1Create a file named “file1” in the current working directory.
file file1Show the file type of file1.
cat file1 file2Concatenate file1 to file 2.
cat > file1Concatenate standard input to file1.
less file1View and paginate file1.
head file1Show first 10 lines of file1.
tail file1Show last 10 lines of file1.
chmod 777 fileChange file permission of file to 777.
chown user:group fileChange ownership of file to user and group group.
chmod -R +rwx /path/to/fileChange the read, write and execute permission bits for all files and folders in the path directory.
ln -s source destinationCreate a symbolic link from source to destination.
ln -f source destinationCreate a hard link from source to destination and remove and existing links between the two files.
File Encryption and Compression
gpg2 -c fileEncrypt file.
gpg2 file.gpgDecrypt file.gpg.
tar -cf archive.tar foo barCreate archive.tar from files foo and bar.
tar -xf archive.tarExtract all files from archive.tar.
gzip file1Compress file1 and rename it to file1.gz.
gzip -d file1.gzDecompress file1.gz.
tar cvzf archive.tar.gz foo barCreate a gunzip archive named “archive.tar.gz” using the files’ foo and bar.
tar xvzf archive.tar.gzExtract a gunzip archive named “archive.tar.gz” and place the contents in the current working directory.
GPG Key Management
gpg2 –full-gen-keyCreate a new GPG keypair.
gpg2 –list-keysList 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.ascImport 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 linuxCreate a new user named “linux”, include it to the group “email” and set its default shell to bash.
groupadd -g 2222 extraCreate a new system group named “extra” with a Group ID of 2222.
usermod -G extra linuxAdd the existing “linux” user to the “extra” group.
groupmod -n super extraRename the group “extra” to “super”.
userdel linuxDelete the user “linux” in the system.
groupdel superDelete the group “super” in the system.
Text Manipulation
sed 11q sample.txtPrint the first 11 lines of the file “sample.txt”.
sed -i s/string1/string2/g sample.txtReplace every instance of the word “string1” with “string2” in the “sample.txt” file.
sed -i y/string1/strings2/g sample.txtTranspose the word “strings2” to every instance of the word “string1” in the “sample.txt” file.
uniq input.txtPrint all the identical lines of text in the “input.txt” file.
sort -r sample.txtReverse the line order of the “sample.txt” file.
sort -R sample.txtRandomize the line order of the “sample.txt” file.
sort -g sample.txtArrange the lines of the “sample.txt” file by absolute numerical order.
sort -d sample.txtArrange the lines of the “sample.txt” file by alphabetical order.
sort -M sample.txtArrange the lines of the “sample.txt” file by month order.
cut -c a,e,i,o,u sample.txtRemove all the vowel characters in the “sample.txt” file and print the result to the terminal.
cut -f field1,field2 sample.txtRemove the first two Tab-separated columns in the “sample.txt” file and print the result to the terminal.
tr “A-Za-z” “A-Z” Translate all the letters in the “sample.txt” file to uppercase.
tr -cd “[:print:]” Delete all the control characters in the “sample.txt” file.
System Commands
uname -aShow system and kernel.
unameShow distribution.
mountShow mounted filesystems.
lsblkList all currently attached block storage devices.
lsmodList all currently installed kernel modules in the system.
lspciList all currently attached PCI and PCI-E devices in the machine.
lsusbList all currently attached USB-type devices in the system.
dateShow system date.
uptimeShow system uptime.
whoamiShow your username.
usersDisplay who is online.
groupsPrint all the names of the groups that the current user is a part of.
apropos commandPrint all the matching manpages for the string “command”.
man commandShow manual for command.
info commandDisplay the appropriate info page for that particular command.
!!Repeat the last command.
exitLog out of current session.
File Searching Commands
grep pattern filesSearch for pattern in files.
grep -iCase insensitive search.
grep -rRecursive search.
grep -vInverted search.
grep -oShow matched part of file only.
locate file1Find file1.
whereis commandFind binary/source/manual for command.
which appLocate a command for the app.
look string file1Display any lines in file1 which contain string as a prefix.
Process Management
psShow snapshot of processes.
topShow real time processes.
kill pidKill process with id pid.
pkill nameKill process with name name.
killall nameKill all processes with names beginning name.
time commandRun the “command” program and print the amount of time the program ran to standard output.
Disk Space
df -hShow free space on mounted filesystems.
du -h folderShow file usage of each folder in folder.
du -hc folderShow the file usage for each folder in folder and print the total file size.
du -sh folderShow the total file size of folder.
fdisk -lShow disks partitions sizes and types (run as root).
freeShow memory and swap usage.

Image credit: Christina Morillo via Pexels

Ramces Red Avatar

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Read next

Mycorrhizal fungi colonised plant roots roughly 450 million years ago and biologists now suspect plants could never have moved out of the oceans onto bare rock without them, meaning every forest on Earth — including the redwoods, the Amazon, and the boreal belt — is still running on a partnership older than trees themselves
Suzanne Simard sealed paper birch and Douglas fir seedlings inside plastic bags, fed them carbon-14 and carbon-13 dioxide, and nine days later found carbon had crossed between species through fungal threads in the British Columbia soil beneath her boots
A species of jellyfish called Turritopsis dohrnii can revert its adult cells back to a juvenile polyp stage when injured or starving, effectively restarting its life cycle, and biologists have so far failed to identify any natural limit to how many times it can do this.
French scientist Michel Siffre spent two months alone in a cave with no clock, no calendar, and no sunlight — and when his team finally told him the experiment was over, he thought he still had nearly a month left underground
When Cingular chief Stan Sigman backed the original iPhone before its 2007 unveiling, he accepted terms American carriers usually refused: no logo on the device, no control over its software, no preloaded apps, and a share of monthly subscriber revenue flowing back to Apple, after signing on without seeing a prototype
In 2016, archaeologists dated two rings of snapped stalagmites in France’s Bruniquel Cave to 176,500 years ago, evidence that Neanderthals had walked 336 metres into darkness with fire and built architecture deep underground long before modern humans reached Europe
Otto von Bismarck was 74 when Germany adopted the world’s first national old-age social insurance program in 1889, setting the pension age at 70 after years of fighting socialists with bans, laws, and a promise few workers would live long enough to use
When cosmonaut Valeri Polyakov stepped out of his Soyuz capsule in March 1995 after 437 consecutive days aboard Mir, doctors recorded him at several centimetres above his pre-flight height, and his spine had become so unaccustomed to gravity that the recovery team carried him to a chair rather than risk the compression of letting him walk.