2.4 Copying and Moving Files

   

Many times you will be copying or moving files from one place to another. These two operations are similar except that the old file is deleted in the move operation.

Copying Files

The files are copied with the cp command. The source and destination file names are supplied to the cp command as arguments. The first argument is the source file name, and second argument is the destination file name .

 $  cp myfile anotherfile  $ 

This command copies myfile from the current directory to anotherfile in the current directory. It is possible to copy files from any directory to any other directory using the path names of the files. For example, if you want to copy profile from the /etc directory to the current directory with the name myprofile , the command line will be as follows .

 $  cp /etc/profile myprofile  $ 

As another example, if you want to copy the file in the above example with the same name in the current directory, you just use " . " in place of the destination name. Note that the " . " character is a relative path that refers to the current directory. For example, to copy /etc/profile with the name profile in the current directory, the following command can be used.

 $  cp /etc/profile .  $ 

Two or more files can be copied simultaneously using the cp command. In this case, the destination must be a directory name. The following command copies two files, file1 and file2 , from the current directory to the /tmp directory.

 $  cp file1 file2 /tmp  $ 

Moving and Renaming Files

The mv command is used for renaming files and moving files from one place to another in the directory structure. Like the cp command, it takes source and destination file names as arguments. If both source and destination names are specified without any path (absolute or relative), the file is renamed . On the other hand, if any or both of the file names contain a path name, the file is moved from the source location to the destination location.

RENAME A FILE
 $  mv myfile newfile  $ 

Make sure that the operation was successful by using the ll command.

MOVE A FILE
 $  mv myfile /tmp/myfile  $ 

Two or more files can be moved simultaneously using the mv command. The destination must be a directory name. The following command moves two files, file1 and file2 , to directory /tmp .

 $  mv file1 file2 /tmp  $ 

Note

You must be careful with the mv command, as it will overwrite any existing file if the destination file name matches any source file. And it will do it without any warning. To make sure that existing files are not overwritten, always use the mv command as mv -i . In this case, if the destination file already exists, the mv command will ask you to confirm the move or rename operation.



   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net