P. 1


|Views: 49|Likes:
Published by vinod_kumar_147

More info:

Published by: vinod_kumar_147 on Nov 21, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less





Introduction to Unix commands This is a very brief introduction to some useful Unix commands, including examples of how to use

each command. For more extensive information about any of these commands, use the man command as described below. Sources for more information appear at the end of this document. Commands cal cp find lpr and lp mv rmdir cal This command will print a calendar for a specified month and/or year. To show this month's calendar, enter: cal To show a twelve-month calendar for 2004, enter: cal 2004 To show a calendar for just the month of June, 1970, enter: cal 6 1970 For more detailed information, see the Knowledge Base document In Unix, how can I display a calendar? cat This command outputs the contents of a text file. You can use it to read brief files or to concatenate files together. To append file1 onto the end of file2, enter: cat file1 >> file2 To view the contents of a file named myfile, enter: cat myfile Because cat displays text without pausing, its output may quickly scroll off your screen. Use the less command (described below) or an editor for reading longer text files. For more detailed information, see the Knowledge Base document In Unix, how do I combine several files into a single file? cd This command changes your current directory location. By default, your UNIX login session begins in your home directory. To switch to a subdirectory (of the current directory) named myfiles, enter: cd myfiles To switch to a directory named /home/dvader/empire_docs, enter: cd /home/dvader/empire_docs To move to the parent directory of the current directory, enter: cd .. To move to the root directory, enter: cd / To return to your home directory, enter: cat date jobs ls ps set cd df kill man pwd vi chmod du less and more mkdir rm w and who

Every file (including directories. These categories are abbreviated as u for owner (or user). reporting the size of their contents. write.e. If you already have a file with the new name. for example. The general syntax for cp is: cp -i oldfile newfile To copy a file named meeting1 in the directory /home/dvader/notes to your current directory. To copy a file named oldfile in the current directory to the new name newfile in the mystuff subdirectory of your home directory. Note: You must have permission to read a file in order to copy it. g for group. The .cd chmod This command changes the permission information associated with a file. or execute the file. time.. (period) indicates the current directory as destination. you could lock yourself out or allow others unrestricted access to your account and its contents. df reports the file system device. it's safest to always add -i after the cp command. Note: Be careful with the chmod command. the number of blocks available. all other users.e. enter: date df This command reports file system disk usage. For more detailed information. to force the system to ask for your approval before it destroys any files. and the -i ensures that if there is another file named meeting1 in the current directory. To find out how much disk space is used on each file system. and year. described below. preserving the original and creating an identical copy. abbreviated as r. enter: cp -i oldfile ~/mystuff/newfile The ~ character (tilde) is interpreted as the path of your home directory. you will not overwrite it by accident. .. For this reason. second. date The date command displays the current day. w. (i. date. the number of blocks used. see the Knowledge Base document In Unix. These permissions are broken down for three categories of user: first. enter the following command: df If the df command is not configured to show blocks in kilobytes by default. you can issue the following command: df -k du This command reports disk usage (i. which Unix treats as files) on a Unix system is stored with records indicating who has permission to read. To allow yourself to execute a file that you own named myfile. and finally reporting a total size for all the files it finds. a group with which both the user and the file may be associated. and o for other. how do I change the permissions for a file? cp This command copies a file. the amount of space taken up by a group of files). To see this information. For each mounted file system. and x. The du command descends all subdirectories from the directory in which you enter the command. and third. enter: chmod o+rx myfile You can view the permission settings of a file using the ls command. the owner of the file. the amount of space taken up on mounted file systems). enter: cp -i /home/dvader/notes/meeting1 . cp will overwrite and destroy the duplicate. and the directory where the file system is mounted. enter: chmod u+x myfile To allow anyone who has access to the directory in which myfile is stored to read or execute myfile. If you tamper with the directory permissions of your home directory.

If the job has not been cancelled. for example). For instance. -name "*. enter: find . harsher measures may be necessary. To kill suspended job number three. To exit either less or more. To read the contents of a file named textfile in the current directory. enter: find "$HOME/" -name myfile. This lets you read text without it scrolling quickly off your screen. enter % (percent sign) followed by the number of the job. tcsh. and how do I use it to search through directories for files? jobs This command reports any programs that you suspended and still have running or waiting in the background (if you had pressed Ctrl-z to suspend an editing session. find The find command lists all of the files within a directory and its subdirectories that match a set of conditions.txt -print On some systems.To find out how much disk space your files take up. The less utility is generally more flexible and powerful than more. enter: kill %3 Now check the jobs command again. enter: less textfile The less utility is often used for reading the output of other commands. the . waiting for you to press the Spacebar between screens. To learn which is the case. press q . press q . but more is available on all Unix systems while less may not be. to search for files named myfile. these sizes will be in units of either 512 byte blocks or kilobytes. for example. For example. use the man command. on different systems. . To find all of the files named myfile. enter: jobs Each job will be listed with a number.txt -print For more detailed information. To exit less after viewing the file. enter: %2 This command is only available in the csh. Enter: kill -9 %3 less and more Both less and more display the contents of a file one screen at a time. to resume a job.txt . described below. and ksh shells. you could substitute more for less with similar results. To restart job number two. omitting the final / (slash) after the directory name can cause find to fail to return any results. and enter: du The numbers reported are the sizes of the files. enter: find /home/user/myusername/ -name myfile. (period) represents your current directory. enter: find . for example. bash. As a shortcut for searching in your home directory. to read the output of the ls command one screen at a time. On most systems.txt in the directory /home/user/myusername and its subdirectories. enter: ls -la | less In both examples. kill Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart.txt -print To look in your current directory and its subdirectories for all of the files that end in the extension . see the Knowledge Base document In Unix. It can be replaced by the full pathname of another directory to search. switch to your home directory with the cd command. what is the find command. Use the jobs command to see a list of suspended jobs.txt" -print In these examples. This command is most commonly used to find all of the files that have a certain name. du -k will give sizes in kilobytes. -name myfile.txt in your current directory and all of its subdirectories. For a list of suspended jobs.

and the lp command is used in System V. To see a brief. enter: ls To also see "dot" files (configuration files that begin with a period. If you are unsure how to use a command or want to find out all its options. combine ls with the less utility. enter: lpr -Plp1 myfile To print the same file to the same printer with lp. such as . you might want to try using man to view the manual page. lpr and lp These commands print a file on a printer connected to the computer network. enter: mkdir /tmp/morestuff Note: To make a subdirectory in a particular directory. for example: ls -la | less For more detailed information. Doing so will display garbage and may lock up your terminal. Also see the Knowledge Base document In Unix.login ). multi-column list of the files in the current directory. such as output files produced by compilers. Both commands may be used on the UITS systems. and sizes of all files. enter: man ls To learn more about man. enter: man -k keyword Replace keyword in the above example with the keyword which you want to reference. The lpr command is used on BSD systems. enter: ls -la If the listing is long and scrolls off your screen before you can read it. how do I print files and list or remove print jobs? ls This command will list the files stored in a directory. you can use man with the -k option to help you find the command you need. To print a file named myfile on a printer named lp1 with lpr. enter: man man If you are not sure of the exact command name. how do I list the files in a directory? man This command displays the manual page for a particular command.Note: Do not use less or more with executables (binary files). enter: mkdir mystuff To create a subdirectory named morestuff in the existing directory named /tmp. For more detailed information. see the Knowledge Base document In Unix. to learn more about the ls command. enter: ls -a To see the file permissions. For example. see the Knowledge Base document In Unix. and how do I use it to read manual pages? mkdir This command will make a new subdirectory. what is the man command. To create a subdirectory named mystuff in the current directory. To see one line summaries of each reference page that contains the keyword you specify. enter: lp -dlp1 myfile Note: Do not print to a printer whose name or location is unfamiliar to you. owners. you must have permission to write to that directory. .

e. processes) that are currently running. To remove an entire subdirectory named oldstuff and all of its contents. Like lp and lpr. To remove a file named junk. Enter the command by itself: pwd For more detailed information. .. rm accepts the -r option. in a BSD system. On most systems this will prompt you to confirm the removal of each file. as well as the amount of information about each. it also has many options for determining what processes to display. mv will not preserve the original file. As a precaution. use the arguments: ps -elf For more information about ps refer to the ps man page on your system. see the Knowledge Base document In Unix. However. enter: ls -R oldstuff | less rmdir This command will remove a subdirectory. so that you'll be asked to confirm each file deletion. use the ls command to list the files within the subdirectory you wish to remove. such as firsthw. To browse through a subdirectory named oldstuff. To clean it out. enter: rm -i junk Note: Using rm will remove a file permanently. enter: mv -i oldname newname To move a file named hw1 from a subdirectory named newhw to another subdirectory named oldhw (both subdirectories of the current directory). For example. To rename a file named oldname in the current directory to the new name newname. how do I determine my current working directory? rm This command will remove (destroy) a file. switch to the directory and use the ls and rm commands to inspect and delete files. This behavior can be prevented by adding the -f option. but also to rename files. you would enter: mv -i newhw/hw1 oldhw/firsthw ps The ps command displays information about programs (i. Note: As with the cp command. you would use ps with the following arguments: ps -alxww To display similar information in System V. You should enter this command with the -i option. the options available differ between BSD and System V implementations. enter: rm -rf oldstuff Note: Using this command will cause rm to descend into each subdirectory within the specified subdirectory and remove all files without prompting you. in this last operation. it lists basic information about interactive processes you own. enter: rmdir oldstuff Note: The directory you specify for removal must be empty. so be sure you really want to delete a file before you use rm. Entered without arguments. what do the output fields of the ps command mean? pwd This command reports the current directory path. you should always use -i to make sure you do not overwrite an existing file. To remove a subdirectory named oldstuff. enter: mv -i newhw/hw1 oldhw If. You can use mv not only to change the directory location of a file. to view detailed information about all running processes. you also wanted to give the file a new name.mv This command will move a file. Use this command with caution. set This command displays or changes various settings and options associated with your Unix session. To remove a non-empty subdirectory. as it is very easy to accidently delete important files. Also see the Knowledge Base document In Unix. Unlike the cp command.

8. Note: You cannot kill processes that are running on a computer different from the one you are logged into. you should probably look at a tutorial. To edit a file named myfile in the current directory. Steel. vi This command starts the vi text editor. da Vinci. you must switch from command mode to ex mode by pressing : (the colon key): Enter w to save. follow the steps below: 1. enter: tty 2. You should see something like this: PID 4. SIGHUP is good because it allows applications like Elm and Emacs to exit gracefully. 7.) before a process name shows that the process is a login shell. how do I kill another login session remotely? You can kill a Unix login session remotely by sending a hangup signal (SIGHUP) to the process running the login session.To see the status of all settings. ps -fu username Replace username with your 3.. see the man entries for set and the references listed at the end of this document for more information. 13964 v5 13126 ue 13133 ue I S R 0:00 elm 0:00 -bash (bash) 0:00 ps x TT STAT TIME COMMAND 13335 v5 S 0:00 -bash (bash) In the first column. how do I cancel a batch job? . wq to save and quit. w and who The w and who commands are similar programs that list all users logged into the computer. "PID" stands for "process ID". combine set with less: set | less The syntax used for changing settings is different for the various kinds of Unix shells. you also get the IP numbers or computer names of the terminals they are using. all the processes that were started in the shell will be killed as well (unless they were in the background). from your Unix shell prompt. For example. you need to switch the program from command mode to insert mode by pressing i . Then issue the following command: kill -HUP processid Replace processid with the process ID number you identified. look for the processes with a dash and choose the process number that is not for your current tty. If you use w. To execute any of the following commands. This rule extends to clusters of Unix systems as well (e. To do this. enter the command without options: set If the output scrolls off your screen. 6. such as the Knowledge Base document How do I use the vi text editor? Another helpful document for getting started with vi is A quick reference list of vi editor commands. To determine your current tty. When you send a SIGHUP (by entering kill -HUP or kill -1) to a login shell. This means that you cannot kill a process running on the Steel node Steel1 if you are logged into Steel2. To navigate around the document with the cursor keys. enter: username. enter: vi myfile The vi editor works fairly differently from other text editors. The dash ( . 5. leaving your files intact.g. you must switch back to command mode by pressing Esc. The very least you need to know to start using vi is that in order to enter text. If you use who. In Unix. Libra. In Unix. If you have not used it before. you also get a list of what they are doing. To show all of your running processes. Identify the shell you want to kill. The second column shows the tty to which your processes are connected. q! to quit without saving. at Indiana University you can't kill processes running on Libra from your Steel account. To remove the remote shell. and Nations).

On System V implementations (including all UITS central systems at Indiana University). For more information about the nice command. at the Unix prompt. The percentage (29. enter: atq If your job is already running. On some systems. enter: man nice In Unix. At the Unix prompt. you can get a listing of your jobs by entering: at -l Each job will be listed with its job number queue and the time it was originally scheduled to execute. How do I run a Unix process in the background? In Unix.96 29.96 seconds of real time.0% 2+5k 0+1io 0pf+0w The program myprog used 1. ksh. For more information.406 seconds of user time. Following are some examples: . To run a process in the background.In Unix.18106 15852 rb 24980 rb 24981 rb 22311 rf 0:22 elm In this example. you will need to find the process ID and kill it. You will see something like the following: PID TTY TIME COMMAND 10005 ? 3:03 a.63% 15. such as NQS and LoadLeveler.406u 0. If it still will not terminate. The following is an output example for users in the csh or tcsh shells: 1. To use this command.80% desert. leaving the terminal free for other work. you can see CPU usage on a job that is running in a number of ways. 0. the "TIME" column shows that the process running Elm has used 22 CPU seconds.042 seconds of system time.18106 0:02 /usr/local/bin/emacs /usr1/mmouse/snd. work differently and are not controlled by the same methods. you can use the time command. you can cancel it at the Unix prompt. if you scheduled a job with at or batch. and the "TIME" column shows that the program desert. If you don't remember the job number. The top command You may also use the top command. try entering: kill -9 <process ID> Note: Other scheduling programs. enter: top You will see something similar to the following: PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND 28000 mmouse 27999 mmouse 96 96 4 4 276K 144K run 276K 144K run 292:20 16. list all running processes by entering: ps -fu username Replace username with your username.02% denver. enter: ps -u username Replace username with your username.80% 16. you may use atrm instead of at -r . The sum of the user and system times is the total CPU time of the process. On some systems. or bash. as described below: The time command If you want to see a grand total of CPU time for a program when it finishes running.exe 292:42 16.exe has used 292 minutes and 20 seconds of CPU time. At the Unix prompt.out 0:01 -tcsh (tcsh) 0:00 sh -c /usr/local/bin/emacs /usr1/mmouse/snd. you can use the nice command to lower your program's priority. At the Unix prompt.exe 19004 goofy 96 4 428K 160K run 357:11 15. by entering: at -r <jobnum> Replace <jobnum> with the number of the job that at or batch reported when you submitted the job. The ps command You can also use the Unix command ps. since the time command is not built into those shells. The equivalent BSD command is: ps x Once you have the process ID. enter: kill <process ID> Replace <process ID> with the process ID. a background process executes independently of the shell. In Unix.63% diskr User mmouse is at the top of the list. include an & (an ampersand) at the end of the command you use to run the job. at the Unix prompt.02% 16. how do I check the CPU usage of a job? Note: If you are concerned about slowing the system down. enter: time myprog Replace myprog with the name of the program you are running.0%) indicates the percentage of the CPU's time that the process used while it ran. see the appropriate man pages.042s 0:04. This is the most interactive way to see CPU usage. The output will appear in a slightly different format when using sh. the atq command is available to list all the jobs on the system. and 4.

No version of ps will display all of these fields. enter: ps You can bring a background process to the foreground by entering: fg If you have more than one job suspended in the background. Redirect the output to /dev/null using the following syntax: count 2> /dev/null & Unix job control command list The following table lists the basic Unix job control commands: Command Explanation & Ctrl-z jobs Run the command in the background Stop the foreground process List background processes Example % long_cmd & [Ctrl-z] Stopped % jobs [1] . most ps variants are rooted enough in either the System V or BSD syntax that entering ps -elf (System V) or ps alx (BSD) will produce something like the following: F S UID ID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME COMD 1 R obiwan 792 779 22 183 20 10ec5f80 29 12:52:24 pts/2 0:00 ps -elf 1 S root 24621 560 0 154 20 13603f80 11 4697c0 Jun 16 ttyp2 0:00 telnetd 1 S dvader 1162 1153 0 154 20 110a1f80 77 452be4 11:25:41 pts/3 0:00 ssh deathstar This particular example is from HP-UX. Note: For security reasons. Column Header Contents . Each vendor incorporates its own flags and outputs the results differently. however.Stopped vi [2] . what do the output fields of the ps command mean? The ps command varies significantly among Unix implementations. you may prevent background processes from sending error messages to the terminal. enter: jobs -l • If you are using sh. copyright 1995. O'Reilly & Associates. However.big_job & % fg %1 % fg %?ls % bg [2] big_job & % kill %2 host2>~[Ctrl-z] Stopped host1> host3>~~[Ctrl-z] Stopped host2> %n %?str bg fg kill ~ Ctrl-z Refers to the background number n Refers to the background job containing str Restart a stopped background process Bring a background process to the foreground % fg %1 Kill a process Suspend an rlogin or ssh session ~~ Ctrl-z Suspend a second level rlogin or ssh session This table is adapted from Essential System Administration.• • • • • To run the count program. enter the following: kill -KILL PID • To determine a job's PID. as shown in the first column of the output of the jobs command. whose output is basically vanilla System V. which will display the process identification number of the job. rlogin is not available on UITS computers at Indiana University. The following table describes the meanings of the columns that commonly appear in ps outputs. enter: count & To check the status of your job. by Aeleen Frisch. In UNIX. or zsh. ksh. Inc. If that fails. bash. You can kill a background process by entering: kill PID Replace PID with the process ID of the job. enter: fg %# Replace # with the job number.

if any nice value Flags Process ID number ID number of the process's parent process Priority of the process Real memory usage Process status code Time when the process started Virtual memory usage Total CPU usage Terminal associated with the process Username of the process's owner Memory address of the event the process is waiting for = Often abbreviated For information specific to your Unix implementation.%CPU %MEM ADDR C or CP COMMAND* NI F PID PPID PRI RSS S or STAT START or STIME SZ TIME TT or TTY UID or USER WCHAN * How much of the CPU the process is using How much memory the process is using Memory address of the process CPU usage and scheduling information Name of the process. . including arguments. consult the ps man page.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->