7 mins
Basic Shell Commands

A comprehensive reference guide to essential Unix/Linux shell commands for file operations, process management, and system administration.

Basic Shell Commandsh1

A comprehensive reference guide to essential Unix/Linux shell commands. This guide covers the most commonly used commands for file operations, process management, text processing, and system administration.

File Operationsh2

cat - Display file contentsh3

Send a file to the screen in one go. Useful for piping to other programs.

Terminal window
cat file1 # list file1 to screen
cat file1 file2 file3 > outfile # add files together into outfile
cat *.txt > outfile # add all .txt files together
cat file1 file2 | grep fred # pipe files

cp - Copy files and directoriesh3

Copy file(s) and directories with various options.

Terminal window
cp file1 file2 # copy file1 to file2
cp file1 directory # copy file1 into directory
cp file1 file2 file3 directory # copy files into directory
cp -R dir1 dir2/ # copy dir1 into dir2 including subdirectories
cp -pR dir1 dir2/ # copy directory, preserving permissions

mv - Move or rename filesh3

Move file(s) or rename a file.

Terminal window
mv file1 file2 # rename file1 to file2
mv dir1 dir2 # rename directory dir1 to dir2
mv file1 file2 file3 directory # move files into directory

rm - Delete files and directoriesh3

Delete (remove) files and directories.

Terminal window
rm file1 # delete a file (use -i to ask whether sure)
rm -r dir1 # delete a directory and everything in it (CARE!)
rm -rf dir1 # like above, but don't ask if we have a -i alias

rmdir - Delete empty directoriesh3

Delete a directory if it is empty.

Terminal window
rmdir dirname # delete empty directory

ls - List directory contentsh3

Show lists of files or information on the files.

Terminal window
ls file # does the file exist?
ls -l file # show information about the file
ls *.txt # show all files ending in .txt
ls -lt # show information about all files in date order
ls -lrt # above reversed in order
ls -a # show all files including hidden files
ls dir # show contents of directory
ls -d dir # does the directory exist?
ls -p # adds meaning characters to ends of filenames
ls -R # show files also in subdirectories of directory
ls -1 # show one file per line

Directory Navigationh2

cd - Change current directoryh3

Navigate between directories.

Terminal window
cd # go to home directory
cd ~/papers # go to /home/user/papers
cd ~fred # go to /home/fred
cd dir # go to directory (relative)
cd /dir1/dir2/dir3… # go to directory (absolute)
cd - # go to last directory you were in

pwd - Show current working directoryh3

Display the current directory path.

Terminal window
pwd # show current working directory

grep - Search for text in filesh3

Look for text in files. List out lines containing text.

Terminal window
grep "hi there" file1 file2 # look for 'hi there' in files
grep -i "hi there" filename # ignore capitals in search
cat filename | grep "hi there" # use pipe
grep -v "foo" filename # list lines that do not include foo

more - Display file contents page by pageh3

Show a file one screen at a time.

Terminal window
more file # show file one screen at a time
grep 'frog' file | more # Do it to output of other command

File Compression and Archivesh2

gtar - GNU tar utilityh3

Store directories and files together into a single archive file.

Terminal window
gtar cf out.tar dir1 # put contents of directory into out.tar
gtar czf out.tar.gz dir1 # write compressed tar, out.tar.gz
gtar tf in.tar # list contents of in.tar
gtar tzf in.tar.gz # list contents of compressed in.tar.gz
gtar xf in.tar # extract contents of in.tar here
gtar xzf in.tar.gz # extract compressed in.tar.gz
gtar xf in.tar file.txt # extract file.txt from in.tar

gzip / gunzip - File compressionh3

GNU Compress files into a smaller space, or decompress files.

Terminal window
gzip file.fits # compresses file.fits into file.fits.gz
gunzip file.fits.gz # recovers original file.fits
gzip *.dat # compresses all .dat files into .dat.gz
gunzip *.dat.gz # decompresses all .dat.gz files into .dat
program | gzip > out.gz # compresses program output into out.gz
program | gunzip > out # decompresses compressed program output

Process Managementh2

ps - List processesh3

List processes on system.

Terminal window
ps -u jss # list jss's processes
ps -f # list processes started here in full format
ps -AF # list all processes in extra full format
ps -A -l # list all processes in long format
ps -A | grep tcsh # list all tcsh processes

kill - Terminate processesh3

Kill, pause or continue a process.

Terminal window
kill 666 # this sends a "nice" kill to the process
kill -KILL 666 # (or equivalently)
kill -9 666 # which should really kill it!
kill -STOP 667 # pause (stop) process
kill -CONT 667 # unpause process

top - Monitor system processesh3

Interactively show you the “top” processes on a system.

Terminal window
top # show top processes consuming CPU time
# Press 'q' to exit, 'k' to kill a process, 'r' to renice a process

nice - Set process priorityh3

Start a process with a specific priority level.

Terminal window
nice +19 myjob1 # run at lowest priority
nice +8 myjob2 # run at lowish priority

System Informationh2

date - Show current date and timeh3

Display the current date and time.

Terminal window
date # shows current date

file - Identify file typeh3

Tells you what sort of file it is.

Terminal window
file temp_70.jpg
# Output: temp_70.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), 72 x 72

quota - Check disk usageh3

Shows you how much disk space you have left.

Terminal window
quota -v # show disk quota information

Environment and Configurationh2

printenv - Print environment variablesh3

Display environment variables.

Terminal window
printenv MYVARIABLE # print specific variable
printenv # print all variables

passwd - Change passwordh3

Change your user password.

Terminal window
passwd # change your password

Text Editorsh2

emacs - The ubiquitous text editorh3

Powerful text editor with extensive features.

Terminal window
emacs foo.txt # open file in emacs

nano - Simple text editorh3

Very simple text editor for quick edits.

Terminal window
nano filename # open file in nano
# Warning: can introduce extra line breaks if screen is too narrow!

Applicationsh2

firefox - Start Mozilla Firefoxh3

Launch the Firefox web browser.

Terminal window
firefox # start Mozilla Firefox

gedit - Gnome text editorh3

Simple graphical text editor.

Terminal window
gedit # start Gnome text editor

openoffice.org - Office suiteh3

Free office suite available for Linux/Unix, Windows and Mac OS X.

Terminal window
openoffice.org # start OpenOffice

Documentation and Helph2

man - Manual pagesh3

Get instructions for Unix commands.

Terminal window
man man # get help on man
man grep # get help on grep
man -s1 sort # show documentation on sort in section 1

info - GNU documentation systemh3

A documentation system designed to replace man for GNU programs.

Terminal window
info gtar # documentation for gtar

Printingh2

lp - Print filesh3

Send files to a printer.

Terminal window
lp file.ps # sends postscript file to the default printer
lp -dlp2 file.ps # sends file to the printer lp2
lp -c file.ps # copies file first, so you can delete it
lpstat -p lp2 # get status and list of jobs on lp2
cancel lp2–258 # cancel print job lp2–258

lpr - Alternative print commandh3

Send files to printer using lpr.

Terminal window
lpr -Plp2 file.ps # send file.ps to lp2
lpq -Plp2 # get list of jobs on lp2
lprm -Plp2 1234 # delete job 1234 on lp2

Development Toolsh2

cc - C compilerh3

Compile a C program.

Terminal window
cc test1.c # compile test1.c to a.out
cc -O2 -o test2.prog test2.c # compile test2.c to test2.prog

latex - LaTeX processorh3

Convert a tex file to dvi.

Terminal window
latex document.tex # convert tex file to dvi

System Controlh2

logout - Exit shellh3

Closes the current shell.

Terminal window
logout # close current shell
exit # alternative to logout

Tips and Best Practicesh2

  1. Use tab completion - Press Tab to auto-complete commands and filenames
  2. Use history - Press ↑/↓ arrows to navigate command history
  3. Use aliases - Create shortcuts for frequently used commands
  4. Be careful with rm -rf - This can delete everything irreversibly
  5. Use -i flag - Add -i to commands like rm and cp for interactive mode
  6. Check before you execute - Use ls to verify files before operations
  7. Use wildcards wisely - * matches any characters, ? matches single character
  8. Pipe commands - Use | to chain commands together
  9. Redirect output - Use > to save output to files, >> to append
  10. Read the manual - Use man command for detailed help

Conclusionh2

These basic shell commands form the foundation of Unix/Linux system administration and file management. Master these commands and you’ll be able to navigate, manipulate, and manage files and processes efficiently from the command line.

Remember: Practice makes perfect! Start with simple operations and gradually work your way up to more complex command combinations.