Basic UNIX Commands with Examples - Part I
Welcome to LiveFire Labs' guide to the basic UNIX commands, the first tutorial in our UNIX for Beginners Series. This guide includes a brief description for each command, an example of how to use the command, and sample command execution output.
When possible, a command example will build upon the example used for a prior command so that the guide will hopefully read more like a tutorial than simply a long list of unrelated commands. It is our desire that this guide will not be as dry as reading through a list of UNIX commands can obviously be.
If you want to practice UNIX commands on a real server and learn to read and write shell scripts, you may be interested in one of our online UNIX courses...
UNIX and Linux Operating System Fundamentals should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts. This course contains a very good "Introduction to UNIX Shell Scripting" module.
UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.
Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.
Basic UNIX Commands - Table of Contents
(click on a link to jump to a specific section)A. UNIX Commands for Manipulating Files
cat, chgrp, chmod, chown, cp, cut, mkdir, more, mv, paste, rm, rmdirB. UNIX Shell Environment Control Commands
alias, cd, clear, exit, export, passwd, read, unalias, unsetC. Commands for UNIX Job/Process Control
bg, fg, kill, jobs, psD. Informational UNIX Commands
date, diff, echo, env, file, find, finger, grep, head, history, hostname, ls, man, print, printinv, pwd, strings, tail, uname, wc, whoE. UNIX Utilities
awk, ftp, gzip, gunzip, ping, sed, talk, tar, vi, wall, write
A. UNIX Commands for Manipulating Files
cat | prints
the contents of a UNIX file $ cat unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! |
chgrp | changes
a UNIX file's group $ ls -l unix-file.txt -rw-r--r-- 1 root root 0 Apr 24 11:41 unix-file.txt $ chgrp users unix-file.txt $ ls -l unix-file.txt -rw-r--r-- 1 root users 0 Apr 24 11:41 unix-file.txt |
chmod | modifies
a UNIX file's access mode (permissions) $ ls -l unix-file.txt -rw-r--r-- 1 root users 0 Apr 24 11:41 unix-file.txt $ chmod 744 unix-file.txt $ ls -l unix-file.txt -rwxr--r-- 1 root users 0 Apr 24 11:41 unix-file.txt* |
chown | changes
UNIX file owner and group $ ls -l unix-file.txt -rwxr--r-- 1 root users 0 Apr 24 11:41 unix-file.txt* $ chown jdoe unix-file.txt $ ls -l unix-file.txt -rwxr--r-- 1 jdoe users 0 Apr 24 11:41 unix-file.txt* |
cp | copy
files and directories (-p preserve file attributes) $ cp -p unix-file.txt unix-file-old.txt |
cut | prints
selected parts of lines from a UNIX file $ cat unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! $ cut -c1-7 unix-file.txt Welcome |
mkdir | makes
a new UNIX directory $ ls -ld mydir /bin/ls: mydir: No such file or directory $ mkdir mydir $ ls -ld mydir drwxr-xr-x 2 root root 2048 Apr 24 11:55 mydir/ |
more | prints
the contents of a UNIX file one screen at a time $ more unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! |
mv | moves
(or renames) a UNIX file $ ls -l mydir total 4 drwxr-xr-x 2 root root 2048 Apr 24 11:55 ./ drwxrwxrwt 10 root root 2048 Apr 24 11:55 ../ $ mv unix-file.txt mydir/unix-file-123.txt $ ls -l mydir total 6 drwxr-xr-x 2 root root 2048 Apr 24 11:58 ./ drwxrwxrwt 10 root root 2048 Apr 24 11:58 ../ -rwxr--r-- 1 jdoe users 52 Apr 24 11:52 unix-file-123.txt* $ mv mydir/unix-file-123.txt ./unix-file.txt $ ls -l mydir total 4 drwxr-xr-x 2 root root 2048 Apr 24 11:58 ./ drwxrwxrwt 10 root root 2048 Apr 24 11:58 ../ $ ls -l unix* -rwxr--r-- 1 jdoe users 0 Apr 24 11:41 unix-file-old.txt* -rwxr--r-- 1 jdoe users 52 Apr 24 11:52 unix-file.txt* |
paste | merge
lines from one or more UNIX files into a single file $ cat unix-file-old.txt $ cat unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! $ paste unix-file.txt > unix-file-old.txt $ cat unix-file-old.txt Welcome to LiveFire Labs' UNIX basic commands page! |
rm | removes
/ deletes UNIX files $ ls -l unix-file-old.txt -rwxr--r-- 1 jdoe users 52 Apr 24 12:02 unix-file-old.txt* $ rm -f unix-file-old.txt $ ls -l unix-file-old.txt /bin/ls: unix-file-old.txt: No such file or directory |
rmdir | removes
/ deletes UNIX directories $ ls -ld mydir drwxr-xr-x 2 root root 2048 Apr 24 11:58 mydir/ $ rmdir mydir $ ls -ld mydir /bin/ls: mydir: No such file or directory |
[Back to Table of Contents]
B. UNIX Shell Environment Control Commands
alias | creates
a UNIX command alias $ alias dateyr='date +%Y' $ dateyr 2013 |
cd | changes
the UNIX present working directory (current
directory) $ mkdir mydir $ pwd /tmp $ cd mydir $ pwd /tmp/mydir |
clear | clears
the screen $ clear $ |
exit | logs
you out of a UNIX system, or exits a shell $ ps -f UID PID PPID C STIME TTY TIME CMD jdoe 23707 23706 0 12:11 pts/1 00:00:00 -ksh jdoe 23728 23707 0 12:11 pts/1 00:00:00 ps -f $ ksh $ ps -f UID PID PPID C STIME TTY TIME CMD jdoe 23707 23706 0 12:11 pts/1 00:00:00 -ksh jdoe 23729 23707 0 12:11 pts/1 00:00:00 ksh jdoe 23730 23729 0 12:11 pts/1 00:00:00 ps -f $ exit $ ps -f UID PID PPID C STIME TTY TIME CMD jdoe 23707 23706 0 12:11 pts/1 00:00:00 -ksh jdoe 23736 23707 0 12:11 pts/1 00:00:00 ps -f |
export | declares
a variable to be an environment variable $ export MYSHELLVAR=hello $ env | grep MY MYSHELLVAR=hello |
passwd | changes
a UNIX account password (logged in as jdoe) $ whoami jdoe $ passwd Changing password for user jdoe. Changing password for jdoe (current) UNIX password: New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. changes a UNIX account password (logged in as root) $ whoami root $ passwd jdoe Changing password for user jdoe. New UNIX password: BAD PASSWORD: it is based on a dictionary word Retype new UNIX password: passwd: all authentication tokens updated successfully. |
read | reads input |
unalias | deletes
a UNIX command alias $ alias dateyr alias dateyr='date +%Y' $ unalias dateyr $ alias dateyr -bash: alias: dateyr: not found |
unset | removes
(deletes) a UNIX shell variable definition $ env | grep MY MYSHELLVAR=hello $ unset MYSHELLVAR $ env MYSHELLVAR env: MYSHELLVAR: No such file or directory |
[Back to Table of Contents]
C. Commands for UNIX Job/Process Control
bg | moves
a specified UNIX job to the background $ cat infinite-loop-shell-script #!/bin/ksh while [ 1 ] do sleep 3 done <pressed Ctrl-Z to stop infinite loop script> $ ./infinite-loop-shell-script ^Z[1] + Stopped ./infinite-loop-shell-script $ jobs [1] + Stopped ./infinite-loop-shell-script $ bg %1 [1] ./infinite-loop-shell-script $ jobs [1] + Running ./infinite-loop-shell-script $ ps -f UID PID PPID C STIME TTY TIME CMD jdoe 24068 24067 0 12:28 pts/1 00:00:00 -ksh jdoe 24092 24068 0 12:29 pts/1 00:00:00 /bin/ksh ./infinite-loop-shell-script jdoe 24163 24092 0 12:30 pts/1 00:00:00 sleep 3 jdoe 24164 24068 0 12:30 pts/1 00:00:00 ps -f $ fg %1 ./infinite-loop-shell-script ^Z[1] + Stopped ./infinite-loop-shell-script $ jobs [1] + Stopped ./infinite-loop-shell-script $ kill %1 $ Terminated [1] + Terminated ./infinite-loop-shell-script $ ps -f UID PID PPID C STIME TTY TIME CMD jdoe 24068 24067 0 12:28 pts/1 00:00:00 -ksh jdoe 24214 24068 0 12:33 pts/1 00:00:00 ps -f |
fg | moves
a specified UNIX job to the foreground SEE "bg" example in this section |
kill |
terminate
a UNIX job SEE "bg" example in this section |
jobs | lists
UNIX
jobs running in the background SEE "bg" example in this section |
ps | displays
active UNIX system processes SEE "bg" example in this section |
[Back to Table of Contents]
D. Informational UNIX Commands
date | prints
(or sets) the UNIX system date and time $ date Wed Apr 24 12:38:02 EDT 2013 |
diff | finds
the differences between UNIX files $ cat unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! $ cat unix-file-old.txt Welcome to LiveFire Labs' UNIX basic commands page! this is an extra line in unix-file-old.txt $ diff unix-file.txt unix-file-old.txt 1a2 > this is an extra line in unix-file-old.txt |
echo | prints
a line of text $ echo hello everyone searching for basic unix commands hello everyone searching for basic unix commands |
env | prints
UNIX shell environmental variables $ env _=/bin/env PATH=/home/jdoe/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin PERL_LOCAL_LIB_ROOT=:/home/jdoe/perl5 SHELL=/bin/ksh USER=jdoe TERM=xterm INPUTRC=/etc/inputrc G_BROKEN_FILENAMES=1 LANG=C PERL_MM_OPT=INSTALL_BASE=/home/jdoe/perl5 ENV=/home/jdoe/.kshrc SSH_TTY=/dev/pts/1 LOGNAME=jdoe PERL_MB_OPT=--install_base /home/jdoe/perl5 MAIL=/var/spool/mail/jdoe HOME=/home/jdoe HISTSIZE=1000 PERL5LIB=/home/jdoe/perl5/lib/perl5: LESSOPEN=|/usr/bin/lesspipe.sh %s CVS_RSH=ssh HISTFILE=/home/jdoe/.history $ |
file | determines
UNIX file type $ file unix-file.txt unix-file.txt: ASCII text |
find | searches
for files in a UNIX directory tree $ find . -name '*unix-file*' ./unix-file-old.txt ./unix-file.txt |
finger | provides
detailed information about UNIX system users $ finger jdoe Login: jdoe Name: (null) Directory: /home/jdoe Shell: /bin/ksh On since Wed Apr 24 12:28 (EDT) on pts/1 from 86.20.39.128 3 minutes 32 seconds idle (messages off) No mail. No Plan. $ |
grep | searches
for patterns in one or more UNIX files $ grep commands * unix-file-old.txt:Welcome to LiveFire Labs' UNIX basic commands page! unix-file.txt:Welcome to LiveFire Labs' UNIX basic commands page! |
head | prints
the first few lines of a UNIX file $ cat unix-file-old.txt Welcome to LiveFire Labs' UNIX basic commands page! this is an extra line in unix-file-old.txt this is line 3 in unix-file-old.txt this is line 4 in unix-file-old.txt this is line 5 in unix-file-old.txt this is line 6 in unix-file-old.txt this is line 7 in unix-file-old.txt this is line 8 in unix-file-old.txt this is line 9 in unix-file-old.txt $ head -5 unix-file-old.txt Welcome to LiveFire Labs' UNIX basic commands page! this is an extra line in unix-file-old.txt this is line 3 in unix-file-old.txt this is line 4 in unix-file-old.txt this is line 5 in unix-file-old.txt |
history | prints
the contents of your UNIX command history file $ history 33 ./infinite-loop-shell-script 34 cat infinite-loop-shell-script 35 jobs 36 bg %1 37 ps -f 38 jobs 39 fg %1 40 jobs 41 kill %1 42 ps -f 43 man kill 44 date 45 echo hello everyone looking for basic unix commands!!! 46 echo hello everyone searching for basic unix commands 47 env 48 env |
hostname | prints
the UNIX system's host name $ hostname server.livefirelabs.com |
ls | lists
the contents of a UNIX directory $ ls un* unix-file-old.txt unix-file.txt* $ ls -l un* -rw-r--r-- 1 root root 347 Apr 24 12:51 unix-file-old.txt -rwxr--r-- 1 jdoe users 52 Apr 24 11:52 unix-file.txt* $ |
man | format
and display the online UNIX manual page (man pages) $ man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort. <snip> |
prints
a line of text $ export MYSHELLVAR=hello $ print "The value of the shell variable MYSHELLVAR is $MYSHELLVAR" The value of the shell variable MYSHELLVAR is hello |
|
printenv | prints
UNIX environment variables. Accepts a variable name as an
argument. $ printenv MYSHELLVAR hello ...or just... $ printenv ...to see all shell variables |
pwd | prints
the working directory $ pwd /home/jdoe |
strings | prints
the strings of printables characters in a UNIX file $ strings /bin/ls |
tail | prints
the last few lines of a UNIX file $ cat unix-file-old.txt Welcome to LiveFire Labs' UNIX basic commands page! this is an extra line in unix-file-old.txt this is line 3 in unix-file-old.txt this is line 4 in unix-file-old.txt this is line 5 in unix-file-old.txt this is line 6 in unix-file-old.txt this is line 7 in unix-file-old.txt this is line 8 in unix-file-old.txt this is line 9 in unix-file-old.txt $ tail -4 unix-file-old.txt this is line 6 in unix-file-old.txt this is line 7 in unix-file-old.txt this is line 8 in unix-file-old.txt this is line 9 in unix-file-old.txt |
uname | prints
UNIX
system information $ uname -s SunOS |
wc | prints
the number of characters, words, or lines in a UNIX file $ wc unix-file-old.txt 9 57 347 unix-file-old.txt |
who | displays
who is logged in on a UNIX system $ who root pts/0 Apr 24 14:36 (86.20.39.128) jdoe pts/1 Apr 24 14:36 (86.20.39.128) |
[Back to Table of Contents]
E. UNIX Utilities
awk | pattern
scanning and processing language $ cat unix-file.txt Welcome to LiveFire Labs' UNIX basic commands page! $ cat unix-file.txt | awk '{ print $5,$6,$7 }' UNIX basic commands |
ftp | transfers
files between two networked computers (hosts) If you have a username on the remote system, you should be able to login using it and your password. There are times when you can login to an FTP server as a guest (anonymously). Many software vendors will allow anonymous FTP connections so that their customers can conveniently download updated versions of their software and patches for existing bugs in their software. The standard username for logging in as a guest is anonymous. Once you enter anonymous at the username prompt, you will be prompted for a password: $ ftp 129.100.1.32 Connected to 129.100.1.32. 220 ProFTPD 1.2.2 Server (ProFTPD Default Installation) [sing2.lab.com] 500 AUTH not understood. 500 AUTH not understood. KERBEROS_V4 rejected as an authentication type Name (129.100.1.32:student1): anonymous 331 Anonymous login ok, send your complete email address as your password. Password: 230 Anonymous access granted, restrictions apply. Remote system type is UNIX. Using binary mode to transfer files. ftp> You will typically be requested to use your full Internet email address (e.g. jdoe@livefirelabs.com) as your password. This information may be used by the system administrator to monitor connections and downloads from his/her server. After successfully logging in to the FTP server, you will be left at the FTP command prompt: ftp> |
gzip | compresses
(or expands) a UNIX file - note the file size change and
".gz" extension $ ls -l demo_ksh_array_shell_script.tar -rw-r--r-- 1 jdoe users 20480 Apr 9 15:07 demo_ksh_array_shell_script.tar $ gzip demo_ksh_array_shell_script.tar $ ls -l demo_ksh_array_shell_script.tar* -rw-r--r-- 1 jdoe users 3496 Apr 9 15:07 demo_ksh_array_shell_script.tar.gz |
gunzip | expands
a compressed UNIX file -
note the file size change and ".gz" extension $ ls -l demo_ksh_array_shell_script.tar* -rw-r--r-- 1 jdoe users 3496 Apr 9 15:07 demo_ksh_array_shell_script.tar.gz $ gunzip demo_ksh_array_shell_script.tar.gz $ ls -l demo_ksh_array_shell_script.tar* -rw-r--r-- 1 jdoe users 20480 Apr 9 15:07 demo_ksh_array_shell_script.tar |
ping | tests
the availability of another computer system (host) on the
network. It can also be used to gauge network
performance. $ ping Internet-name or $ ping Internet-address $ ping 192.168.0.20 PING 192.168.0.20 (192.168.0.20) from 192.168.0.10 : 56(84) bytes of data. 64 bytes from 192.168.0.20: icmp_seq=0 ttl=255 time=1.883 msec 64 bytes from 192.168.0.20: icmp_seq=1 ttl=255 time=2.090 msec 64 bytes from 192.168.0.20: icmp_seq=2 ttl=255 time=1.816 msec 64 bytes from 192.168.0.20: icmp_seq=3 ttl=255 time=1.831 msec 64 bytes from 192.168.0.20: icmp_seq=4 ttl=255 time=1.824 msec 64 bytes from 192.168.0.20: icmp_seq=5 ttl=255 time=2.248 msec 64 bytes from 192.168.0.20: icmp_seq=6 ttl=255 time=1.993 msec 64 bytes from 192.168.0.20: icmp_seq=7 ttl=255 time=1.830 msec 64 bytes from 192.168.0.20: icmp_seq=8 ttl=255 time=1.829 msec 64 bytes from 192.168.0.20: icmp_seq=9 ttl=255 time=1.819 msec --- 192.168.0.20 ping statistics --- 10 packets transmitted, 10 packets received, 0% packet loss round-trip min/avg/max/mdev = 1.816/1.916/2.248/0.144 ms $ Since this is a continuous operation, you will need to use the Ctrl-C key combination to terminate the command. |
sed | sed
is a "non-interactive" stream-oriented editor |
talk | used
for having 2-way communication sessions with other UNIX
system users. This utility is similar to write, but is
more sophisticated. $ talk user [-x] [ttyname] |
tar | creates
an archive containing one or more UNIX files $ ls -l un* -rw-r--r-- 1 root root 347 Apr 24 12:51 unix-file-old.txt -rwxr--r-- 1 jdoe users 52 Apr 24 11:52 unix-file.txt* $ $ tar cvf /tmp/unix-files.tar ./un* ./unix-file-old.txt ./unix-file.txt $ $ tar tvf /tmp/unix-files.tar -rw-r--r-- root/root 347 2013-04-24 12:51:57 ./unix-file-old.txt -rwxr--r-- jdoe/users 52 2013-04-24 11:52:10 ./unix-file.txt $ cd mydir $ mv ../unix-files.tar . $ tar xvf unix-files.tar ./unix-file-old.txt ./unix-file.txt $ pwd /tmp/mydir $ ls -l total 18 drwxr-xr-x 2 root root 2048 Apr 24 15:18 ./ drwxrwxrwt 10 root root 2048 Apr 24 15:18 ../ -rw-r--r-- 1 root root 347 Apr 24 12:51 unix-file-old.txt -rwxr--r-- 1 jdoe users 52 Apr 24 11:52 unix-file.txt* -rw-r--r-- 1 root root 10240 Apr 24 15:17 unix-files.tar |
vi | starts
the UNIX vi text editor (1) open an existing file for editing: $ vi existing-filename (2) open a new file for editing: $ vi new-filename (3) start editing a file without specifying the name of a file: $ vi |
wall | sends
a message to everyone who is logged in on a UNIX system (from "root" terminal screen) $ wall "hello all system users" $ Broadcast message from root (pts/0) (Wed Apr 24 15:25:23 2013): hello all system users (from "jdoe" terminal screen) $ Broadcast message from root (pts/0) (Wed Apr 24 15:25:23 2013): hello all system users |
write | sends
a message to a specific user. Used for 2-way
communication. (from "root" terminal screen) $ write jdoe hi, jdoe! how are you? (from "jdoe" terminal screen) Message from root@server.livefirelabs.com on pts/0 at 15:29 ... hi, jdoe! how are you? $ write root good, how are you? (from "root" terminal screen) Message from jdoe@server.livefirelabs.com on pts/1 at 15:30 ... good, how are you? TIP: You may need to run the "mesg y" command before this will work on your system |
[Back to Table of Contents]