• United States
Unix Dweeb

The Linux command-line cheat sheet

Feb 05, 201911 mins

This select set of Linux commands can help you master the command line and speed up your use of the operating system.

crib sheet cheat sheet answers tests notes
Credit: Toonzzz / Getty

When coming up to speed as a Linux user, it helps to have a cheat sheet that can help introduce you to some of the more useful commands.

In the 18 tables below, you’ll find sets of commands with simple explanations and usage examples that might help you or Linux users you support become more productive on the command line.

Getting familiar with your account

These commands will help new Linux users become familiar with their Linux accounts.

Command Function Example
pwd Displays your current location in the file system pwd
whoami Displays your username – most useful if you switch users with su and need to be reminded what account you’re using currently whoami
ls Provides a file listing. With -a, it also displays files with names starting with a period (e.g., .bashrc). With -l, it also displays file permissions, sizes and last updated date/time. ls ls -a ls -l
env Displays your user environment settings (e.g., search path, history size, home directory, etc.) env
echo Repeats the text you provide or displays the value of some variable echo hello echo $PATH
history Lists previously issued commands history history | tail -5
passwd Changes your password. Note that complexity requirements may be enforced. passwd history | tail -5

See more commands below this ad.

Examining files

Linux provides several commands for looking at the content and nature of files. These are some of the most useful commands.

Command Function Example
cat Displays the entire contents of a text file. cat .bashrc
more Displays the contents of a text file one screenful at a time. Hit the spacebar to move to each additional chunk. more .bash_history
less Displays the contents of a text file one screenful at a time, but in a manner that allows you to back up using the up arrow key. less .bash_history
file Identifies files by type (e.g., ASCII text, executable, image, directory) file myfile file ~/.bashrc file /bin/echo


Managing files

These are some Linux commands for changing file attributes as well as renaming, moving and removing files.

Command Function Example
chmod Changes file permissions (who can read it, whether it can be executed, etc.) chmod a+x myscript chmod 755 myscript
chown Changes file owner sudo chown jdoe myfile
cp Makes a copy of a file. cp origfile copyfile
mv Moves or renames a file – or does both mv oldname newname mv file /new/location mv file /newloc/newname
rm Deletes a file or group of files rm file rm *.jpg rm -r directory


Creating and editing files

Linux systems provide commands for creating files and directories. Users can choose the text editor they are comfortable using. Some require quite a bit of familiarity before they’ll be easy to use while others are fairly self-explanatory.

Command Function Example
nano An easy-to-use text editor that requires you to move around in the file using your arrow keys and provides control sequences to locate text, save your changes, etc. nano myfile
vi A more sophisticated editor that allows you to enter commands to find and change text, make global changes, etc. vi myfile
ex A text editor designed for programmers and has both a line-oriented and visual mode ex myfile
touch Creates a file if it doesn’t exist or updates its timestamp if it does touch newfile touch updatedfile
> Creates files by directing output to them. A single > creates a file while >> appends to an existing file. cal > calendar ps > myprocs date >> date.log
mkdir Creates a directory mkdir mydir mkdir ~/mydir mkdir /tmp/backup


Moving around the file system

The command for moving around the Linux file system is ls, but there are many variations.

Command Function Example
cd With no arguments, takes you to your home directory. The same thing would happen if you typed cd $HOME or cd ~ cd
cd .. Moves up (toward /) one directory from your current location cd ..
cd Takes you to the specified location. If the location begins with a /, it is taken to be relative to the root directory; otherwise it is taken as being relative to your current location. The ~ character represents your home directory. cd /tmp cd Documents cd ~/Documents


Learning about and identifying commands

There are a number of Linux commands that can help you learn about other commands, the options they offer and where these commands are are located in the file system. Linux systems also provide a command that can help you to learn what commands are available related to some subject – for example, commands that deal with user accounts.

Command Function Example
man Displays the manual (help) page for a specified command and (with -k) provides a list of commands related to a specified keyword man cd man -k account
which Displays the location of the executable that represents the particular command which cd
apropos Lists commands associated with a particular topic or keyword apropos user apropos account


Finding files

There are two commands that can help you find files on Linux, but they work very differently. One searches the file system while the other looks through a previously built database.

Command Function Example
find Locates files based on criteria provided (file name, type, owner, permissions, size, etc.). Unless provided with a location from which to start the search, find only looks in the current directory. find . -name myfile find /tmp -type d
locate Locates files using the contents of the /var/lib/mlocate/mlocate.db which is updated by the updatedb command usually run through cron. No starting location is required. locate somefile locate “*.html” -n 20


Viewing running processes

You can easily view processes that are running on the system – yours, another user’s or all of them.

Command Function Example
ps Shows processes that you are running in your current login session ps
ps -ef Shows all processes that are currently running on the system ps -ef ps -ef | more
pstree Shows running processes in a hierarchical (tree-like) display that demonstrates the relationships between processes (-h highlights current process) pstree pstree username pstree -h


Starting, stopping and listing services

These commands allow you to display services as well as start and stop them.

Command Function Example
systemctl The systemctl command can start, stop, restart and reload services. Privileged access is required. sudo systemctl stop apache2.service sudo systemctl restart apache2.service sudo systemctl reload apache2.service
service Lists services and indicates whether they are running service –status-all


Killing processes

Linux offers a few commands for terminating processes. Privileged access is needed if you did not start the process in question.

Command Function Example
kill Terminates a running process provided you have the authority to do so kill 8765 sudo kill 1234 kill -9 3456
killall Terminates all processes with the provided name killall badproc
pkill Terminates a process based on its name pkill myproc


Identifying your OS release

The table below lists commands that will display details about the Linux OS that is running on a system.

Command Function Example
uname Displays information on OS release in a single line of text uname -a uname -r
lsb_release On Debian-based systems, this command displays information on the OS release including its codename and distributor ID lsb_release -a
hostnamectl Displays information on the system including hostname, chassis type, OS, kernel and architecture hostnamectl


Gauging system performance

These are some of the more useful tools for examining system performance.

Command Function Example
top Shows running processes along with resource utilization and system performance data. Can show processes for one selected user or all users. Processes can be ordered by various criteria (CPU usage by default) top top jdoe
atop Similar to top command but more oriented toward system performance than individual processes atop
free Shows memory and swap usage – total, used and free free
df Display file system disk space usage df df -h


Managing users and groups

Commands for creating and removing user accounts and groups are fairly straightforward.

Command Function Example
useradd Adds a new user account to the system. A username is mandatory. Other fields (user description, shell, initial password, etc.) can be specified. Home directory will default to /home/username. useradd -c “John Doe” jdoe useradd -c “Jane Doe” -g admin -s /bin/bash jbdoe
userdel Removes a user account from the system. The -f option runs a more forceful removal, deleting the home and other user files even if the user is still logged in. userdel jbdoe userdel -f jbdoe
groupadd Adds a new user group to the system, updating the /etc/group. groupadd developers
groupdel Removes a user group from the system groupdel developers


Examining network connections

The commands below help you view network interfaces and connections.

Command Function Example
ip Displays information on network interfaces ip a
ss Displays information on sockets. The -s option provides summary stats. The -l option shows listening sockets. The -4 or -6 options restrict output to IPv4 or IPv6 connections. ss -s ss -l ss -4 state listening
ping Check connectivity to another system ping remhost ping


Managing security

There are many aspects to managing security on a Linux system, but there are also a lot of commands that can help. The commands below are some that will get you started. Click on this link to see these and other commands on 22 essential Linux security commands.

Command Function Example
visudo The visudo command allows you to configure privileges that will allow select individuals to run certain commands with superuser authority. The command does this by making changes to the /etc/sudoers file. visudo
sudo The sudo command is used by privileged users (as defined in the /etc/sudoers file to run commands as root. sudo useradd jdoe
su Switches to another account. This requires that you know the user’s password or can use sudo and provide your own password. Using the means that you also pick up the user’s environment settings. su (switch to root) su – jdoe sudo su – jdoe
who Shows who is logged into the system who
last Lists last logins for specified user using records from the /var/log/wtmp file. last jdoe
ufw Manages the firewall on Debian-based systems. sudo ufw status sudo ufs allow ssh ufw show
firewall-cmd Manages the firewall (firewalld) on RHEL and related systems. firewall-cmd –list-services firewall-cmd –get-zones
iptables Displays firewall rules. sudo iptables -vL -t security


Setting up and running scheduled processes

Tasks can be scheduled to run periodically using the command listed below.

Command Function Example
crontab Sets up and manages scheduled processes. With the -l option, cron jobs are listed. With the -e option, cron jobs can be set up to run at selected intervals. crontab -l crontab -l -u username crontab -e
anacron Allows you to run scheduled jobs on a daily basis only. If the system is powered off when a job is supposed to run, it will run when the system boots. sudo vi /etc/anacrontab


Updating, installing and listing applications

The commands for installing and updating applications depend on what version of Linux you are using, specifically whether it’s Debian- or RPM-based.

Command Function Example
apt update On Debian-based systems, updates the list of available packages and their versions, but does not install or upgrade any packages sudo apt update
apt upgrade On Debian-based systems, installs newer versions of installed packages sudo apt upgrade
apt list Lists all packages installed on Debian-based system. With –upgradable option, it shows only those packages for which upgrades are available. apt list apt list –installed apt list –upgradable
apt install On Debian-based systems, installs requested package sudo apt install apache2
yum update On RPM-cased systems, updates all or specified packages sudo yum update yum update mysql
yum list On RPM-based systems, lists package sudo yum update mysql
yum install On RPM-based systems, installs requested package sudo yum -y install firefox
yum list On RPM-based systems, lists known and installed packages sudo yum list sudo yum list –installed


Shutting down and rebooting

Commands for shutting down and rebooting Linux systems require privileged access. Options such as +15 refer to the number of minutes that the command will wait before doing the requested shutdown.

Command Function Example
shutdown Shuts down the system at the requested time. The -H option halts the system while the -P powers it down as well. sudo shutdown -H now shutdown -H +15 shutdown -P +5
halt Shuts down the system at the requested time. sudo halt sudo halt -p sudo halt –reboot
poweroff Powers down the system at the requested time. sudo shutdown -H now sudo shutdown -H +15 sudo shutdown -P +5


Wait, wait, there’s more!

Remember to consult the man pages for more details on these commands. A cheat sheet provides only a quick explanation and a handful of command examples to help you get started.

Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author