[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

File Handling Procedures

There are many ways to work with the files on your system. Usually you create a text file with a text editor. The common editors in the UNIX environment are vi and ed. Because several text editors are available, you can choose to edit with the editor you feel comfortable with.

You can also create files by using input and output redirection, as described in "Input and Output Redirection" . You can send the output of a command to a new file or append it to an existing file.

After creating and modifying files, you might have to copy or move files from one directory to another, rename files to distinguish different versions of a file, or give different names to the same file. You might also need to create directories when working on different projects.

Also, you might need to delete certain files. Your directory can quickly get cluttered with files that contain old or useless information. To release storage space on your system, ensure that you delete files that are no longer needed.

This section discusses the following:

Deleting Files (rm Command)

When you no longer need a file, you can remove it with the rm command. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command. However, you must have write permission for the directory containing that file.

The following are examples of how to use the rm command:

  1. To delete the file named myfile, type:
    rm myfile
    Press Enter.
  2. To delete all the files in the mydir directory, one by one, type:
    rm -i mydir/*
    Press Enter.

    After each file name displays, type y and press Enter to delete the file. Or to keep the file, just press Enter.

See the rm command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Moving and Renaming Files (mv Command)

To move files and directories from one directory to another or rename a file or directory, use the mv command. If you move a file or directory to a new directory without specifying a new name, it retains its original name.

Attention: The mv command can overwrite many existing files unless you specify the -i flag. The -i flag prompts you to confirm before it overwrites a file. The -f flag does not prompt you. If both the -f and -i flags are specified in combination, the last flag specified takes precedence.

Moving Files with mv Command

The following are examples of how to use the mv command:

  1. To move a file to another directory and give it a new name, type:
    mv intro manual/chap1
    Press Enter.

    This moves the introfile to the manual/chap1 directory. The name intro is removed from the current directory, and the same file appears as chap1 in the manual directory.

  2. To move a file to another directory, keeping the same name, type:
    mv chap3 manual
    Press Enter.

    This moves chap3 to manual/chap3.

Renaming Files with mv Command

You can use the mv command to change the name of a file without moving it to another directory.

To rename a file, type:

mv appendix apndx.a

Press Enter.

This renames the appendixfile to apndx.a. If a file named apndx.a already exists, its old contents are replaced with those of the appendix file.

See the mv command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Copying Files (cp Command)

You can use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters. If the file specified as the TargetFile exists, the copy writes over the original contents of the file without warning. If you are copying more than one SourceFile, the target must be a directory.

If a file with the same name exists at the new destination, the copied file overwrites the file at the new destination. Therefore, it is a good practice to assign a new name for the copy of the file to ensure that a file of the same name does not exist in the destination directory.

To place a copy of the SourceFile into a directory, specify a path to an existing directory for the TargetDirectory parameter. Files maintain their respective names when copied to a directory unless you specify a new file name at the end of the path. The cp command also copies entire directories into other directories if you specify the -r or -R flags.

You can also copy special-device files using the -R flag. Specifying -R causes the special files to be re-created under the new path name. Specifying the -r flag causes the cp command to attempt to copy the special files to regular files.

The following are examples of how to use the cp command:

  1. To make a copy of a file in the current directory, type:
    cp prog.c prog.bak
    Press Enter.

    This copies prog.c to prog.bak. If the prog.bak file does not already exist, then the cp command creates it. If it does exist, then the cp command replaces it with a copy of the prog.c file.

  2. To copy a file in your current directory into another directory, type:
    cp jones /home/nick/clients
    Press Enter.

    This copies the jones file to /home/nick/clients/jones.

  3. To copy all the files in a directory to a new directory, type:
    cp /home/janet/clients/* /home/nick/customers
    Press Enter.

    This copies only the files in the clients directory to the customers directory.

  4. To copy a specific set of files to another directory, type:
    cp jones lewis smith /home/nick/clients
    Press Enter.

    This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory.

  5. To use pattern-matching characters to copy files, type:
    cp programs/*.c .
    Press Enter.

    This copies the files in the programs directory that end with .c to the current directory, indicated by the single dot (.). You must type a space between the c and the final dot.

See the cp command in the AIX 5L Version 5.2 Commands Reference for the exact syntax.

Finding Files (find Command)

You can use the find command to recursively searche the directory tree for each specified Path, seeking files that match a Boolean expression written using the terms given in the following text. The output from the find command depends on the terms specified by the Expression parameter.

The following are examples of how to use the find command:

  1. To list all files in the file system with the name .profile, type:
    find / -name .profile
    Press Enter.

    This searches the entire file system and writes the complete path names of all files named .profile. The slash (/) tells the find command to search the /(root) directory and all of its subdirectories.

    To save time, limit the search by specifying the directories where you think the files might be.

  2. To list files having a specific permission code of 0600 in the current directory tree, type:
    find . -perm 0600
    Press Enter.

    This lists the names of the files that have only owner-read and owner-write permission. The dot (.) tells the find command to search the current directory and its subdirectories. For an explanation of permission codes, see the chmod command.

  3. To search several directories for files with certain permission codes, type:
    find manual clients proposals -perm -0600
    Press Enter.

    This lists the names of the files that have owner-read and owner-write permission and possibly other permissions. The manual, clients, and proposals directories and their subdirectories are searched. In the previous example, -perm 0600 selects only files with permission codes that match 0600 exactly. In this example, -perm -0600 selects files with permission codes that allow the accesses indicated by 0600 and other accesses above the 0600 level. This also matches the permission codes 0622 and 2744.

  4. To list all files in the current directory that have been changed during the current 24-hour period, type:
    find . -ctime 1 
    Press Enter.
  5. To search for regular files with multiple links, type:
    find . -type f -links +1
    Press Enter.

    This lists the names of the ordinary files (-type f) that have more than one link (-links +1).

    Note
    Every directory has at least two links: the entry in its parent directory and its own .(dot) entry. For more information on multiple file links, see the ln command.
  6. To search for all files that are exactly 414 bytes in length, type:
    find . -size 414c 
    Press Enter.

See the find command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying the File Type (file Command)

You can use the file command to read the files specified by the File or -f FileList parameter, perform a series of tests on each one. The command attempt to classify the files by type. The command then writes the file types to standard output.

If a file appears to be ASCII, the file command examines the first 512 bytes and determines its language. If a file does not appear to be ASCII, the file command further attempts to determine whether it is a binary data file or a text file that contains extended characters.

If the File parameter specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp.

The file command uses the /etc/magic file to identify files that have a magic number, that is, any file containing a numeric or string constant that indicates the type.

The following are examples of how to use the file command:

  1. To display the type of information the file named myfile contains, type:
    file myfile
    Press Enter.

    This displays the file type of myfile (such as directory, data, ASCII text, C-program source, and archive).

  2. To display the type of each file named in the filenames.lstfile, which contains a list of file names, type:
    file -f filenames.lst
    Press Enter.

    This displays the type of each file named in the filenames.lst file. Each file name must display on a separate line.

  3. To create the filenames.lst file, so that it contains all the file names in the current directory, type:
    ls > filenames.lst
    Press Enter.

    Edit the filenames file as desired.

See the file command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying File Contents (pg, more, page, and cat Commands)

The pg, more, and page commands allow you to view the contents of a file and control the speed at which your files are displayed. You can also use the cat command to display the contents of one or more files on your screen. Combining the cat command with the pg command allows you to read the contents of a file one full screen at a time.

You can also display the contents of files by using input and output redirection. See Input and Output Redirection for more details on input and output redirection.

pg Command

The pg command reads the file names from the File parameter and writes them to standard output one screen at a time. If you specify hyphen (-) as the File parameter, or run the pg command without options, the pg command reads standard input. Each screen is followed by a prompt. If you press the Enter key, another screen displays. Subcommands used with the pg command let you review something that has already passed.

For example, to look at the contents of the file myfile one page at a time, type:

pg myfile

Press Enter.

See the pg command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

more or page Command

The more or page command displays continuous text one screen at a time. It pauses after each screen and prints the filename and percent completed (for example, myfile (7%)) at the bottom of the screen. If you then press the Enter key, the more command displays an additional line. If you press the spacebar, the more command displays another screen of text.

Note: On some terminal models, the more command clears the screen, instead of scrolling, before displaying the next screen of text.

For example, to view a file named myfile, type:

more myfile

Press Enter.

Press the spacebar to view the next screen.

See the more command in theAIX 5L Version 5.2 Commands Reference for the complete syntax.

cat Command

The cat command reads each File parameter in sequence and writes it to standard output.

For example, to display the contents of the file notes, type:

cat notes

Press Enter. If the file is more than 24 lines long, some of it scrolls off the screen. To list a file one page at a time, use the pg command.

For example, to display the contents of the files notes, notes2, and notes3, type:

cat notes notes2 notes3

Press Enter.

See the cat command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Finding Text Strings Within Files (grep Command)

The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard output.

The following are examples of how to use the grep command:

  1. To search in a file named pgm.s for a pattern that contains some of the pattern-matching characters *, ^, ?, [, ], \(, \), \{, and \}, in this case, lines starting with any lowercase or uppercase letter, type:
    grep "^[a-zA-Z]" pgm.s
    Press Enter.

    This displays all lines in the pgm.s file that begin with a letter.

  2. To display all lines in a file named sort.c that do not match a pattern, type:
    grep -v bubble sort.c
    Press Enter.

    This displays all lines that do not contain the word bubble in the sort.c file.

  3. To display lines in the output of the ls command that match the string staff, type:
    ls -l | grep staff
    Press Enter.

See the grep command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Sorting Text Files (sort Command)

You can use the sort command to alphabetize or sequence lines in the files specified by the File parameters and writes the result to standard output. If the File parameter specifies more than one file, the sort command concatenates the files and alphabetizes them as one file.

Note
The sort command is case-sensitive and orders uppercase letters before lowercase (this is dependent on the locale).

In the following examples, the contents of the file named names are:

marta
denise
joyce
endrica
melanie

and the contents of the file named states are:

texas
colorado
ohio
  1. To display the sorted contents of the file named names, type:
    sort names
    Press Enter.

    The system displays information similar to the following:

    denise
    endrica
    joyce
    marta
    melanie
  2. To display the sorted contents of the names and states files, type:
    sort names states
    Press Enter.

    The system displays information similar to the following:

    colorado
    denise
    endrica
    joyce
    marta
    melanie
    ohio
    texas
  3. To replace the original contents of the file named names with its sorted contents, type:
    sort -o names names
    Press Enter.

    This replaces the contents of the names file with the same data but in sorted order.

See the sort command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Comparing Files (diff Command)

You can use the diff command to compare text files. It can compare single files or the contents of directories.

When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

The following are examples of how to use the diff command:

  1. To compare two files, type:
    diff chap1.bak chap1
    Press Enter.

    This displays the differences between the chap1.bak and chap1 files.

  2. To compare two files while ignoring differences in the amount of white space, type:
    diff -w prog.c.bak prog.c

    Press Enter. If the two files differ only in the number of spaces and tabs between words, the diff -w command considers the files to be the same.

See the diff command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Counting Words, Lines, and Bytes in Files (wc Command)

By default, the wc command counts the number of lines, words, and bytes in the files specified by the File parameter. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files. If flags are specified, the ordering of the flags determines the ordering of the output. A word is defined as a string of characters delimited by spaces, tabs, or newline characters.

When files are specified on the command line, their names are printed along with the counts.

For example, to display the line, word, and byte counts of the file named chap1, type:

wc chap1

Press Enter. This displays the number of lines, words, and bytes in the chap1 file.

For example, to display only byte and word counts, type:

wc -cw chap*

Press Enter. This displays the number of bytes and words in each file where the name starts with chap, and displays the totals.

See the wc command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying the First Lines of Files (head Command)

The head command writes to standard output the first few lines of each of the specified files or of the standard input. If no flag is specified with the head command, the first 10 lines are displayed by default.

For example, to display the first five lines of the Test file, type:

head -5 Test

Press Enter.

See the head command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying the Last Lines of Files (tail Command)

The tail command writes the file specified by the File parameter to standard output beginning at a specified point.

For example, to display the last 10 lines of the notes file, type:

tail notes

Press Enter.

For example, to specify the number of lines to start reading from the end of the notes file, type:

tail -20 notes

Press Enter.

For example, to display the notes file one page at a time, beginning with the 200th byte, type:

tail -c +200 notes | pg

Press Enter.

For example, to follow the growth of the file named accounts, type:

tail -f accounts

Press Enter. This displays the last 10 lines of the accounts file. The tail command continues to display lines as they are added to the accounts file. The display continues until you press the (Ctrl-C) key sequence to stop the display.

See the tail command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Cutting Sections of Text Files (cut Command)

To write selected bytes, characters, or fields from each line of a file to standard output, use the cut command.

For example, to display several fields of each line of a file, type:

cut -f1,5 -d: /etc/passwd

Press Enter. This displays the login name and full user name fields of the system password file. These are the first and fifth fields (-f1,5) separated by colons (-d:).

For example, if the /etc/passwd file looks like this:

su:*:0:0:User with special privileges:/:/usr/bin/sh
daemon:*:1:1::/etc:
bin:*:2:2::/usr/bin:
sys:*:3:3::/usr/src:
adm:*:4:4:System Administrator:/var/adm:/usr/bin/sh
pierre:*:200:200:Pierre Harper:/home/pierre:/usr/bin/sh
joan:*:202:200:Joan Brown:/home/joan:/usr/bin/sh

the cut command produces:

su:User with special privileges
daemon:
bin:
sys:
adm:System Administrator
pierre:Pierre Harper
joan:Joan Brown

See the cut command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Pasting Sections of Text Files (paste Command)

The paste command merges the lines of up to 12 files into one file.

For example, if you have a file named names that contains the following text:

rachel
jerry
mark
linda
scott

another file named places that contains the following text:

New York
Austin
Chicago
Boca Raton
Seattle

and another file named dates that contains the following text:

February 5
March 13
June 21
July 16
November 4

To paste the text of the files names, places, and dates together, type:

paste names places dates > npd

Press Enter. This creates a file named npd that contains the data from the names file in one column, the places file in another, and the dates file in a third. The npd file now contains the following:

rachel          New York        February 5
jerry           Austin          March 13
mark            Chicago         June 21
linda           Boca Raton      July 16
scott           Seattle         November 4

A tab character separates the name, place, and date on each line. These columns do not align, because the tab stops are set at every eighth column.

For example, to separate the columns with a character other than a tab, type:

paste -d"!@" names places dates > npd

Press Enter. This alternates ! and @ as the column separators. If the names, places, and dates files are the same as in example 1, then the npd file contains the following:

rachel!New York@February 5
jerry!Austin@March 13
mark!Chicago@June 21
linda!Boca Raton@July 16
scott!Seattle@November 4

For example, to list the current directory in four columns, type:

ls | paste - - - -

Press Enter. Each hyphen (-) tells the paste command to create a column containing data read from the standard input. The first line is put in the first column, the second line in the second column, and so on.

See the paste command in the AIX 5L Version 5.2 Commands Reference for the exact syntax.

Numbering Lines in Text Files (nl Command)

The nl command reads the specified file (standard input by default), numbers the lines in the input, and writes the numbered lines to standard output.

For example, to number only the nonblank lines, type:

nl chap1

Press Enter. This displays a numbered listing of chap1, numbering only the nonblank lines in the body sections.

For example, to number all lines, type:

nl -ba chap1

Press Enter. This numbers all the lines in the file named chap1, including blank lines.

See the nl command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Removing Columns in Text Files (colrm Command)

The colrm command removes specified columns from a file. Input is taken from standard input. Output is sent to standard output.

If the command is called with one parameter, the columns of each line from the specified column to the last column are removed. If the command is called with two parameters, the columns from the first specified column to the second specified column are removed.

Note: Column numbering starts with column 1.

For example, to remove columns from the text.fil file, type:

colrm 6 < text.fil

Press Enter.

If text.fil contains:

123456789

then the colrm command displays:

12345

See the colrm command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]