Common Linux Commands

 Linux / UNIX Commands

For a basic introduction to linux / UNIX take a look at this  linux_introduction.pdf

Listing files and directories

Name: ls

Description: You can use the ls command to list the files in a directory.

Syntax: 

# ls [option] directory_name

Useful options:
-a : list all files
-s : list size (in how many 512K blocks)
-l : list in long format
-C : show in columns (default)

Making Directories

Name: mkdir

Description: Create the Directory(ies), if they do not already exist.

Syntax:

# mkdir directory_name 

Changing to a different directory

Name: cd

Description: To change your current working directory.

Syntax: 

# cd pathname

Changing to a different password

Name: passwd

Description:To change your current password.

Syntax:

# passwd LOGIN

Pathnames

Name: pwd

Description:To display the pathname to your current directory.

Syntax:

# pwd

Copying Files / Directories

Name: cp

Description:To copy a directory or a file.

Syntax:

# cp -r directory1 directory2

# cp file1 file2

Note: Here the -r option for recursive copy.

Moving Files / Directories

Name: mv

Description: To move files and directories from one place to another.

Syntax:

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

Syntax:

# 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

Name: cat

Description:The cat command is useful for displaying short files of a few lines. To display longer files use an editor or pager.

Syntax:

# cat file_name

Note: The cat command can also be used to concatinate 2 files:

# cat file1 file2 > file3

Name: vi

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.

Syntax:

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

Syntax:

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

Syntax:

# head file_name

Options:

-n <NUMBER> : To print first NUMBER lines instead of first 10.

# tail file_name 

Command History

Name: history

Description: List history of last 15 commands used.

Syntax:

# history

Example

history

With no options,it displays the history list with line numbers.

Searching the contents of a file

Name: grep

Description:To search a text file for a string of characters or a regular expression.

Syntax:

# grep [option] pattern file_name

Useful options:

-i ignore upper/lower case distinctions
-v display those lines that do NOT match 
-n precede each maching line with the line number 
-c print only the total count of matched lines
-r recursive [linux only] 

Getting Help

Name: man

Description:Type man command to read the manual page for a particular command.

Syntax:

# 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 
w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file
x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriateAccess rights on directories.
allows users to list files in the directory; 
w means that users may delete files from the directory or move files into it; 
means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.

Changing access rights

Name: chmod

Description:To change the access permissions for a file or directory.

Syntax:

# 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
4 2 1These values are added together for any one user category:1 = execute only
2 = write only
3 = write and execute (1+2)
4 = read only
5 = read and execute (4+1)
6 = read and write (4+2)
7 = read and write and execute (4+2+1)So access permissions can be expressed as three digits. For example:

user group others (world)
chmod 640 file rw- r-- ---
chmod 754 file rwx r-x r--
chmod 664 file rw- rw- r--
 

Processes and Jobs

Name: ps

Description:To monitor the state of your processes.

Syntax:

# 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'.

Name: jobs

Description:To display the state of any background jobs that you are currently running use the job command.

Syntax:

# jobs

Example1: To displays the job number, its state and the command associated with this job.

# jobs
[1] + Running xterm -g 90x55
[2] - Running xterm -g 90x55Example2: To displays the job number, its state and the command associated with this job.

# jobs -l
[1] + 3584 Running xterm -g 90x55
[2] - 3587 Running xterm -g 90x55

Placing a foreground process in the background

  1. Suspend the foreground process by using the key combination 'Ctrl-z'.

  2. Enter the bg (background) command to move the process into the background.

  3. View the process by using the jobs command.

For example:# lpr -Pps23 part[1-8].ps

^Z

Stopped

# bg

[1] lpr -Pps23 interface.ps &

# jobs

[1] Running lpr -Pps7 interface.psTo run MATLAB in the background:

  1. redirect the standard input

  2. redirect the standard output

  3. eliminate the graphical output

  4. call MATLAB as a background process

    The script file looks like this:

#!/bin/csh -f

# Clear the DISPLAY.

unsetenv 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

Name: fg

Description:To bring a specific background job into the foreground.

Syntax:

# fg job_number

For example: To bring the third job which is running in the background:

# lpr -Pps23 interface.ps &
# jobs

[1] + Running xterm -g 90x55
[2] - Running xterm -g 90x55
[3] Running lpr -Ppsa interface.ps
# fg 3
lpr -Pps23 interface.ps

Killing processes / jobs

Name: kill

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.

Syntax:

# kill [-signal] process_identifier(PID)

Working:

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

Name: find

Description: To locate a file in the file system , use the find command.

Syntax:

# find path_name -name file_name -print

Example:

# 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

Name: file

Description: Determines the type of content, "file" looks inside the file to find particular patterns in contents

Syntax:

# file

Example:

# 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

Name: tar

Description:To create an archive file or directory use the command.

Syntax:

# tar cvf tarfile_name.tar file_names(directory_name)

Name: zip

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.

Syntax:

# zip file_name

Name: gzip

Description:To compress a file to *.gz format. Usually used to further compress the *.tar file.

Syntax:

# gzip [-options] file_name

Note: More about gzip is obtained by using the command "man gzip".

Name: bzip2

Description:To compress a file to *.bz2 format. Usually used to further compress the *.tar file. This is the best compressing tool of all.

Syntax:

# bzip2 [-options] file_name

Note: More about bzip2 is obtained by using the command "man bzip2".

Name: compress

Description:Compress reduces the size of the named files ( *.Z format).

Syntax:

# compress [-options] file_name

Note: More about compress is obtained by using the command "man compress".

ADVANCED COMPRESSION:

# tar czf file_name.tgz file_names(directory_name)

# tar cjf file_name.bz2 file_names(directory_name)

Uncompressing a file

Name: tar

Description:To unpack a tar file use the tar command.

Syntax:

# tar xvf tarfile_name.tar

Name: unzip

Description:To unzip a zip file.

Syntax:

# unzip file_name

Name: bunzip2

Description:To decompress *.bz2, *.bz, *.tbz2, *.tbz format files.

Syntax:

bunzip2 file_name

Note: bunzip2 will decompress the files to following format:

filename.bz2 ---> becomes ---> filename
filename.bz ---> becomes ---> filename
filename.tbz2 ---> becomes ---> filename.tar
filename.tbz ---> becomes ---> filename.tar

Name: gunzip, uncompress

Description: gunzip can currently decompress files created by gzip, zip, compress or pack. Simillarly use uncompress for *.Z files.

Syntax:

# 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 - 

Transferring files

Name: sftp

Description:Secure file transfer over the network. To transfer a file/directory use the sftp command.

Syntax:

# 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:

sftp>

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:

sftp> help  

Name: scp

Description:To transfer a file/directory you can also use scp (secure copy client).

Syntax:

# 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 user@mathpost.asu.edu:/home/user/stuff

Note: It will prompt you to enter password.

Login to Remote Unix Computer

Name: ssh

Description:secure shell client (remote login program).

Syntax:

# ssh -oport=portnumber username@host_name

or

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

Syntax:

# lp -d <printer_name> <file to print>

Or

# 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

Example:

# lpstat -s
scheduler is running
system default destination: lp
system for lp: localhost
system for c: localhost

# 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

Name: wc

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.

Syntax:

# wc [option] file1 file2

# wc [option] -

Useful options:
-c, --bytes : print the byte counts
-m, --chars : print the character count 
-l, --lines : print the newline counts
-w, --words : print the word counts

Changing the priority of the Job

Name: nice

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

Syntax:

# nice [option] [command [arg] . . .]

Finding out who has logged on

Name: who

Description:Display information about the current system users.

Syntax:

# who [option] file1

# who [option] arg1 arg2

Sorts contents of a file

Name: sort

Description: Write sorted concatenation of all FILE(s) to standard output.

Syntax:

# sort [option] file1

Clear the Screen

Name: clear

Description:Clear the screen.

Syntax:

# clear  

Shows the time and date

Name: date

Description:Print or set system date and time.

Syntax:

# date [option] [MMDDhhmm] [[CC]YY][.ss]]

The date can be displayed as the above format: 
MM : month of the year 
DD : day of the month 
hh : hour of the day 
mm : minutes passed of an hour 
CC : century 
YY : year

Prints a summary of total space occupied by all files in a hierarchy

Name: du

Description: This command shows you the size of your subdirectories in kilobytes

Syntax:

du [option]

Example:

#du -s *

This command is used summarize the disk use of all the files and directories in home directories