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.
cat file1 # list file1 to screencat file1 file2 file3 > outfile # add files together into outfilecat *.txt > outfile # add all .txt files togethercat file1 file2 | grep fred # pipe filescp - Copy files and directoriesh3
Copy file(s) and directories with various options.
cp file1 file2 # copy file1 to file2cp file1 directory # copy file1 into directorycp file1 file2 file3 … directory # copy files into directorycp -R dir1 dir2/ # copy dir1 into dir2 including subdirectoriescp -pR dir1 dir2/ # copy directory, preserving permissionsmv - Move or rename filesh3
Move file(s) or rename a file.
mv file1 file2 # rename file1 to file2mv dir1 dir2 # rename directory dir1 to dir2mv file1 file2 file3 … directory # move files into directoryrm - Delete files and directoriesh3
Delete (remove) files and directories.
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 aliasrmdir - Delete empty directoriesh3
Delete a directory if it is empty.
rmdir dirname # delete empty directoryls - List directory contentsh3
Show lists of files or information on the files.
ls file # does the file exist?ls -l file # show information about the filels *.txt # show all files ending in .txtls -lt # show information about all files in date orderls -lrt # above reversed in orderls -a # show all files including hidden filesls dir # show contents of directoryls -d dir # does the directory exist?ls -p # adds meaning characters to ends of filenamesls -R # show files also in subdirectories of directoryls -1 # show one file per lineDirectory Navigationh2
cd - Change current directoryh3
Navigate between directories.
cd # go to home directorycd ~/papers # go to /home/user/paperscd ~fred # go to /home/fredcd dir # go to directory (relative)cd /dir1/dir2/dir3… # go to directory (absolute)cd - # go to last directory you were inpwd - Show current working directoryh3
Display the current directory path.
pwd # show current working directoryText Processing and Searchh2
grep - Search for text in filesh3
Look for text in files. List out lines containing text.
grep "hi there" file1 file2 … # look for 'hi there' in filesgrep -i "hi there" filename # ignore capitals in searchcat filename | grep "hi there" # use pipegrep -v "foo" filename # list lines that do not include foomore - Display file contents page by pageh3
Show a file one screen at a time.
more file # show file one screen at a timegrep 'frog' file | more # Do it to output of other commandFile Compression and Archivesh2
gtar - GNU tar utilityh3
Store directories and files together into a single archive file.
gtar cf out.tar dir1 # put contents of directory into out.targtar czf out.tar.gz dir1 # write compressed tar, out.tar.gzgtar tf in.tar # list contents of in.targtar tzf in.tar.gz # list contents of compressed in.tar.gzgtar xf in.tar # extract contents of in.tar heregtar xzf in.tar.gz # extract compressed in.tar.gzgtar xf in.tar file.txt … # extract file.txt from in.targzip / gunzip - File compressionh3
GNU Compress files into a smaller space, or decompress files.
gzip file.fits # compresses file.fits into file.fits.gzgunzip file.fits.gz # recovers original file.fitsgzip *.dat # compresses all .dat files into .dat.gzgunzip *.dat.gz # decompresses all .dat.gz files into .datprogram | gzip > out.gz # compresses program output into out.gzprogram | gunzip > out # decompresses compressed program outputProcess Managementh2
ps - List processesh3
List processes on system.
ps -u jss # list jss's processesps -f # list processes started here in full formatps -AF # list all processes in extra full formatps -A -l # list all processes in long formatps -A | grep tcsh # list all tcsh processeskill - Terminate processesh3
Kill, pause or continue a process.
kill 666 # this sends a "nice" kill to the processkill -KILL 666 # (or equivalently)kill -9 666 # which should really kill it!kill -STOP 667 # pause (stop) processkill -CONT 667 # unpause processtop - Monitor system processesh3
Interactively show you the “top” processes on a system.
top # show top processes consuming CPU time# Press 'q' to exit, 'k' to kill a process, 'r' to renice a processnice - Set process priorityh3
Start a process with a specific priority level.
nice +19 myjob1 # run at lowest prioritynice +8 myjob2 # run at lowish prioritySystem Informationh2
date - Show current date and timeh3
Display the current date and time.
date # shows current datefile - Identify file typeh3
Tells you what sort of file it is.
file temp_70.jpg# Output: temp_70.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), 72 x 72quota - Check disk usageh3
Shows you how much disk space you have left.
quota -v # show disk quota informationEnvironment and Configurationh2
printenv - Print environment variablesh3
Display environment variables.
printenv MYVARIABLE # print specific variableprintenv # print all variablespasswd - Change passwordh3
Change your user password.
passwd # change your passwordText Editorsh2
emacs - The ubiquitous text editorh3
Powerful text editor with extensive features.
emacs foo.txt # open file in emacsnano - Simple text editorh3
Very simple text editor for quick edits.
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.
firefox # start Mozilla Firefoxgedit - Gnome text editorh3
Simple graphical text editor.
gedit # start Gnome text editoropenoffice.org - Office suiteh3
Free office suite available for Linux/Unix, Windows and Mac OS X.
openoffice.org # start OpenOfficeDocumentation and Helph2
man - Manual pagesh3
Get instructions for Unix commands.
man man # get help on manman grep # get help on grepman -s1 sort # show documentation on sort in section 1info - GNU documentation systemh3
A documentation system designed to replace man for GNU programs.
info gtar # documentation for gtarPrintingh2
lp - Print filesh3
Send files to a printer.
lp file.ps # sends postscript file to the default printerlp -dlp2 file.ps # sends file to the printer lp2lp -c file.ps # copies file first, so you can delete itlpstat -p lp2 # get status and list of jobs on lp2cancel lp2–258 # cancel print job lp2–258lpr - Alternative print commandh3
Send files to printer using lpr.
lpr -Plp2 file.ps # send file.ps to lp2lpq -Plp2 # get list of jobs on lp2lprm -Plp2 1234 # delete job 1234 on lp2Development Toolsh2
cc - C compilerh3
Compile a C program.
cc test1.c # compile test1.c to a.outcc -O2 -o test2.prog test2.c # compile test2.c to test2.proglatex - LaTeX processorh3
Convert a tex file to dvi.
latex document.tex # convert tex file to dviSystem Controlh2
logout - Exit shellh3
Closes the current shell.
logout # close current shellexit # alternative to logoutTips and Best Practicesh2
- Use tab completion - Press Tab to auto-complete commands and filenames
- Use history - Press ↑/↓ arrows to navigate command history
- Use aliases - Create shortcuts for frequently used commands
- Be careful with
rm -rf- This can delete everything irreversibly - Use
-iflag - Add-ito commands likermandcpfor interactive mode - Check before you execute - Use
lsto verify files before operations - Use wildcards wisely -
*matches any characters,?matches single character - Pipe commands - Use
|to chain commands together - Redirect output - Use
>to save output to files,>>to append - Read the manual - Use
man commandfor 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.