Wednesday, January 15, 2014

UNIX : Some important command

The UNIX operating system is made up of three parts; the kernel, the shell and the programs.

The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.

As an illustration of the way that the shell and the kernel work together, suppose a user types rmmyfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rmmyfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).

The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.

History - The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.

Common Commands

ls                                             list the content of your current working directory
ls -a                                         list hidden files + normal files
ls -l                                          list file details in long format (-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1)
•The left group of 3 gives the file permissions for the user that owns the file (or directory) (ee51ab in the above example);
•the middle group gives the permissions for the group of people to whom the file (or directory) belongs (eebeng95 in the above example);
•the rightmost group gives the permissions for all others.

mkdir                                     make directory
cd directory                          change to named directory
cd                                            change to home-directory
cd ~                                         change to home-directory
cd ..                                        change to parent directory
cd .                                          current directory
cp file1 file 2                         copy file1 to file2
mv file1 file2                        move file1 to file2 or rename file1 to file2
rm file                                    remove file
rmdir directory                    remove directory (make sure directory is empty)
clear                                       clear screen
cat file                                    display the content on the screen at once
If you run the cat command without specifying a file to read, it reads the standard input (the keyboard), and on receiving the 'end of file' (^D), copies it to the standard output (the screen).
less file                                   display the contents of a file onto the screen a page at a time
head file                                writes the first ten lines of a file to the screen
head -5 file                            writes the first five lines of a file to the screen             
tail file                                    writes the last ten lines of a file to the screen
tail -5 file                               writes the last five lines of a file to the screen              
grep “key” file                      It searches files for specified words or patterns
grep -i                                    ignore upper/lower case distinction
grep -v                                   display those lines that do NOT match
grep -n                                   precede each matching line with the line number
grep -c                                    print only the total count of matched lines
wc file                                    print the number of newlines, words, and bytes in files
wc -l                                        print the newlines count
wc -L                                       print the length of the longest line
wc -w                                     print the word counts                        


Redirecting the output
>                                              We use the > symbol to redirect the output of a command (ex:- cat > list 1)
>>                                           The form >> appends standard output to a file. (ex:- to add more names in list1 cat >> list1)

Redirecting the input
<                                              We use the > symbol to redirect the input of a command (ex:- cat > list 1)

Sort list                                  it will sort the list alphabetically or numerically
cmd1 | cmd2                        pipe the output of command1 to the input of command2
wildcard                                 * (any), % (pattern) , ? (exact word)
mancmd                                online description including options
whatiscmd                            description of the command, but omits any information about options
apropos keyword                When you are not sure of the exact name of a command
chmod                                    change permission on a file (Only the owner of a file can use chmod to change the permissions of a file). The options of chmod are as follows (ex - chmoda+rwbiglist)
                                               
Symbol
Meaning
u
user
g
group
o
other
a
all
r
read
w
write (and delete)
x
execute (and access directory)
+
add permission
-
take away permission

quota -v                                 To check your current quota and how much of it you have used, type
df .                                          reports on the space left on the file system
du -s *                                    No. of KBs used by each subdirectory ( -s flag will display only a summary (total size) and the * means all files and directories)
gzip file                                  compress file
tar file                                    unzip file (extract the command)
zcat file                                  read gzipped files without needing to uncompress them first. (ex:- zcat science.txt.gz | less )
diff file1 file2                        compare the content of two files and displays the differences. (Lines beginning with a < denotes file1, while lines beginning with a > denotes file2)
find                                         This searches through the directories for files and directories with a given name, date, size, or any other attribute you care to specify.
                                                Ex:- find . -name "*.txt" -print
                                                find . -size +1M -ls



Process (A process is an executing program identified by a unique PID (process identifier).)
ps                            information about your processes, with their associated PID and status, type

A process may be in the foreground, in the background, or be suspended. In general the shell does not return the UNIX prompt until the current process has finished executing.

Some processes take a long time to run and hold up the terminal. Backgrounding a long process has the effect that the UNIX prompt is returned immediately, and other tasks can be carried out while the original process continues executing.

To background a process, type an & at the end of the command line. For example, the command sleep waits a given number of seconds before continuing. Type

% sleep 10

This will wait 10 seconds before returning the command prompt %. Until the command prompt is returned, you can do nothing except wait.

To run sleep in the background, type

% sleep 10 &                        The& runs the job in the background and returns the prompt straight away, allowing you do run other programs while waiting for that one to finish.
Backgrounding a current foreground process
At the prompt, type

% sleep 1000

You can suspend the process running in the foreground by typing ^Z, i.e.hold down the [Ctrl] key and type [z]. Then to put it in the background, type

% bg

Note: do not background programs that require user interaction e.g. vi

Listing suspended and background processes
When a process is running, backgrounded or suspended, it will be entered onto a list along with a job number. To examine this list, type

% jobs

An example of a job list could be

[1] Suspended sleep 1000
[2] Running netscape
[3] Running matlab

To restart (foreground) a suspended processes, type

% fg %jobnumber

For example, to restart sleep 1000, type

% fg %1

Typing fg with no job number foregrounds the last suspended process.

Killing a process
kill (terminate or signal a process)
It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop)

To kill a job running in the foreground, type ^C (control c). For example, run

% sleep 100
^C

To kill a suspended or background process, type

% kill %jobnumber

For example, run

% sleep 100 &
% jobs

If it is job number 4, type

% kill %4

To check whether this has worked, examine the job list again to see if the process has been removed.

ps (process status)
Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill PID_number

% sleep 1000 &
% ps

PID TT S TIME COMMAND
20077 pts/5 S 0:05 sleep 1000
21563 pts/5 T 0:00 netscape
21873 pts/5 S 0:25 nedit

To kill off the process sleep 1000, type

% kill 20077

and then type ps again to see if it has been removed from the list.

If a process refuses to be killed, uses the -9 option, i.e. type

% kill -9 20077

Note: It is not possible to kill off other users' processes !!!

history
The C shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.

% history (show command history list)

If you are using the C shell, you can use the exclamation character (!) to recall commands easily.

% !! (recall last command)

% !-3 (recall third most recent command)

% !5 (recall 5th command in list)

% !grep (recall last command starting with grep)

You can increase the size of the history buffer by typing

% set history=100


No comments:

Post a Comment