Monday, August 6, 2012

Introduction to Unix commands


The man Command

To obtain information on any Unix command, use the on-line manual pages.
        Syntax: man command
If you do not know the name of the actual command you need, execute man with the -k option.
        man -k keyword
For example, to find out all the commands related to directories, type
        man -k directory
This command outputs information to your screen about the commands that pertain to directories, along with a brief description of each command's functions. To access man pages for local utilities, add/usr/local/man to your path variable.

 Printing a Hardcopy of a man page

To print a hardcopy of a manual page, redirect the output to a file, and then send this file to a printer using one of the printing commands, as in the following example:To generate a file for hardcopy output of the ls command, type
        man ls > ls.txt        (SGI, Sun, HP Alpha)
You can then send the formatted file to a printer using qpr ls.txt.

Directory Commands


The Unix file system allows users to create several levels of subdirectories for logically grouping files. This section describes the basic utilities for creating these directories and moving around within them.

Creating a Directory (mkdir)

The mkdir command allows you to create directories.
    Syntax: mkdir [options] directory
From a login directory, such as /users/jones, create a subdirectory, memos, with the following command.
    mkdir memos
This creates the directory, /users/jones/memosYou can create multiple directories with one command. From the login directory, /users/jones, the command,
    mkdir projects samples
creates the directories, /users/jones/projects and /users/jones/samples. You can also specify a directory that is not under your current directory by specifying its absolute position. From the directory,/users/jones/samples, the following command would create the directory, /users/jones/memos/old.
    mkdir /users/jones/memos/old

3.2.2 Removing Directories (rmdir)

To remove a directory, use the rmdir command. Note that you must first delete all files within a directory before deleting the directory itself.
    Syntax: rmdir directory
From the directory, /users/jones/memos, the following command would delete the subdirectory, old.
    rmdir old
To remove that same directory from the directory, /users/jones, use the command
    rmdir memos/old
An alternative is the recursive option with the remove command (rm).
    Syntax: rm -ri directory
Note: Use the above command with extreme caution, as it will very quickly delete every file and subdirectory below the specified directory.

3.2.3 Moving around Directories (cd)

To move around in your directory structure, use the change-directory command, cd. The cd command specified by itself without any arguments will always return you to your login directory.
    Syntax: cd directory
For example, from the main directory, /users/jones, change to the sub-directory, /users/jones/memos, with the command
    cd memos
From the subdirectory, /users/jones/memos, change to the directory, /users/jones/samples with the command,
    cd /users/jones/samples

3.2.4 Directory Shortcuts

You can use shortcuts when specifying your directory pathname. Examples here show how to use these shortcuts with the cd command, but they can be used with any of the Unix commands when specifying filenames. Your home directory can be used as a starting point when specifying pathnames by using a tilde (~). The tilde instructs the shell to start the pathname from your home directory.For example, from the directory, /users/jones/memos/old, change to the subdirectory, /users/jones/samples with the command
    cd ~/samples
You can also use cd to move to the home directory of another user.
    cd ~username
In addition, use the .. notation for moving around directories. The .. refers to the parent directory of your current directory.For example, from the directory, /users/jones/memos, change to the subdirectory, /users/jones/samples, with the command,
    cd ../samples
The .. says to move up to the parent directory, in this case /users/jones, and then down to the sub-directory, samples. You can also use cd .. to back up from subdirectories level by level.

3.2.5 Present Working Directory (pwd)

When moving around the Unix file system, it is helpful to check your present working directory, with the pwd command.
    Syntax: pwd

On-line Information

    man mkdir
    man rmdir
    man cd
    man pwd
    man rm
     (see option -ri)


 Return to top of page 

3.3 File Commands


3.3.1 Listing Directory Files (ls)

To list the files in any directory, use the ls command.
    Syntax: ls [options] [filename_spec]
The ls command by itself will list the files in your present working directory. By default, the ls command lists only the names of the files in the directory. The following examples show some of the more commonly used options.
  1. To list all the Unix hidden files, such as .login or .cshrc in
    your home directory, enter
      ls -a
  2. For a long listing that shows file protections, size, and date,
    enter
      ls -l
  3. To recursively list all the files in the current directory and
    any subdirectories of the current directory, enter
      ls -R
  4. It is sometimes useful to use the ls command in conjunction
    with other Unix commands. For instance, if there are too many files
    in a directory to fit on one screen, the output can display to the
    screen a page at a time.
  5. To search for a subset of files, use the ls command with
    the Unix search command, grep. To find all files written
    on a particular date, enter
      ls -l |grep 'Jun 25'

3.3.2 File Protections (chmod)

Following are the protections that can be set on a file with their symbolic or numeric representations.
Symbolic    Numeric    Protection
  r             4      Read access 
  w             2      Write access
  x             1      Execute access
With the chmod command, you can modify protections on a file for three classes of users: the user (u), group (g), or others (o) by entering
    Syntax: chmod ugo filename
The protection for each class of user can be obtained by adding the numerical equivalent for each type of access. For instance, to give the owner read, write, and execute access, it would be 4+2+1 or 7. To give group read and write access to the file would be 4+2 or 6, and to give world read access would be 4. The resulting command would be
    chmod 764 filename
You can use wild card characters in the filename specification, or specify multiple files separating them by spaces.The following sample listing of a file shows owner with read/write access and group and world with read only. The command, chmod 644 would produce these protections.
    -rw-r--r-- 1 waukau 5417 Jun 6 05:28 test
To include execute access to this file for all user classes, enter chmod 755, which produces these protections.
    -rwxr-xr-x 1 waukau 5417 Jun 6 05:28 test
You can also use the symbolic representation for adding or removing access to files. For example, to give execute access to all categories (user, group, and others), enter
    chmod +x filename
To remove write access to a file for group and others, enter
    chmod g-w, o-w filename

3.3.3 Default File Protections (umask)

You can set up a default file protection for all new files created by using the umask command in your .login file.
    Syntax: umask ugo
The access modes specified in the umask command are the complement of the chmod command. You specify those access modes you do not want a particular class to have, as in the following examples.
  1. To remove write access for group and others, specify
    umask 022
  2. To remove all access for group and others, specify
    umask 077

3.3.4 Copying Files (cp)

Use the cp command to make a copy of a file.
    Syntax: cp [options]    old_filename    new_filename

3.3.5 Renaming Files (mv)

To rename a file, use the mv command.
    Syntax: mv  [options]    old_filename    new_filename

3.3.6 Deleting Files (rm)

To delete a file or files, use the rm command.
    Syntax: rm [options] filename
You can use wild cards to remove multiple files. When deleting with wild cards, it is best to have the rm command ask you whether you want to delete each individal file. To do this, enter
    rm -i *.f
You are prompted for a response for each file that matches this criterion (in this case, all files that have the suffix, .f). If you answer y, the file is deleted. If you answer n (or any other response), the file is not deleted.

3.3.7 File Searches (grep)

At times, it is necessary to search through your files for a particular string of characters. The command to perform searches is grep.
    Syntax: grep  string file1  [file2 . . .]
By default, the grep command matches the letter case of the string specified, as in the following examples.
  1. To search all files in the directory for the string, "May," enter
    grep May *
  2. To find any occurrence of the string, "May," regardless of letter
    case, use the -i option.
    grep -i May *
    This would find strings such as "may," "maybe," or "MAY," along
    with any other occurrence of that string.
It is useful to pipe (|) the output of the grep command into the more command to see the listing one screen at a time, as shown below. For further information on pipes, see Section 3.7.1.
    grep -i May * | more
Note: Certain special characters must be "escaped" with a "\" when searching for them in regular expressions.

3.3.8 Viewing Files (more, cat, head and tail)

There are a number of commands that can be used for viewing files in the Unix operating system. This section will describe a few of these.

The more Command

The more command outputs a file to the screen a page at a time. To view the next page, press the space bar. To see just one new line at a time, press RETURN. The more utility also allows you to perform some basic editing functions such as string searches, moving forward and backward in the file, and entering the vi editor. CTRL-c aborts a more session.
    Syntax: more [options] filename
Following are some helpful options to use with the more utility.q       Exit immediately from more.
ib      Move backward by screens, where i is the number
        of screens

if      Move forward by screens, where i is the number
        of screens

/string Search forward for string. Backward searches are not
        possible.

The cat Command

The cat command will also display file contents to a screen.
    Syntax: cat filename
Also, use cat for quickly creating a short file, by entering the following.
    cat >  filename
After pressing the return key, type in the text. To save and exit the file, press CTRL-D.An additional feature of cat is that it allows you to concatenate two files together by entering the following
    cat file1 >> file2
This command appends file1 to the end of file2.

The head Command

To view the first ten lines in a file, use the head command.
    Syntax: head [-countfilename
If the -count option is specified, you can display more than ten lines. The following command would display the first 20 lines.
    head -20  filename

The tail Command

To view the last ten lines in a file, use the tail command.
    Syntax: tail [-countfilename
If the -count option is specified, you can display more than ten lines. The following command would display the last 20 lines.
    tail -20 filename
It is helpful to "pipe" the head and tail command outputs into the more command when -count exceeds 20.  See Section 3.7.1.

3.3.9 Pattern Matching

In the previous sections, examples were given for matching exact strings. There are also certain metacharacters available in Unix that allow more flexibility in performing string searches.

Matching Any Single Character in a String Expression

You can use a period (.) to match any single character in an expression, and concatenate periods to represent an exact number of characters, as in the following example.To search a file for dollar amounts in the ten-thousand-dollar range, enter
    grep 10,... proposal
This would find matches such as 10,234 and 10,456. However, 10234, typed without a comma, would not match.

Matching Any Single Character in a File Name Specification

When specifying file names, use a question mark (?) to match any single character, and concatenate question marks to represent an exact number of characters. For example, entering
    ls -l plot?.dat
would find files such as plot1.datplot2.dat or plots.dat. It would not find the file plot22.dat.

Matching Any Number of Occurrences of a Character

In an expression or filename specification, an asterisk (*) stands for any string of characters, even a null one. For example, to find all derivatives of the word meteorology in a file, enter
    grep 'meteor*' filename
This would show occurrences of the words, "meteorological", "meteorologist", "meteors", etc.As another example, to find all files with the file extension .f, enter
    ls -l *.f

On-line Information

    man ls
    man chmod
    man umask
    man cp
    man mv
    man rm
    man grep
    man more
    man cat
    man head
    man tail


 Return to top of page 

3.4 Process Monitoring


It is important to recognize that in an X Window environment, a single user can generate a minimum of five processes just by logging on to a system, and can expand this number exponentially, depending on the number of windows open and software being executed. Therefore, you need to be conscientious about the number of CPU and memory resources you are using. This section will describe some of the Unix commands that you can use to monitor your processes.

3.4.1 The ps Command

The ps command is a general utility for checking process status. It reports information such as current percentage of the CPU being used by processes, percent of memory, accumulated CPU time, and other statistics. It is not a real-time display, but takes a snapshot of the system.
    Syntax: ps [options]
The ps command specified by itself will display your current processes. To display information about all processes on the system, enter
    ps -ef (on SGI, Sun, and HP Alpha)
If there is more information than will fit on one screen, use the ps option with the more utility, as follows:
    ps -aux | more
To find information about a specific user on the system, use the ps command with the grep search utility, as follows:
    ps -aux | grep username
Below is a sample output from the ps command and a table that explains the information displayed. 
UIDPIDPPIDCSTIMETTYTIMECMD
root15710Mar 18?0:03/usr/sbin/inetd -s
root26010Mar 18?0:02/usr/sbin/vold
smith1056610564007:02:31pts/40:01/usr/local/bin/tcsh
jones1174111739111:12:30pts/80:00-csh
smith1064410510007:09:25pts/20:14emacs NEW_CUTILS.c
brown1165611440010:53:33pts/70:00idt -soft ppi-cesar.cgm
ColumnDescription
UIDUser ID of process
PIDProcess id number
PPIDProcess ID of parent process
CProcessor utilization for scheduling (obsolete)
STIMEStarting time of process
TTYControlling terminal of process, ? when no controlling terminal
TIMECumulative execution time for the process
CMDCommand name

The columns and information displayed varies between architectures.  Use the man command to check for specifics.

3.4.2 The top Command

The top command is a pseudo-real-time command that is useful for examining current processes on the system and for changing process priorities.
    Syntax: top
To exit the top command, type q. To show other options, type h.

3.4.3 The who and w Commands

Other commands to determine who is currently on the system are the who and w commands. The w command provides additional information about login and idle times for different processes.
    Syntax: who
    Syntax: w

3.4.4 Killing Processes

There are a number of ways to kill processes on a workstation. To kill a job that is in the foreground, press CTRL-c.Use the kill command along with the process ID (PID). (PID information can be obtained from the ps command).
        Syntax: kill -9 pid
To kill a background job, use the jobs command to find its job ID (n) and enter
    kill -9 %n
Another useful utility is pkill, available on the Sun, Linux, and SGI architectures. Your current processes will be listed to the screen one at a time sorted by the age of the process, with your oldest process (most likely your login shell) appearing first. You will be given the option to kill each process. Responding yes to the inquiry will kill the process; a carriage return will retain the process.
    Syntax: pkill

3.4.5 Checking the Load on Other Workstations

If a workstation appears to be heavily loaded, you can check the load on other workstations with the ruptim command.
    Syntax: ruptim [workstation]
Following is a sample output from this utility.
oak       number of users:  1   load average:     0.26,   0.16,   0.00
fir       number of users: 15   load average:     0.26,   0.06,   0.00
pine      number of users: 25   load average:     0.21,   0.03,   0.01
juniper   number of users: 15   load average:     0.39,   0.12,   0.01
cypress   number of users:  9   load average:     0.22,   0.04,   0.00
balsam    number of users: 15   load average:     0.18,   0.15,   0.00
walnut    number of users:  3   load average:     2.00,   1.94,   1.56
beech     number of users:  4   load average:     1.17,   1.15,   1.00
The last three columns are 1-, 5-, and 15-minute averages. Values indicate whether the CPU is underutilized (< 1.0), 100% utilized (1.0), or processes are waiting (> 1.0). If you receive the error message,permission denied when executing this command, you need to modify your .rhosts file. The .rhosts file should include the systems being checked as well as the system you are logged on to.

3.4.6 Changing Process Priority

All Unix processes have a dynamically calculated priority. This priority changes over time as a process accumulates CPU time. To execute a program that will use several minutes of CPU time over a short wall-clock period, change the process priority so that it does not monopolize the machine. To do this, use the nice command when executing your process:
    Syntax: nice -number argument
The higher the number specified, the lower the priority of the process. The default is 10, and you can specify numbers as high as 20. Note that you can only increase this number. For example, a process started with a value of 18 cannot be lowered to 10 with nice -10, except by root.For instance, to execute a program called a.out at a low priority, enter
    nice -n 19 a.out 
If you have already started a process without the nice command, you can use the renice command to change the priority of the process.
    Syntax: renice -number process_id
Processes can also be reniced from the top command (see Section 3.4.2) by typing r followed by the priority number and process ID (PID).

On-line Information

    man ps
    man top
    man who
    man w
    man kill
    man nice
    man renice


 Return to top of page 

3.5 Disk Usage


3.5.1 The df Command

The df command lists the current total disk usage for all file systems accessible by your workstation.
    Syntax: df -k

 The du Command

To determine your current disk quota usage, use the du command.
    Syntax: du [options] directory
By default, the du command by itself will give a summary of quota usage for the directory specified and all subdirectories below it.
    du /users/username
For a summary of total disk usage, use the -s option.
    du -s /users/username
For a summary of your own disk usage, enter
    du -s ~

The quota Command

On the Sun, HP Alpha, and Linux workstations, the quota command displays your current usage. This command does not display relevant information on the SGI systems.
    Syntax: quota -v username

On-line Information

    man df
    man du
    man quota

Command History


The shell keeps a history list of recently executed commands if you instruct it to do so. The following line should be placed in your .login and .cshrc files.
    set history=nn
where nn is the number of lines you wish to retain; 40 is a good number. There are a number of ways to re-execute commands maintained in the history file. !! entered on the command line executes the most recently executed command.If you enter the history command, a list of your most recently executed commands will be displayed with a line number next to each command. To re-execute a specific command, enter
        !line_number
Another mechanism for recalling a previously executed command is to enter the first few letters of the previous command. For example, if you had previously entered a command to recursively look through your directory structure for all directory files, using
    ls -lR | grep drw
You could recall that command with the following.
    !ls
The shell would then reexecute the most recent command line that began with ls. 

Unix Tools


The Unix operating system provides a variety of utilities that allow you to perform functions such as combining multiple commands and redirecting your output.

Pipes

Pipes allow you to connect various combinations of Unix commands together. The pipe symbol is the vertical line, |, on your keyboard.For example, to search a directory listing for a particular string, and output the information to the screen a page at a time, enter
    ls -l | grep 'string' | more
To search the current system processes for a particular user, enter
    ps -ef | grep username

 I/O Redirection

In general, Unix commands send output to "standard out" (stdout), your terminal screen. At times, you may want this information written to a file instead. Unix allows you to either redirect output to a file or to have a Unix command read input from a file. The symbol > outputs information to a file, and < reads input from a file.For example, to perform a search on a directory listing and write that output to a file called "source.txt," enter
    ls -l |grep '*.f' > source.txt
To have the Mail utility read in information from a file called "input.txt," enter
    mail user@host < input.txt

The sort Command

The sort utility sorts the lines in a file. By default, it sorts the file in ASCII order, with numbers preceding alphabetic characters.
    Syntax: sort filename
You can also use the sort command in combination with other commands using pipes. To sort the output from a who command, enter
    who | sort

File Differences (diff)

It is sometimes useful to check the differences between two files. The diff command performs this function.
    Syntax: diff [options] file1   file2

Counting Lines, Words, and Characters (wc)

It is often useful to know the number of lines, words, and characters in a file. The wc utility accomplishes this task. By default, it prints out all three of these fields.
    Syntax: wc [options] filename
This command is useful when combined with other commands. For example, to find the number of files in a directory, enter
    ls -l | wc -l

On-line Information

    man sort
    man diff
    man wc


Miscellaneous Utilities


There are several general-purpose utilities developed either within MMM or obtained from other sources. This section briefly describes some of these commands.

The phone Command

The phone utility accesses the on-line NCAR phone directory. It performs a search on an ASCII file and takes a string as its argument.
    Syntax: phone string
For example, to search for information on a person with the last name of jones, enter
    phone jones
For numbers of all MMM staff, enter
    phone mmm | more

NCAR Central E-mail Database

To obtain e-mail information for individuals at NCAR that are not in the MMM Division, you can also access the NCAR central e-mail database with the following procedure.
  1. Enter telnet directory
  2. At the login prompt, enter directory
  3. A menu will appear. Choose the appropriate option for searching for
    an individual name.
Some non-NCAR individuals may also be listed in this database, especially if they have an SCD account.

The ispell Command

The ispell utility checks a file for misspelled words. To use this command, enter
    Syntax: ispell filename
The ispell utility will flag each word that it does not find in its dictionary. The flagged word will be displayed at the top of the screen followed by a list of numbered words that are possible options.To select one of the suggested spellings, enter the number of the appropriate word. If none of the suggested words is correct, and you wish to enter a replacement word, enter R. If the flagged word is correct, either press the space bar or enter I to put the word in your personal dictionary.When using ispell on a TeX file, use the -t option.
    Syntax: ispell -t filename

The mmminfo Command

As there are a large number of systems of varying architectures within the division, we have developed a utility for checking to find out what architecture a particular machine is as well as other pertinent information such as location and IP address. To find out information about a particular system enter the mmminfo command.
    mmminfo string
where string can be the information you want to check on, such as a system name, IP address, or individual's name. This will display basic information about the system. If you want information related to networking use the -n option. For complete information on a system enter the -a option. There is also a man page available for the mmminfo command.

The assist Command

Assist can also be accessed through our website. 

The assist utility will allow users to report problems or view problems that have been reported.
    assist options
Following is a list of the available options. More detailed information can be found in the man pages man assist .-e editor
        To log a problem, use the -e  option, where the editor is
        either "vi" or "emacs". It will ask you specific questions and
        place you in an editing session to type in information. After you
        have submitted a problem report, you should receive mail stating
        that your report has been received and informing you of a
        reference ID for future reference.

 -s     Displays a listing of short-term entries in the assist database.
        Short-term entries are those that will be addressed within a short
        time frame (a day to a couple of weeks).

 -l     Displays a listing of all requests in the assist database.
 -r request-id
        List a complete description of specified request.

 -u username
        List brief descriptions of all requests submitted by the specified
        user.

 -w     Show who is on-duty.
        This person is responsible for monitoring
        the assist database for that day, and can also

        be contacted directly for immediate problems. 

Following 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.
On this page:

cal
cat
cd
chmod
cp
date
df
du
find
jobs
kill
less and more
lpr and lp
ls
man
mkdir
mv
ps
pwd
rm
rmdir
set
vi
w and who

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 2008, enter:
cal 2008
To show a calendar for just the month of June 1970, enter:
cal 6 1970

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.

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

chmod

This command changes the permission information associated with a file. Every file (including directories, which Unix treats as files) on a Unix system is stored with records indicating who has permission to read, write, or execute the file, abbreviated as r, w, and x. These permissions are broken down for three categories of user: first, the owner of the file; second, a group with which both the user and the file may be associated; and third, all other users. These categories are abbreviated as u for owner (or user), g for group, and o for other.
To allow yourself to execute a file that you own named myfile, enter:
chmod u+x myfile
To allow anyone who has access to the directory in which myfile is stored to read or execute myfile, enter:
chmod o+rx myfile
You can view the permission settings of a file using the ls command, described below.
Note: Be careful with the chmod command. If you tamper with the directory permissions of your home directory, for example, you could lock yourself out or allow others unrestricted access to your account and its contents.

cp

This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's safest to always add  -i  after the cp command, to force the system to ask for your approval before it destroys any files. 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, enter:
cp -i /home/dvader/notes/meeting1 .
The  .  (period) indicates the current directory as destination, and the  -i  ensures that if there is another file named meeting1 in the current directory, you will not overwrite it by accident.
To copy a file named oldfile in the current directory to the new name newfile in the mystuff subdirectory of your home directory, enter:
cp -i oldfile ~/mystuff/newfile
The  ~  character (tilde) is interpreted as the path of your home directory.
Note: You must have permission to read a file in order to copy it.

date

The date command displays the current day, date, time, and year.
To see this information, enter:
date

df

This command reports file system disk usage (i.e., the amount of space taken up on mounted file systems). For each mounted file system, dfreports the file system device, the number of blocks used, the number of blocks available, and the directory where the file system is mounted.
To find out how much disk space is used on each file system, 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.e., the amount of space taken up by a group of files). The du command descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it finds.
To find out how much disk space your files take up, switch to your home directory with the cd command, and enter:
du
The numbers reported are the sizes of the files; on different systems, these sizes will be in units of either 512 byte blocks or kilobytes. To learn which is the case, use the man command, described below. On most systems, du -k will give sizes in kilobytes.

find

The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.
To find all of the files named myfile.txt in your current directory and all of its subdirectories, enter:
find . -name myfile.txt -print
To look in your current directory and its subdirectories for all of the files that end in the extension .txt , enter:
find . -name "*.txt" -print
In these examples, the  .  (period) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, enter:
find /home/user/myusername/ -name myfile.txt -print
On some systems, omitting the final  /  (slash) after the directory name can cause find to fail to return any results.
As a shortcut for searching in your home directory, enter:
find "$HOME/" -name myfile.txt -print

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, for example). For a list of suspended jobs, enter:
jobs
Each job will be listed with a number; to resume a job, enter  %  (percent sign) followed by the number of the job. To restart job number two, for example, enter:
%2
This command is only available in the cshbashtcsh, and ksh shells.

kill

Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job number three, for example, enter:
kill %3
Now check the jobs command again. If the job has not been cancelled, harsher measures may be necessary. Enter:
kill -9 %3

less and more

Both less and more display the contents of a file one screen at a time, waiting for you to press the Spacebar between screens. This lets you read text without it scrolling quickly off your screen. The less utility is generally more flexible and powerful than more, but more is available on all Unix systems while less may not be.
To read the contents of a file named textfile in the current directory, enter:
less textfile
The less utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:
ls -la | less
In both examples, you could substitute more for less with similar results. To exit either less or more, press . To exit less after viewing the file, press .
Note: Do not use less or more with executables (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal.

lpr and lp

These commands print a file on a printer connected to the computer network. The lpr command is used on BSD systems, and the lp command is used in System V. Both commands may be used on the UITS systems.
To print a file named myfile on a printer named lp1 with lpr, enter:
lpr -Plp1 myfile
To print the same file to the same printer with lp, enter:
lp -dlp1 myfile
Note: Do not print to a printer whose name or location is unfamiliar to you.

ls

This command will list the files stored in a directory. To see a brief, multi-column list of the files in the current directory, enter:
ls
To also see "dot" files (configuration files that begin with a period, such as .login ), enter:
ls -a
To see the file permissions, owners, and sizes of all files, enter:
ls -la
If the listing is long and scrolls off your screen before you can read it, combine ls with the less utility, for example:
ls -la | less

man

This command displays the manual page for a particular command. If you are unsure how to use a command or want to find out all its options, you might want to try using man to view the manual page.
For example, to learn more about the ls command, enter:
man ls
To learn more about man, enter:
man man
If you are not sure of the exact command name, you can use man with the  -k  option to help you find the command you need. To see one line summaries of each reference page that contains the keyword you specify, enter:
man -k keyword
Replace keyword in the above example with the keyword which you want to reference. Also see In Unix, what is the man command, and how do I use it to read manual pages?

mkdir

This command will make a new subdirectory.
To create a subdirectory named mystuff in the current directory, enter:
mkdir mystuff
To create a subdirectory named morestuff in the existing directory named /tmp, enter:
mkdir /tmp/morestuff
Note: To make a subdirectory in a particular directory, you must have permission to write to that directory.

mv

This command will move a file. You can use mv not only to change the directory location of a file, but also to rename files. Unlike the cp command,mv will not preserve the original file.
Note: As with the cp command, you should always use  -i  to make sure you do not overwrite an existing file.
To rename a file named oldname in the current directory to the new name newname, 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), enter:
mv -i newhw/hw1 oldhw
If, in this last operation, you also wanted to give the file a new name, such as firsthw, you would enter:
mv -i newhw/hw1 oldhw/firsthw

ps

The ps command displays information about programs (i.e., processes) that are currently running. Entered without arguments, it lists basic information about interactive processes you own. However, it also has many options for determining what processes to display, as well as the amount of information about each. Like lp and lpr, the options available differ between BSD and System V implementations. For example, to view detailed information about all running processes, in a BSD system, you would use ps with the following arguments:
ps -alxww
To display similar information in System V, use the arguments:
ps -elf
For more information about ps refer to the ps man page on your system. Also see In Unix, what do the output fields of the ps command mean?

pwd

This command reports the current directory path. Enter the command by itself:
pwd

rm

This command will remove (destroy) a file. You should enter this command with the  -i  option, so that you'll be asked to confirm each file deletion. To remove a file named junk, enter:
rm -i junk
Note: Using rm will remove a file permanently, so be sure you really want to delete a file before you use rm.
To remove a non-empty subdirectory, rm accepts the  -r  option. On most systems this will prompt you to confirm the removal of each file. This behavior can be prevented by adding the  -f  option. To remove an entire subdirectory named oldstuff and all of its contents, 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. Use this command with caution, as it is very easy to accidently delete important files. As a precaution, use the ls command to list the files within the subdirectory you wish to remove. To browse through a subdirectory named oldstuff, enter:
ls -R oldstuff | less

rmdir

This command will remove a subdirectory. To remove a subdirectory named oldstuff, enter:
rmdir oldstuff
Note: The directory you specify for removal must be empty. To clean it out, switch to the directory and use the ls and rm commands to inspect and delete files.

set

This command displays or changes various settings and options associated with your Unix session.
To see the status of all settings, enter the command without options:
set
If the output scrolls off your screen, combine set with less:
set | less
The syntax used for changing settings is different for the various kinds of Unix shells; see the man entries for set and the references listed at the end of this document for more information.

vi

This command starts the vi text editor. To edit a file named myfile in the current directory, enter:
vi myfile
The vi editor works fairly differently from other text editors. If you have not used it before, you should probably look at a tutorial, such as 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.
The very least you need to know to start using vi is that in order to enter text, you need to switch the program from command mode to insert mode by pressing  i . To navigate around the document with the cursor keys, you must switch back to command mode by pressing Esc. To execute any of the following commands, you must switch from command mode to ex mode by pressing  :  (the colon key): Enter  w  to save;  wq  to save and quit;  q!  to quit without saving.

w and who

The w and who commands are similar programs that list all users logged into the computer. If you use w, you also get a list of what they are doing. If you use who, you also get the IP numbers or computer names of the terminals they are using.

No comments:

Post a Comment