Using cp, mv, cat and rm commands in AIX

You can use the regular commands which we generally use in any UNIX platforms to access files or directories in AIX aswell. Use cp command to copy the files from one directory to another directory and similarly use mv command to move the files from one directory to another directory. Also use cat, pg and more commands to read a file and use rm command to delete a file or directory.

Commands used to manage AIX Files and Directories are

cp Command

Syntax

To Copy a File to another File

cp [ -f ] [ -h ] [ -i ] [ -p ][ -- ] SourceFile TargetFile

To Copy a File to a Directory

cp [ -f ] [ -h ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory

To Copy a Directory to a Directory

cp [ -f ] [ -h ] [ -i ] [ -p ] [ -- ] { -r | -R } SourceDirectory TargetDirectory

Description

The cp command copies the source file specified by the SourceFile parameter to the destination file specified by the TargetFile parameter. If the target file exists, cp overwrites the contents, but the mode, owner, and group associated with it are not changed.

When using the cp command, if the file specified as the target file already exists, then the copy operation will write over the original contents of the file without warning. To avoid this use cp -i (interactive copy). If you are copying more than one file in one operation, then the specified target must be a directory. If the target is a directory, the copies will have the same file names as the original. cp -R can be used to recursively copy all files, subdirectories, and the files in those subdirectories to a new directory. For example, cp -R /home/team01/mydir /home/team01/newdir

Examples 
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, the cp command creates it. If it does exist, the cp command replaces  it with a copy of the prog.c file. 

mv Command

Syntax

To Move Files to a Directory Maintaining Original File Names

mv [ -i | -f ] SourceFile TargetFile

To Move and Rename a File or Directory

mv [ -i | -f ] SourceFile ... TargetDirectory

Description

    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. If both the -f and -i flags are specified in combination, the last     flag specified takes precedence.

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, it retains the base file name. When you move a file, all links to other files remain intact, except when you move it to a different file system. When you move a directory into an existing directory, the directory and its contents are added under the existing directory.

When you use the mv command to rename a file or directory, the TargetDirectory parameter can specify either a new file name or a new directory path name.

Examples
  1. To rename a file, enter:

     mv appendix apndx.a

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

cat Command

Syntax

cat [ - q ] [ -r ] [ - s ] [ - S ] [ - u ] [ - n [ - b ] ] [ - v [ - e ] [ - t ] ] [ - | File ... ]

Description

The cat command reads each File parameter in sequence and writes it to standard output. If you do not specify a file name, the cat command reads from standard input. You can also specify a file name of - (dash) for standard input.

    Attention: Do not redirect output to one of the input files using the     redirection symbol, > (caret). If you do this, you lose the original data in     the input file because the shell truncates the file before the cat command     can read it. See "Redirecting Input and Output in the Korn Shell" in AIX 5L     Version 5.1 System User's Guide: Operating System and Devices for more     information.

Examples

 Attention: Do not redirect output to one of the input files using the     redirection symbol, > (caret).

  1. To display a file at the workstation, enter:

     cat notes

     This command displays the data in the notes file. If the file is more than  one less than the number of available display lines, some of the file      scrolls off the screen. To list a file one page at a time, use the pg
     command.

pg and more Commands

The pg command reads the file names specified and displays the files one page at a time. Each screen is followed by a prompt. Press Enter to display the next page down. The more command works in much the same way as the pg command - it displays continuous text one screen at a time. It pauses after each screen and prints the word More at the bottom of the screen. If you press Enter, it displays an additional line. If you press the space bar, it displays another screen of text. When more is reading from a file, it displays a % with the More prompt. This provides the fraction of the file (in characters) that the more command has read.

Apart from moving down one page at a time, both the pg and the more commands support subcommands to perform other functions, for example to move up by one page with more use the b character. To move up one page with the pg command use the - (dash) character. Don't forget that with the pg command you must hit the Enter key after a subcommand, whereas with the more command the action takes place as soon as the subcommand is entered.

wc Command

Syntax

wc [ -c | -m ] [ -l ] [ -w ] [ File ... ]

wc -k [ -c ] [ -l ] [ -w ] [ File ... ]

Description

By default, the wc command counts the number of lines, words, and bytes in the files specified by the File parameter. The command writes the number of newline characters, words, and bytes to the standard output and keeps a total count for all named files.

Examples

  1. To display the line, word, and byte counts of a file, enter:

     wc chap1

     The wc command displays the number of lines, words, and bytes in the chap1   file.

ln Command

Syntax

To Link a File to a File

ln [ -f ] [ -s ] SourceFile [ TargetFile ]

To Link a File or Files to a Directory

ln [ -f ] [ -s ] SourceFile ... TargetDirectory

Description

The ln command links the file designated in the SourceFile parameter to the file designated by the TargetFile parameter or to the same file name in another directory specified by the TargetDirectory parameter. By default, the ln command creates hard links. To use the ln command to create symbolic links, designate the -s flag.

A symbolic link is an indirect pointer to a file; its directory entry contains the name of the file to which it is linked. Symbolic links may span file systems and may refer to directories. If you are linking a file to a new name, you can list only one file. If you are linking to a directory, you can list more than one file.

The TargetFile parameter is optional. If you do not designate a target file, the ln command creates a new file in your current directory. The new file inherits the name of the file designated in the SourceFile parameter.

Examples
  1. To create another link (alias) to a file, enter:

     ln  -f chap1  intro

     This links chap1 to the new name, intro. If intro does not already exist,  the file name is created. If intro does exist, the file is replaced by a  link to chap1. Then both the chap1 and intro file names will refer to the  same file. Any changes made to one also appear in the other. If one file  name is deleted with the rm command, the file is not completely deleted  since it remains under the other name.

rm Command

Syntax

rm [ -f ] [ -r ] [ -R ] [ -i ] [ -e ] File ...

Description

The rm command removes the entries for the specified File parameter from a directory. If an entry is the last link to a file, the file is then deleted. If you do not have write permission for a file and the standard input is a
terminal, you are prompted with the file name and ask to confirm that you want to delete the file. If you type a y (for yes), the file is deleted, type any other character and the file is not deleted. 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 the file.

If the file is a symbolic link, the link is removed, but the file or directory that the symbolic link refers to remains. You do not need write permission to delete a symbolic link, if you have write permission in the directory.

The -r option permits recursive removal of directories and their contents if a directory is specified. Be careful when using this option as it does not require the directory to be empty in order for this option to work.

Examples
1. To delete a file, enter:

     rm myfile

     If there is another link to this file, then the file remains under that  name, but the name myfile is removed. If myfile is the only link, the file itself is deleted.

0 Comment to "Using cp, mv, cat and rm commands in AIX"

Post a Comment