[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 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. The AIX operating system also includes its own text editor, INed. 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/output redirection, as described in "Input and Output Redirection". The output of a command can be sent to a new file or appended to an existing file.

After creating and modifying files, you may 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 may also need to create new directories when working on different projects.

Also, you may need to delete certain files. Your directory can quickly get cluttered with files that contain old or useless information. Deleting files that are not needed frees up storage space on your system.

This section discusses:

Deleting Files (del and rm Commands)

When you no longer need a file, you can remove it with the del, rm, or delete commands. The del command asks for confirmation before deleting a file. This gives you a chance to change your mind. The rm and delete commands do not require user confirmation before removing the file. You can also use any of these commands to delete a group of files or to select certain files from a list for deletion.

del Command

Attention: The del command ignores file protection, allowing the owner of a file to delete a write-protected file. However, to delete a file, you must have write permission in the directory containing the file. Because pressing the Enter key by itself is the same as answering yes, be careful not to delete files accidentally.

The del command displays the list of specified files and asks you to confirm your request to delete the group of files. To answer yes (delete the files), press the Enter key, or enter a line beginning with y (or the locale's equivalent of a y ). Any other response specifies no (do not delete the files).

Note: The del command does not delete directories. See the rmdir command for information about deleting directories.

For example, to delete the file named chap1.bak , enter:

del chap1.bak

This displays the message:

del: Remove chap1.bak? Enter y or the Enter key for yes.

You can press the Enter key or y to answer yes. Pressing any other key cancels the deletion.

See the del command in the AIX Version 4.3 Commands Reference for the exact syntax.

rm Command

The rm or delete command removes the entries for the specified file or files from a directory. You do not need read or write permission for the file you want to remove. However, you must have write permission for the directory containing that file.

For example, to delete the file named myfile , enter:

rm myfile

To delete all the files in the mydir directory one by one, enter:

rm -i mydir/*

After each file name is displayed, enter y to delete the file, or press the Enter key to keep it.

Note: This is different from the del command.

See the rm command in the AIX Version 4.3 Commands Reference for the exact syntax.

Moving and Renaming Files (mv Command)

The mv command moves files and directories from one directory to another or renames a file or directory. 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 effect.

Moving Files with mv Command

For example, to move a file to another directory and give it a new name, enter:

mv intro manual/chap1

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

For example, to move a file to another directory, keeping the same name, enter:

mv chap3 manual

This moves chap3 to manual/chap3 . Note the previous Warning.

Renaming Files with mv Command

For example, to rename a file, enter:

mv appendix apndx.a

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

See the mv command in the AIX Version 4.3 Commands Reference for the exact syntax.

Copying Files (cp Command)

The cp or copy command creates 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 will overwrite 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. The preferred option for accomplishing this is the -R flag. Specifying -R causes the special files to be recreated under the new path name. Specifying the -r flag causes the cp command to attempt to copy the special files to regular files.

For example, to make a copy of a file in the current directory, enter:

cp prog.c prog.bak

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.

For example, to copy a file in your current directory into another directory, enter:

cp jones /home/nick/clients

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

For example, to copy all the files in a directory to a new directory, enter:

cp /home/janet/clients/* /home/nick/customers

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

For example, to copy a specific set of files to another directory, enter:

cp jones lewis smith /home/nick/clients

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

For example, to use pattern-matching characters to copy files, enter:

cp programs/*.c .

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

See the cp command in the AIX Version 4.3 Commands Reference for the exact syntax.

Finding Files (find Command)

The find command recursively searches 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.

For example, to list all files in the file system with the name .profile , enter:

find / -name .profile

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 avoid wasting time, it is best to limit the search by specifying the directories where you think the files might be.

For example, to list files having a specific permission code of 0600 in the current directory tree, enter:

find . -perm 0600

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. See the chmod command for an explanation of permission codes.

For example, to search several directories for files with certain permission codes, enter:

find manual clients proposals -perm -0600

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.

For example, to list all files in the current directory that have been changed during the current 24-hour period, enter:

find . -ctime 0 

For example, to search for regular files with multiple links, enter:

find . -type f -links +1

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. See the ln command for more information on multiple file links.

For example, to print the path names of all files in or below the current directory, except the directories named SCCS or files in the SCCS directories, enter:

find . -name SCCS -prune  

For example, to search for all files that are exactly 414 bytes long, enter:

find . -size 414c 

See the find command in the AIX Version 4.3 Commands Reference for the exact syntax.

Showing File Type (file Command)

The file command reads the files specified by the File or -f FileList parameter, performs a series of tests on each one, and attempts to classify them by types. 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 some sort of magic number, that is, any file containing a numeric or string constant that indicates type.

For example, to display the type of information the file named myfile contains, enter:

file myfile

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

For example, to display the type of each file named in filenames.lst , which contains a list of file names, enter:

file -f filenames.lst

This displays the type of each file named in the filenames.lst list. Each file name must appear alone on a line.

For example, to create the file filenames.lst , so that it contains all the file names in the current directory enter:

ls > filenames.lst

Then edit filenames as desired.

See the file command in the AIX Version 4.3 Commands Reference for the exact 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 that 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/output redirection. See "Input and Output Redirection Overview" for more details on input/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 - (minus) 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, enter:

pg myfile

See the pg command in the AIX Version 4.3 Commands Reference for the exact 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 done (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 data.

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

more myfile

Press the Spacebar to view the next screen.

See the more command in theAIX Version 4.3 Commands Reference for more information and the exact 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 , enter:

cat notes

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 , enter:

cat notes notes2 notes3

See the cat command in the AIX Version 4.3 Commands Reference for the exact syntax.

Finding Strings in Text Files (grep Command)

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

For example, 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 lower or upper case letter, enter:

grep "^[a-zA-Z]" pgm.s

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

To display all lines in a file named sort.c that do not match a pattern, enter:

grep -v bubble sort.c

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

To display lines in the output of the ls command that match the string staff , enter:

ls -l | grep staff

See the grep command in the AIX Version 4.3 Commands Reference for the exact syntax.

Sorting Text Files (sort Command)

The sort command alphabetizes or sequences 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 names are:

marta
denise
joyce
endrica
melanie

and the contents of the file states are:

texas
colorado
ohio

To display the sorted contents of the file named names , enter:

sort names

The system displays information similar to the following:

denise
endrica
joyce
marta
melanie

To display the sorted contents of the two files names and states , enter:

sort names states

The system displays information similar to the following:

colorado
denise
endrica
joyce
marta
melanie
ohio
texas

To replace the original contents of the file named names with its sorted contents, enter:

sort -o names names

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

See the sort command in the AIX Version 4.3 Commands Reference for the exact syntax.

Comparing Files (diff Command)

The diff command compares 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 to make them agree.

For example, to compare two files, enter:

diff chap1.bak chap1

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

For example, to compare two files while ignoring differences in the amount of white space, enter:

diff -w prog.c.bak prog.c

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

See the diff command in the AIX Version 4.3 Commands Reference for the exact 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 will be printed along with the counts.

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

wc chap1

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

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

wc -cw chap*

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 Version 4.3 Commands Reference for the exact 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, enter:

head -5 Test

See the head command in the AIX Version 4.3 Commands Reference for the exact 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, enter:

tail notes

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

tail -20 notes

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

tail -c +200 notes | pg

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

tail -f accounts

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

See the tail command in the AIX Version 4.3 Commands Reference for the exact syntax.

See the tail command in the AIX Version 4.3 Commands Reference for more information and the exact syntax.

Cutting Sections of Text Files (cut Command)

The cut command writes to standard output selected bytes, characters, or fields from each line of a file.

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

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

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 Version 4.3 Commands Reference for the exact 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, enter:

paste names places dates > npd

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:

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 always line up because the tab stops are set at every eighth column.

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

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

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:

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, enter:

ls | paste - - - -

Each - (minus) 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 Version 4.3 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, enter:

nl chap1

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

For example, to number all lines:

nl -ba chap1

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

See the nl command in the AIX Version 4.3 Commands Reference for the exact 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 called with one parameter, the columns of each line from the specified column to the last column are removed. If 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, enter:

colrm 6 < text.fil

If text.fil contains:

123456789

then the colrm command displays:

12345

See the colrm command in the AIX Version 4.3 Commands Reference for the exact syntax.

Related Information

Commands Overview

Processes Overview

Input and Output Redirection Overview

Shells Overview

File Systems Overview

Directory Overview

Files Overview

Linking Files and Directories

DOS Files

Printers, Print Jobs, and Queues

Backup Files and Storage Media

File and System Security


[ Previous | Next | Contents | Glossary | Home | Search ]