Conquering the Command Line: Your Essential Linux Cheat Sheet

Conquering the Command Line: Your Essential Linux Cheat Sheet

This cheat sheet equips you with most of the commands used in Linux terminal, often referred to as the command line to manage files, directories, processes and many more. This might seem intimidating at first glance. But fear not! With a handful of essential commands, you can navigate the digital landscape of Linux with confidence.

Keyboard Shortcuts

Moving the cursor

DESCRIPTIONCOMMAND
Go to the beginning of the line (Home).Ctrl + A
Go to the End of the line (End).Ctrl + E
Previous command (Up arrow).Ctrl + P
Next command (Down arrow).Ctrl + N
Back (left) one word.Alt + B
Forward (right) one word.Alt + Z
Forward one character.Ctrl + F
Backward one character.Ctrl + B
Toggle between the start of line and current cursor position.Ctrl + XX

Editing

DESCRIPTIONCOMMAND
Clear the Screen, similar to the clear command.Ctrl + L
Delete the Word before the cursor.Alt + Del
Delete the Word after the cursor.Alt + D
Delete character under the cursor.Ctrl + D
Delete character before the cursor (Backspace).Ctrl + H
Cut the Word before the cursor to the clipboard.Ctrl + W
Cut the Line after the cursor to the clipboard.Ctrl + K
Cut/delete the Line before the cursor to the clipboard.Ctrl + U
Swap current word with previous.Alt + T
Swap the last two characters before the cursor (typo).Ctrl + T
Swap the last two words before the cursor.Esc + T
Paste the last thing to be cut (yank).Ctrl + Y
UPPER capitalize every character from the cursor to the end of the current word.Alt + U
Lower the case of every character from the cursor to the end of the current word.Alt + l
Capitalize the character under the cursor and move to the end of the word.Alt + c
Cancel the changes and put back the line as it was in the history (revert).Alt + r
Undo.ctrl + _
Tab completion for file/directory names.[TAB]

Special keys:

Text Terminals send characters (bytes), not key strokes. Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters which are not printable, they display in the terminal as ^ and are intended to have an effect on applications.

DESCRIPTIONCOMMAND
[TAB]Ctrl+ I
NewlineCtrl+ J
EnterCtrl+ M
EscapeCtrl+ [
^@Ctrl+ 2
^[ → EscapeCtrl+ 3
^\Ctrl+ 4
^]Ctrl+ 5
^^Ctrl+ 6
^_ → UndoCtrl+ 7
^? → Backward-delete-charCtrl+ 8

Ctrl+ V tells the terminal to not interpret the following character,
so <Ctrl + V Ctrl + I > will display a tab character,
similarly Ctrl + V ENTER will display the escape sequence for the Enter key: ^M

History

DESCRIPTIONCOMMAND
Recall the last command including the specified character(s). Search the command history as you type. Equivalent to : vim ~/.bash_history.Ctrl + R
Previous command in history (walk back).Ctrl + P
Next command in history (walk forward).Ctrl + N
Go back to the next most recent command.
(beware to not execute it from a terminal because this will also launch its XOFF).Ctrl + S
Execute the command found via Ctrl+r or Ctrl+sCtrl + O
Escape from history searching mode.Ctrl + G
Run last command.!!
Repeat from the last command: args n e.g. !:2 for the second argument.!n
Repeat from the last command: args from n to m. e.g. !:2-3 for the second and third.!n:m
Repeat from the last command: args n to the last argument.!n:$
Print last command starting with n.!n:p
Print the last command beginning with string.!string
Quote the last command with proper Bash escaping applied.
Tip: enter a line of Bash starting with a # comment, then run
!:q on the next line to escape it.!:q
Last argument of previous command.!$
Last argument of previous command.ALT + .
All arguments of previous command.!*
Run previous command, replacing abc with def^abc^def

Process control

DESCRIPTIONCOMMAND
Interrupt/Kill whatever you are running (SIGINT).Ctrl + C
Clear the screen.Ctrl + l
Stop output to the screen (for long running verbose commands).
Then use PgUp/PgDn for navigation.Ctrl + S
Allow output to the screen (if previously stopped using command above).Ctrl + Q
Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT).Ctrl + D
Send the signal SIGTSTP to the current task, which suspends it.
To return to it later enter fg 'process name' (foreground).Ctrl + Z

Users and Groups

DESCRIPTIONCOMMAND
See details about the active users.id
Show the last system logins.last
Display who is currently logged into the system.who
Show which users are logged in and their activity.w
Display information about all the users currently logged into the system, including their usernames, login time, and terminal.finger
Create a new user account on the system with the specified <username>.sudo useradd <username>
Delete a user accountsudo userdel [user_name]
Modify user information (add a user to a group)sudo usermod -aG [group_name] [user_name]
Change the current user's or another user's password.sudo passwd [user_name]
Add a new group.sudo groupadd [group_name]
Delete a group.sudo groupdel [group_name]
Modify a user group (change group name).sudo groupmod -n [new_name] [old_name]
Temporarily elevate user privileges to superuser or root.sudo [command]
Switch the user account or become a superuser.su - [user_name]
Change file or directory group.chgrp [group_name] [file/ directory]

Files

DESCRIPTIONCOMMAND
Create a new directory.mkdir [directory_name]
Remove a file.rm [file_name]
Remove a directory recursively.(deletes contents also.)rm -r [directory_name]
Recursively remove a directory without requiring confirmation.rm -rf [directory_name]
Copy the contents of one file to another file.cp [source_file] [destination_file]
Recursively copy a directory to a second directory.cp -r [source_directory] [destination_directory]
Move or rename files or directories.mv [source_file] [destination_file]
Create a symbolic link to a file.ln -s [path]/[file_name] [link_name]
Create a new file.touch [file_name]
Show the contents of a file.cat [file_name]
Append file contents to another file.cat [source_file] >> [destination_file]
Show the first ten lines of a file.head [file_name]
Show the last ten lines of a file.tail [file_name]
Display contents of a file page by page.more [file_name]
Show the contents of a file with navigation.less [file_name]
Open or create a file using the nano text editor.nano [file_name]
Open or create a file using the Vi/Vim text editor.vi [file_name] vim [file_name]
Encrypt a file.gpg -c [file_name]
Decrypt an encrypted .gpg file.gpg [file_name].gpg
Show the number of words, lines, and bytes in a file.wc -w [file_name]
List the number of lines/ words/characters in each file in a directory.ls I xargs wc
Cut a section of a file and print the result to standard output.cut -d [delimiter] [file_name]
Cut a section of piped data and print the result to standard output.[data] I cut -d [delimiter]
Overwrite a file to prevent its recovery, then delete it.shred -u [file_name]
Compare two files and display differences.diff [first_file] [second_file]
Read and execute the file content in the current shell.source [file_name]
Store the command output in a file and skip the terminal output.[command] I tee [file_name] >/dev/null

System Management

DESCRIPTIONCOMMAND
Show system information via uname command.uname -r
See kernel release information.uname -a
Display how long the system has been running, including the load average.uptime
View system hostname.hostname
Show the IP address of the system.hostname -i
List system reboot history.last reboot
See current time and date.date
Query and change the system clock.timedatectl
Show current calendar (month and day).cal
List logged-in users.w
See which user you are using.whoami
Show information about a particular user.finger [user_name]
View or limit system resource amounts.ulimit [flags] [limit]
Schedule a system shutdown.shutdown [hh:mm]
Shut down the system immediately.shutdown now
Add a new kernel module.modprobe [module_name]
Show bootup messages.dmesg

Processes

DESCRIPTIONCOMMAND
List active processes.ps
Show processes in a treelike diagram.pstree
Display a memory usage map of processes.pmap
See all running processes.top
Interactive and colorful process viewer.htop
Terminate a Linux process under a given ID.kill [process_id]
Terminate a process under a specific name.pkill [process_name]
Terminate all processes with a given label.killall [label]
List processes based on the provided keyword.prgrep [keyword]
Show the PID of a process.pidof [process_name]
List and resume stopped jobs in the background.bg
Bring the most recently suspended job to the foreground.fg
Bring a particular job to the foreground.fg [job]
List files opened by running processes.lsof
Catch a system error signal in a shell script. Executes provided commands when the signal is caught.trap "[commands]" [signal]
Pause the terminal or a Bash script until a running process is completed.wait
Run a Linux process in the background.nohup [command] &

Searching

DESCRIPTIONCOMMAND
Find files and directories that match the specified pattern in a specified location.find [path] -name [search_pattern]
See files and directories larger than a specified size in a directory.find [path] -size [+100M]
Search for a specific pattern in a file with grep.grep [search_pattern] [file_name]
Recursively search for a pattern in a directory.grep -r [search_pattern] [directory_name]
Locate all files and directories related to a particular name.locate [name]
Search the command path in the $PATH environment variable.which [command]
Find the source, binary, and manual page for a command.whereis [command]
Print all lines matching a pattern in a file. See also the gawk command, the GNU version of awk.awk '[search_pattern] {print $0}' [file_name]
Find and replace text in a specified file.sed 's/[old_text]/ [new_text]/' [file_name]

SSH Login

DESCRIPTIONCOMMAND
Connect to a remote host as a user via SSH.ssh [user_name]@[host]
Securely connect to a host via SSH default port 22.ssh [host]
Connect to the host using a particular port.ssh -p [port] [user_name]@[host]
Generate SSH key pairs.ssh-keygen
Start SSH server daemon.sudo service sshd start
Securely copy files between local and remote systems via SSH.scp [file_name] [user_name]@[host]:[rem ote_path]
Interactive file transfer over encrypted SSH session using SFTP protocol.sftp [user_name]@[host]
Connect to the host via Telnet default port 23telnet [host]

Network

DESCRIPTIONCOMMAND
List IP addresses and network interfaces.ip addr show
Assign an IP address to interface eth0.ip address add [IP_address]
Display IP addresses of all network interfaces.ifconfig
Ping remote host.ping [remote_host]
See active (listening) ports with the netstat command.netstat -pnltu
Show TCP and UDP ports and their programs.netstat -tuln
Display more information about a domain.whois [domain_name]
Show DNS information about a domain using the dig command.dig [domain_name]
Do a reverse DNS lookup on the domain.dig -x [domain_name]
Do a reverse DNS lookup of an IP address.dig -x [IP_address]
Perform an IP lookup for a domain.host [domain_name]
Show the local IP address.hostname -I
Receive information about an internet domain.nslookup [domain_name]

Hardware Information

DESCRIPTIONCOMMAND
See CPU information.lscpu
See information about block devices.lsblk
Show PCI devices in a treelike diagram.lspci -tv
Display USB devices in a tree-like diagram.lsusb -tv
List hardware configuration information.lshw
Show detailed CPU information.cat /proc/cpuinfo
View detailed system memory information.cat /proc/meminfo
See mounted file systems.cat /proc/mounts
Display free and used memory.free -h
Show hardware information from the BIOS.sudo dmidecode
Display disk data information.hdparm -i /dev/ [device_name]
Conduct a read speed test on the device/disk.hdparm -tT /dev/ [device_name]
Test for unreadable blocks on the device/disk.badblocks -s /dev/ [device_name]
Run a disk check on an unmounted disk or partition.fsck /dev/[device_name]

Directory Navigation

DESCRIPTIONCOMMAND
List files and directories in the current directory.ls
List all files and directories in the current directory (shows hidden files).ls -a
List files and directories in long format.ls -l
Show the directory you are currently working in.pwd
Change directory to $HOME.cd ~
Move up one directory level.cd ..
Change to the previous directory.cd -
Change location to a specified directory.cd [directory_path]
Show current directory stack.dirs

File Permissions

What are octal values?

When Linux file permissions are represented by numbers, it's called numeric mode. In numeric mode, a three-digit value represents specific file permissions (for example, 744.) These are called octal values. The first digit is for owner permissions, the second digit is for group permissions, and the third is for other users. Each permission has a numeric value assigned to it:

  • r (read): 4

  • w (write): 2

  • x (execute): 1

In the permission value 744, the first digit corresponds to the user, the second digit to the group, and the third digit to others. By adding up the value of each user classification, you can find the file permissions.

For example, a file might have read, write, and execute permissions for its owner, and only read permission for all other users. That looks like this:

  • Owner: rwx = 4+2+1 = 7

  • Group: r-- = 4+0+0 = 4

  • Others: r-- = 4+0+0 = 4

The results produce the three-digit value 744.

DESCRIPTIONCOMMAND
Assign read, write, and execute file permission to everyone (rwxrwxrwx).chmod 777 [file_name]
Give read, write, and execute permission to owner, and read and execute permission to group and others (rwxr-xr-x).chmod 755 [file_name]
Assign full permission to the owner, and read and write permission to the group and others (rwxrw-rw-).chmod 766 [file_name]
Change the ownership of a file with chown command.chown [user_name] [file_name]
Change the owner and group ownership of a file.chown [user_name]:[group_name] [file_name]

Disk Usage

DESCRIPTIONCOMMAND
Check free and used space on mounted systems.(human-readable format)df -h
Show free inodes on mounted file systems.df -i
Display disk partitions, sizes, and types with the command.fdisk -l
See disk usage for all files and directories.du -ah
Show disk usage of the current directory.du -sh
Show currently mounted file systems.mount
Display target mount point for all file systems.findmnt
Mount a device.mount [device_path] [mount_point]

File Compression

DESCRIPTIONCOMMAND
Archive an existing file or directory.tar cf [archive.tar] [file/ directory]
Extract an archived file.tar xf [archive.tar]
Create a .gz compressed tar archive.tar czf [archive.tar.gz]
Compress or decompress .gz files respectively.gzip [file_name] gunzip [file_name.gz]
Compress or decompress .bz2 files respectively.bzip2 [file_name] bunzip2 [file_name.bz2]
Creates a zip archive named “archive.zip” containing files “file1.txt” and “file2.txt”.zip archive.zip file1.txt file2.txt

Shell Commands

DESCRIPTIONCOMMAND
Create an alias for a command.alias [aliasname]='[ command]'
Set a custom interval to run a user-defined command.watch -n [interval-inseconds] [command]
Postpone the execution of a command.sleep [time-interval] && [command]
Create a job to be executed at a certain time (Ctrl+D to exit prompt after command).at [hh:mm]
Display a built-in manual for a command.man [command]
Print the command history used in the terminal.history

Packages (Universal)

DESCRIPTIONCOMMAND
Install software from source code.tar zxvf [file_name.tar.gz] cd [extracted_directory] ./configure make install
Install a Snap package.sudo snap install [package_name]
Search for a package in the Snap store.sudo snap find [keyword]
List installed Snap packages.sudo snap list
Install a Flatpak package.flatpak install [package_name]
Search for a Flatpak application in repositories.flatpak search [keyword]
List installed Flatpack packages.flatpak list

Packages (Debian/Ubuntu)

DESCRIPTIONCOMMAND
Install an APT package using the apt-get package utility.sudo apt-get install [package_name]
Install an APT package using a newer APT package manager.sudo apt install [package_name]
Search for a package in the APT repositories.apt search [keyword]
List packages installed with APT.apt list
Show information about a package.apt show [package_name]
Install a .deb package with the Debian package manager.sudo dpkg -i [package_name.deb]
List packages installed with dpkg.sudo dpkg -l

Packages (Red Hat, CentOS, Fedora)

DESCRIPTIONCOMMAND
Install a package using the YUM package manager.sudo yum install [package_name]
Find a package in the YUM repositories based on the provided keyword.yum search [keyword]
List all packages installed with YUM.yum list installed
Show package information for a package.yum info [package_name]
Install a package using the DNF package manager.sudo dnf install [package_name]
Install a .rpm package from a local file.sudo rpm -i [package_name.rpm]

File Transfer

DESCRIPTIONCOMMAND
Copy a file to a server directory securely.scp [source_file] [user]@[remote_host]:[de stination_path]
Synchronize the contents of a directory with a backup directory.rsync -a [source_directory] [user]@[remote_host]:[destination_directory]
Download files from FTP or web servers.wget [link]
Transfer data to or from a server with various protocols.curl -O [link]
Transfer files between local and remote systems interactively using FTP.ftp [remote_host]
Securely transfer between local and remote hosts using SFTP.sftp [user]@[remote_host]

Variables

DESCRIPTIONCOMMAND
Assign an integer value to a variable.let "[variable_name]=[value]"
Export a Bash variable.export [variable_name]
Declare a Bash variable.declare [variable-name]= "[value]"
List the names of all the shell variables and functions.set
Remove an environment variable.unset [variable_name]
Display the value of a variable.echo $[variable-name]