DEV Community

Cover image for Linux Commands Cheat Sheet
Serverspace.us
Serverspace.us

Posted on • Updated on

Linux Commands Cheat Sheet

It happens that some Linux terminal commands are hard to recall, and keeping them on your computer or paper as a cheat sheet is a good practice. This list is not exhaustive, but it includes the most commonly used commands. Feel free to add your most frequently used commands in the comments below and share this list✌️.

USERS

  • Id – Detailed information about a user (uid, gid, and group).
  • last – Lists information about recent logins, including time, username, IP address, and session duration.
  • who – Displays authorized users.
  • groupadd "testgroup" – Creates a group named "testgroup".
  • adduser NewUser – Adds a user with the name "NewUser".
  • userdel NewUser – Deletes the user with the name "NewUser".
  • usermod NewUser – Modifies information about the user "NewUser".

Directory Navigation

  • cd /- Navigate to the root directory.
  • cd - Navigate to the home directory (using the $HOME variable).
  • cd /root - Navigate to the /root directory.
  • cd .. - Move one level up.
  • cd /root/.ssh - Navigate to the hidden folder .ssh.

Working with Files

  • ls -al – Displays files and directories in the current folder.
  • pwd – Shows the current working directory.
  • mkdir NewFolder – Creates a new directory named 'NewFolder'.
  • rm NewFile – Deletes the file named 'NewFile'.
  • rm -f NewFile – Forcefully deletes the file named 'NewFile'.
  • rm -r NewFolder – Recursively deletes the directory named 'NewFolder'.
  • rm -rf NewFolder – Forcefully and recursively deletes the directory named 'NewFolder'.
  • cp oldfile1 newfile2 – Copies the content of 'oldfile1' to 'newfile2'.
  • cp -r olddir1 newdir2 – Recursively copies the directory 'olddir1' to 'newdir2'. Dir2 will be created if it doesn't exist.
  • mv oldfile1 newfile2 – Renames 'oldfile1' to 'newfile2'.
  • ln -s /etc/log/file logfile – Creates a symbolic link to the file.
  • touch newfile – Creates an empty file named 'newfile'.
  • cat > newfile – Takes STDIN and puts it into 'newfile'.
  • more newfile – Outputs the content of 'newfile' one screen at a time.
  • head newfile – Outputs the first 10 lines of the file 'newfile'.
  • tail newfile – Outputs the last 10 lines of 'newfile'.
  • gpg -c newfile – Encrypts 'newfile' in gpg format using a password and saves it in the same directory.
  • gpg newfile.gpg – Decrypts the gpg file.
  • wc newfile – Displays the count of bytes, words, and lines in the new file.

File/Directory Permissions

  • chmod 777 /root/ssh – Sets read, write, and execute permissions for everyone who has access to the server (owner, group, others).
  • chmod 755 /root/ssh – Configures permissions as rwx for the owner and r_x for the group and others.
  • chmod 766 /root/ssh – Sets rwx for the owner and rw for the group and others.
  • chown newuser newfile – Changes the owner of newfile to newuser.
  • chown newuser:newgroup newfile – Changes both the owner and group owner of newfile to newuser and newgroup.
  • chown newuser:newgroup newfolder – Changes both the owner and group owner of the directory newfolder to newuser and newgroup.
  • stat -c “%U %G” newfile – Displays the user and group owners of newfile.

Search

  • grep searchargument newfile – Searches for the searchargument in newfile.
  • grep -r searchargument newfolder – Recursively searches for the searchargument in all files within the newfolder.
  • locate newfile – Shows all locations of the newfile.
  • find /etc/ -name "searchargument" – Finds files with names starting with searchargument in the /etc directory.
  • find /etc/ -size +50000k – Finds files larger than 50000k in size in the /etc directory.

Archive

  • tar -cf archive.tar newfile – Create an archive 'archive.tar' from the file 'newfile.'
  • tar -xf archive.tar – Extract the contents of the file 'archive.tar.'
  • tar -zcvf archive.tar.gz /var/log/ – Create an archive from the /var/log/ directory and compress it using gzip.
  • gzip newfile – Compress the new file (it will have the extension .gz).

Installing Programs from Packages

  • rpm -i pkg_program.rpm – Installs an RPM package on CentOS, RHEL, etc.
  • rpm -e pkg_name – Removes an RPM package on CentOS, RHEL, etc.
  • dnf install pkg_name – Installs a package using DNF from the repository on CentOS, RHEL, etc. YUM was previously used, but it has recently been replaced by DNF.
  • dpkg -i pkg_name – Installs from a DEB package on Debian, Ubuntu, Mint, etc.
  • dpkg -r pkg_name – Removes a DEB package on Debian, Ubuntu, Mint, etc.
  • apt install pkg_name – Installs a package from the repository on Debian, Ubuntu, Mint, etc.
  • apt remove pkg_name – Removes a package on Debian, Ubuntu, Mint, etc.
  • apt upgrade && apt update – Updates the packages in the system (Debian, Ubuntu, Mint, etc.) and updates the repositories.

Processes

  • ps – Displays currently running processes.
  • ps aux | grep 'bash' – Finds the process ID (PID) of 'bash'.
  • pmap -x 11 – Maps the process with PID 11 in process memory.
  • top – Shows all running processes.
  • kill pid – Terminates a process by PID.
  • killall process – Terminates all processes with the name "process".
  • pkill process-name – Sends a signal to a process by name.
  • bg – Sends a suspended process to the background.
  • fg – Brings a running process to the foreground.
  • fg process – Brings a process named "process" to the foreground.
  • lsof – Lists files opened by processes.
  • renice 19 PID – Sets the lowest priority for a process.
  • pgrep bash – Finds the process ID for 'bash'.
  • pstree – Shows a tree-like representation of processes.

System

  • uname – Displays system information.
  • uname -r – Shows information about the Linux kernel.
  • uptime – Shows system uptime and average load.
  • hostname – Displays the host name.
  • hostname -i – Displays the host's IP address.
  • last reboot – Shows the reboot history.
  • date – Displays date and time.
  • timedatectl – Outputs and modifies date and time settings.
  • cal – Displays the calendar.
  • w – Shows users currently logged in.
  • whoami – Displays your username.
  • finger root – Shows information about the root user (requires installation with "apt-get install finger").

Hardware Commands

  • dmesg – Displays system messages during boot.
  • cat /proc/cpuinfo – Shows information about the processor.
  • cat /proc/meminfo – Displays information about the memory.
  • lshw – Shows detailed information about devices.
  • lsblk – Displays information about block devices.
  • free -m – Frees up memory: RAM and swap (switch -m for MB).
  • lspci -tv – Shows PCI device information in a tree view.
  • lsusb -tv – Displays USB devices in a tree view.
  • dmidecode – Shows information about BIOS devices.
  • hdparm -i /dev/xda – Displays information about the disk.
  • hdparm -tT /dev/xda – Shows read and write speed of xda.
  • badblocks -s /dev/xda – Performs a test for bad sectors.

Disc Management Commands

  • df -h – Shows free space on mounted partitions (in bytes).
  • df -i – Displays free inodes in the file system.
  • fdisk -l – Provides information about the disk, partitions, and file system.
  • du -sh – Shows undistributed space on mounted partitions in MB, GB, TB.
  • findmnt – Displays all mount points.
  • mount /dev/sdb1 /mnt – Mounts partition 1 of sdb disk to the /mnt directory.

Network

  • ip addr show – Displays the IP addresses of all available network interfaces.
  • ip address add 192.168.0.1/24 dev eth0 – Assigns the address 192.168.0.1 to the eth0 interface.
  • ifconfig – Shows the IP addresses of all available network interfaces.
  • ping 192.168.0.1 – Sends an ICMP protocol request to connect to the node at 192.168.0.1.
  • whois domain – Displays information about the domain name.
  • *dig domain *– Retrieves DNS information about the domain.
  • *dig -x 192.168.0.1 *– Performs reverse DNS resolution.
  • host serverspace.us– Resolves the host address.
  • *hostname -I *– Shows local addresses.
  • wget file_name(link to file) – Downloads a file.
  • *netstat -pnltu *– Displays all ports being listened to on the host (requires "apt-get install net-tools").

Remote Connection

  • ssh root@host – Connects to a remote host via ssh as the root user.
  • ssh -p port_number user@host – Connects to a remote host using a non-default ssh port, specifying the user.
  • ssh host – Utilizes the default connection using the current user.
  • telnet host – Uses a telnet connection (port 23).

Serverspace is an international cloud provider offering automatic deployment of virtual infrastructure based on Linux and Windows from anywhere in the world in less than 1 minute. For the integration of client services, open tools like API, CLI, and Terraform are available.

Top comments (23)

Collapse
 
joolsmcfly profile image
Julien Dephix • Edited

Hi.

A had a quick glance at the list I have the following suggestions for you:

Id – Detailed information about a user (uid, gid, and group).

It should be id.

cd - Navigate to the home directory (using the $HOME variable).

You probably meant cd ~.

ls -al – Displays files and directories in the current folder.

Add h to have human readable sizes! (ls -lah)

There's a command I find myself using from time to time:
du --max-depth=1 -h which lists sizes of files and folders with, you guessed it, max depth of 1. Useful when you want to find out which folder takes most disk space.

Collapse
 
danlee1996 profile image
Daniel Lee

the cd command without any arguments work exactly like cd ~.

Collapse
 
joolsmcfly profile image
Julien Dephix

TIL. Thanks!

cd - is also very helpful to go back and forth between two folders.

Collapse
 
olexy profile image
Scarlet Olexy

Didn't know that, thanks

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I don't understand why people still use chmod like that. It's fine if you want to put it in a script, or even in a manual type thing. But for everyday stuff, it's just way easier to do for example chmod o+rx.

Collapse
 
teamradhq profile image
teamradhq

Because 3 octals are easier to understand.

chmod 777 is equal to `ugo+rwx

chmod 750 is equal to chmod u+rwx,g+rx,o-rwx

One of them seems much simpler to me…

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

When you want to set the exact permissions of a file, maybe, but most of the time people just want to do "let me execute this" or don't let others even read this", and you just cannot tell me that the average user finds it easier to just assemble the exact numeric code of the current permissions with that change applied on the fly as they type the command.

Thread Thread
 
teamradhq profile image
teamradhq • Edited

Each to their own I suppose.

I don’t think you can quantify that most of the time ppl just want to execute something they do so in a way that adheres to your personal preferences.

Personally, if I want to let others read something, I’ll chmod 640 or 644 for that. For me, three octals are much easier to type and remember than the dozens of characters required to achieve the same result.

Also, it’s not a numeric code. It’s a set of 3 octals that assign rwx permissions to owner, group and all. An octal is not a number, it’s a set of three bits 7 = 111, 6 = 110, 5 = 101, etc. The digit just represents the order of bits.

This is just base 2, which is pretty fundamental to computing. It’s really not hard to count from 0 to 7 in binary. My seven year old figured it out in about five minutes, so I’d suggest the average terminal user could do the same. Certainly much quicker than determining the exact collection and combination of letters and symbols required to provide 754 permissions.

Perhaps you should do some reading on the topic to shore up the clear gaps in your understanding. :)

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I don't see where that condescending conclusion comes from, but I think you're conflating what is a good way of teaching chmod and how you and everyone else should be using it.

I'm in your exact shoes with colours in hexadecimal; They're easiest to type if you've spent way too much time using them and can just write and interpret them as any other notation, but I would never put it as the default in any tutorial.

Most users of a tool won't be using it often enough to memorise these things, and a more mnemonic notation like g+w to say "add write to group" will be quicker to learn and easier to remember when you need it.

This post was supposed to be a cheat-sheet, after all.

Collapse
 
kaamkiya profile image
Kaamkiya

This is awesome! I personally use tldr, because it's easy to access from the terminal. This is a fantastic sheet, though. Also, ls -al displays the verbose output for files (date, author, update, etc), while ls just displays the files.

Collapse
 
greenteaisgreat profile image
Nathan G Bornstein

Holy heck, thanks for the lead to tldr; I never knew I needed that in my life until now

Collapse
 
semo profile image
semo

Cool. I use each command. Good to know I left the early admin stages.

Collapse
 
teyim profile image
Teyim Asobo

nice

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

Very good list.

A command I use almost daily is grep -rnI "thing I want to find" to search though a directory recursively but Ignoring binary files and giving me line numbers. I also add --exclude-dir=.svn because searching through the SVN stuff is pointless.

Collapse
 
moopet profile image
Ben Sinclair

Some of these are problematic, e.g.:

cd - Navigate to the home directory (using the $HOME variable)

This is correct, but the way you say it makes it look like cd - which acts to toggle you between the current and previous directories.

You mix adduser with userdel and usermod when there's a useradd command but don't explain why.

You should explain what you mean by "forecefully".

touch newfile – Creates an empty file named 'newfile'.

This is incorrect. touch newfile updates one of the timestamps on a file called newfile. If newfile doesn't exist, it is created as a side effect, but that's not what the command does.

find /etc/ -name "searchargument" – Finds files with names starting with searchargument in the /etc directory.

This finds files and directories with the exact name "searchargument".

Collapse
 
cappe987 profile image
Casper

ifconfig is deprecated and ip command should be used instead.

Collapse
 
tontonastro profile image
tontonastro

Tonton Astro Ria for live episodes of Malay dramas and Malaysian telefilms 2024.
Explore Drama Melayu free online 2023 and enjoy free series online dfm2u hd today.

Tonton Astro Ria

Collapse
 
abhixsh profile image
Abishek Haththakage

Really important

Some comments may only be visible to logged-in visitors. Sign in to view all comments.