Linux / UNIX Commands
For a basic introduction to linux / UNIX take a look at this linux_introduction.pdf
Listing files and directories
Description: You can use the ls command to list the files in a directory.
# ls [option] directory_name
Description: Create the Directory(ies), if they do not already exist.
# mkdir directory_name
Changing to a different directory
Description: To change your current working directory.
# cd pathname
Changing to a different password
Description:To change your current password.
# passwd LOGIN
Description:To display the pathname to your current directory.
Copying Files / Directories
Description:To copy a directory or a file.
# cp -r directory1 directory2
# cp file1 file2
Note: Here the -r option for recursive copy.
Moving Files / Directories
Description: To move files and directories from one place to another.
# mv [option] file1 file2
# mv [option] directory1 directory2
# mv [option] file directory
Removing files and directories
Name: rm , rmdir
Description:To remove files and directories.
# rm file_name
# rmdir directory
Note:The directory must be empty before deleting it. You will need to remove any files and subdirectories that it contains. To remove a directory that contains files or subdirectories use the command:
# rm -r directory
Displaying the contents of a file on the screen
Description:The cat command is useful for displaying short files of a few lines. To display longer files use an editor or pager.
# cat file_name
Note: The cat command can also be used to concatinate 2 files:
# cat file1 file2 > file3
Description:The vi editor (pronounced "vee eye") is available on all UNIX systems: other editors are not. Being able to use vi ensures that you will always have an editor available to you.
# vi file_name
Note:More information about vi editor is obtained at this website http://thomer.com.vi/vi.html
Name: more, less
Description:A pager is a program for displaying the contents of a text file one screenful at a time. The most commonly used pager programs are more and less. A Pager is a filter.
# more file_name
# less file_name
You can now use one of the following commands:
space bar Display next screenful of text.
<RETURN> Display next line of text.
q Exit from more: this can be done at any time.
d Scroll forwards about half a screenful of text.
b Skip backwards one screenful of text.
h Display a list of commands (help).
Name: head, tail
Description:To display the first 10 lines of a text file use head. And to display the last 10 lines of a text file use the command tail.
# head file_name
-n <NUMBER> : To print first NUMBER lines instead of first 10.
# tail file_name
Description: List history of last 15 commands used.
With no options,it displays the history list with line numbers.
Searching the contents of a file
Description:To search a text file for a string of characters or a regular expression.
# grep [option] pattern file_name
-i ignore upper/lower case distinctions
Description:Type man command to read the manual page for a particular command.
# man command_name
File system security (access rights)
Each file (and directory) has associated access rights, which may be found by typing 'ls -l'.
# ls -l
You will see that you now get lots of details about the contents of your directory, similar to the example below.
Access rights on files.
r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file
Changing access rights
Description:To change the access permissions for a file or directory.
# chmod mode file_name
# chmod mode directory_name
Note: More details about mode:There is a shorthand way of setting permissions by using octal numbers. Read permission is given the value 4, write permission the value 2 and execute permission 1.r w x
Processes and Jobs
Description:To monitor the state of your processes.
# ps [-options]
Note: The information displayed by the ps command varies acccording to which command option(s) you use and the type of UNIX that you are using.Example: These are some of the column headings displayed by the following command:
Here "| more" is used to filter the output to 'more'.
Description:To display the state of any background jobs that you are currently running use the job command.
Example1: To displays the job number, its state and the command associated with this job.
# jobs -l
Placing a foreground process in the background
For example:# lpr -Pps23 part[1-8].ps
 lpr -Pps23 interface.ps &
 Running lpr -Pps7 interface.psTo run MATLAB in the background:
# Clear the DISPLAY.
# Call MATLAB with the appropriate input and output,
# make it immune to hangups and quits using ''nohup'',
# and run it in the background.
nohup matlab < 12 &
# unsetenv DISPLAY# nohup matlab < input_file > output_file &
Bring a job into the foreground
Description:To bring a specific background job into the foreground.
# fg job_number
For example: To bring the third job which is running in the background:
# lpr -Pps23 interface.ps &
Killing processes / jobs
Description:Each process you start is usually completed within a few seconds. Sometimes a background process or a process without a controlling terminal hangs up and you will need to destroy this process by killing it.
# kill [-signal] process_identifier(PID)
Use the ps -ef command if you need to find the PID of a process.
Always try to kill a process with a simple kill command.
# kill process_identifier
This is the cleanest way to kill a process and has the same effect as cancelling a process.
If this does not work use the -1 (one) signal.
# kill -1 process_identifier
This tells the process to hangup just as though you were logging out. The system will attempt to kill any child processes.
As a last resort use the -9 signal:
# kill -9 process_identifier
This will stop the process dead in its tracks but it may leave any child processes still running.
A few other added features of kill command are:
# kill ^C
This will kill the current process - the one in the foreground
# kill ^Z
This will suspend foreground process.
Finding a file
Description: To locate a file in the file system , use the find command.
# find path_name -name file_name -print
# find . -name "*.ps" -print
The above example search current and all sub-directories for all .ps (postscript) files.
Note1: * = Wild Char
Note2: . = Current directory
Determine file type
Description: Determines the type of content, "file" looks inside the file to find particular patterns in contents
# file -z attachment3.doc
In the above example option -z makes "file" look also inside compressed files to determine what the compressed file is and determines the type of file attachment3.doc.
Compressing a file
Description:To create an archive file or directory use the command.
# tar cvf tarfile_name.tar file_names(directory_name)
Description:zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows NT, Minix, Atari and Macintosh, Amiga and Acorn RISC OS.
# zip file_name
Description:To compress a file to *.gz format. Usually used to further compress the *.tar file.
# gzip [-options] file_name
Note: More about gzip is obtained by using the command "man gzip".
Description:To compress a file to *.bz2 format. Usually used to further compress the *.tar file. This is the best compressing tool of all.
# bzip2 [-options] file_name
Note: More about bzip2 is obtained by using the command "man bzip2".
Description:Compress reduces the size of the named files ( *.Z format).
# compress [-options] file_name
Note: More about compress is obtained by using the command "man compress".
# tar czf file_name.tgz file_names(directory_name)
# tar cjf file_name.bz2 file_names(directory_name)
Uncompressing a file
Description:To unpack a tar file use the tar command.
# tar xvf tarfile_name.tar
Description:To unzip a zip file.
# unzip file_name
Description:To decompress *.bz2, *.bz, *.tbz2, *.tbz format files.
# bunzip2 file_name
Note: bunzip2 will decompress the files to following format:
filename.bz2 ---> becomes ---> filename
Name: gunzip, uncompress
Description: gunzip can currently decompress files created by gzip, zip, compress or pack. Simillarly use uncompress for *.Z files.
# gunzip [-options] file_name
# uncompress [-options] file_name
Advanced Uncompressing .tgz and .bz2
# tar zxf file_name.tgz
# tar jxf file_name.bz2
or in Solaris:
# gzcat file_name.tar.gz | tar xf -
# bzcat file_name.tar.bz2 | tar xf -
Description:Secure file transfer over the network. To transfer a file/directory use the sftp command.
# sftp -oport=portnumber user_name@remote_host
Note: You will be prompted to enter the password for that remote_host. After entering the password you will get the following prompt:
Example:To use file pathnames to define both the local-file and the remote-file:
sftp> put REPORTS/april reports/month4
This copies the contents of the file april in the sub-directory REPORTS, which is in your current directory on the local host to the file month4 in the sub-directory reports, which is in your login directory on the remote host.
Simillarly. to get a file from a subdirectory on the remote host:
sftp> get reports/quarterly/march tmp/month3
This transfers a copy of the file march in the remote subdirectory reports/quarterly to the local filename month3 in the subdirectory tmp.To get more options try:
Description:To transfer a file/directory you can also use scp (secure copy client).
# scp [-option] [[user@]host[#port]:]file [[user@]host[#port]:]file_or_dir
Example: To copy a file from local host directory "/etc/export/example" to remote computer "mathpost.asu.edu" with user name "user" and to location "/home/user/stuff" use the following command:
# scp -rp /etc/export/example email@example.com:/home/user/stuff
Note: It will prompt you to enter password.
Login to Remote Unix Computer
Description:secure shell client (remote login program).
# ssh -oport=portnumber username@host_name
# ssh username@host_name
Note: This will prompt you to enter the password.
Printing a file
Name: lp, lpr
Description:Print to a particular printer.
# lp -d <printer_name> <file to print>
# lpr -P <printer_name> <file to print>
Note1: To get the names of the printer you have access to, use the following command:
# lpstat -s
# lpstat -s
# lp -P c example.txt
Note2: To print on both sides if the printer support duplex printing:
# lpr -Z duplex <file to print>
Word, Line and Character count
Description:Print byte, word, and newline counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input.
# wc [option] file1 file2
# wc [option] -
Changing the priority of the Job
Description:Run COMMAND with an adjusted scheduling priority. With no COMMAND, print the current scheduling priority. ADJUST is 10 by default. Range goes from -20 (highest priority) to 19 (lowest).
# nice [option] [command [arg] . . .]
Finding out who has logged on
Description:Display information about the current system users.
# who [option] file1
# who [option] arg1 arg2
Sorts contents of a file
Description: Write sorted concatenation of all FILE(s) to standard output.
# sort [option] file1
Clear the Screen
Description:Clear the screen.
Shows the time and date
Description:Print or set system date and time.
# date [option] [MMDDhhmm] [[CC]YY][.ss]]
The date can be displayed as the above format:
Prints a summary of total space occupied by all files in a hierarchy
Description: This command shows you the size of your subdirectories in kilobytes
#du -s *
This command is used summarize the disk use of all the files and directories in home directories