Linux Commands Cheat Sheet
Tags: linux, cheatsheet, programming, beginners
Published: 2023-12-13 14:14:55

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✌️.

<a href="http://serverspace.us?utm_source=devto&utm_medium=articles&utm_campaign=main"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3i5ukd2vvi8bst6tohi.png"></a>

## 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.

<a href="http://serverspace.us?utm_source=devto&utm_medium=articles&utm_campaign=main"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3i5ukd2vvi8bst6tohi.png"></a>

## 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](http://serverspace.us?utm_source=devto&utm_medium=articles&utm_campaign=main) is an international cloud provider offering automatic deployment of [virtual infrastructure](http://serverspace.us?utm_source=devto&utm_medium=articles&utm_campaign=main) 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._