Americas

  • United States
sandra_henrystocker
Unix Dweeb

Getting started on the Linux (or Unix) command line, Part 3

How-To
Nov 21, 20236 mins
Linux

Our Linux cheat sheet includes some of the most commonly used commands along with brief explanations and examples of what the commands can do.

Learning, computer and training business interns in night office with manager, boss and leadership help. Men with technology planning ideas, kpi strategy and marketing innovation vision with mentor
Credit: PeopleImages.com - Yuri A / Shutterstock

One of the things you need when building your “chops” on Linux is a command vocabulary that will allow you to do all of the things that you need to do on the command line. One of the best ways to do this is to start with a “cheat sheet” – a list of the commonly used commands with brief explanations and examples of what the commands can do. A cheat sheet can help you to become familiar with commands you might not even have known were available.

This post provides an intro Linux cheat sheet. It includes a series of commands with descriptions and examples and categorizes them in groups by their primary focus. Keep in mind that, while most commands on Linux will work the same from one distribution to the next, some commands – like those used to install manage packages (tools, programming languages, etc.) – will differ from one distro to another.

A brief cheat sheet

The tables below provides information on some of the most basic Linux commands.

Getting familiar with your account

Command Function Example
pwd display current file system location pwd
whoami display username whoami
who list logged in users who
ls list files in current location ls; ls -l; ls -a
env display settings (search path, command history …) env
echo repeat the argument provided echo hello; echo $PATH
history list recently used commands history; history | tail -5
passwd reset your password passwd

Looking at text file content

Command Function Example
cat display file content cat myfile
more view file content a page at a time more myfile
less display file content a page at a time, allows backing up less myfile
head show top of file head myfile; head -5 myfile
tail show bottom of file tail myfile; tail -3 myfile

Managing your files

Command Function Example
chmod change file permissions chmod a+x myscript; chmod 700 myscript
cp make a copy of a file cp myfile savefile
mv rename a file or move to another location mv file oldfile; mv script bin
rm delete a file rm oldfile

View running processes

Command Function Example
ps show processes associated with your current login ps
ps -ef show processes running on the system ps -ef; ps -ef | more

Command output

The examples in this section provide sample output for many of the commands described in the tables above.

env

The env command provides a lot of information on your command environment. This includes the shell assigned to your account, how many of the commands you run will be maintained in your command history (HISTSIZE), your default text editor, your current file system location, terminal type and most recent former file system location as well as other data.

$ env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=fedora
EDITOR=/usr/bin/nano
PWD=/home/justme
LOGNAME=justme
HOME=/home/justme
LANG=en_US.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;37;41:su=37;41:sg=30;43:ca=00:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.avif=01;35:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:*~=00;90:*#=00;90:*.bak=00;90:*.old=00;90:*.orig=00;90:*.part=00;90:*.rej=00;90:*.swp=00;90:*.tmp=00;90:*.dpkg-dist=00;90:*.dpkg-old=00;90:*.ucf-dist=00;90:*.ucf-new=00;90:*.ucf-old=00;90:*.rpmnew=00;90:*.rpmorig=00;90:*.rpmsave=00;90:
TERM=xterm
LESSOPEN=||/usr/bin/lesspipe.sh %s
USER=justme
SHLVL=1
DEBUGINFOD_URLS=https://debuginfod.fedoraproject.org/
XDG_DATA_DIRS=/home/justme/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
PATH=/home/justme/.local/bin:/home/justme/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
MAIL=/var/spool/mail/justme
_=/usr/bin/env
OLDPWD=/tmp

echo

The echo command will display content you provide and variables you include, but it can also be used to add lines to existing files.

$ echo date: `date`
date: Tue Nov 20 02:53:43 PM EST 2023

Note that including command output in an echo command requires using backticks to run the command rather than just display its name. To add the generated output to a file, you would use a command like this:

$ echo date: `date` > myreport
$ cat myreport
date: Tue Nov 20 03:06:33 PM EST 2023

Keep in mind that > overwrites and former file content. Use >> to append to an existing file.

history

The history command shows recent commands you have run – as many as your $HISTSIZE setting allows. You can narrow this down to the most recent four with a command like this:

$ history | tail -3
   84  echo date: `date` > myreport
   85  cat myreport
   86  history | tail -3

The passwd command allows you to change your password but requires that you enter your current password first. It also enforces some length and complexity requirements.

$ passwd
Changing password for user justme.
Current password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

more

The more command displays file content a screen at a time. To save space below, I’ve used the -n option to display only five lines at a time from a very long list of time zones. You would press the enter key to see the next five.

$ more -n 5 TZs
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara

head and tail

The head and tail commands will display the top and bottom lines in text files. As with the more command, you can add the -n option to limit the number of lines displayed.

$ head -n 5 TZs
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
$ tail -n 5 TZs
UTC
Universal
W-SU
WET	
Zulu

chmod

The chmod command is used to change file permissions. If you set up a script that you want others to be able to run, you can give them execute permission with command like this:

$ chmod 755 myscript

The first 5 in the chmod command above sets the script’s permissions to allow others in the same group to read and execute the script. The second allows anyone else to run the script (provided they have access to the file’s location). Normally, Linux accounts are set up with the account owner being the only member of a same-named group.

$ chmod 755 mkTable
$ ls -l mkTable
-rwxr-xr-x. 1 justme justme 701 Nov 17 14:05 mkTable

cp

The cp command makes a copy of a file. For example, it’s often a good idea to make a backup copy of a file before making significant changes so that you can easily “roll back” if something goes wrong.

$ cp myfile myfile.backup

mv and rm

The mv (remove) command allows you to rename a file, move it to another location, or do both in a single command. Here are some examples:

$ mv myreport oldreport
$ mv myscript ~/bin
$ mv myreport /shared/reports

The rm (remove) command deletes a file.

$ rm oldfile
$ ls oldfile
ls: cannot access 'oldfile': No such file or directory

The final ls command above shows that the file is no longer available after the rm command is run.

ps

The ps command shows running processes. Without arguments, it only shows processes related to your current login.

$ ps
    PID TTY          TIME CMD
   6688 pts/1    00:00:00 bash
   7201 pts/1    00:00:00 ps

Use ps -ef (ps -aux on some systems) to see all running processes. You can send the command output to a more or a grep command to restrict the output to just what you’re looking for.

$ ps -ef | grep sshd
root         782       1  0 Nov20 ?        00:00:00 sshd: /usr/sbin/sshd -D
root        6632     782  0 14:38 ?        00:00:00 sshd: shs [priv]
justme      6678    6632  0 14:38 ?        00:00:00 sshd: shs@pts/1
justme      7228    6688  0 16:14 pts/1    00:00:00 grep --color=auto sshd

The command output above is showing processes associated with the sshd (secure login) processes.

who

The who command provides a list of everyone currently logged into the system, whether at the system console or logged on remotely.

$ who
fedora   seat0        2023-11-16 09:06 (login screen)
fedora   tty2         2023-11-16 09:06 (tty2)
justme   pts/1        2023-11-17 14:38 (192.168.0.8)

Wrap-up

There’s a lot to learn about Linux before you will likely feel “at home” on the command line, but it’s well worth the effort.

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