Copying and Moving
Course Objectives Covered
Executing Commands at the Command Line (3036)
Common Command Line Tasks (3036)
Copying and Moving Files and Directories (3036)
Creating Directories (3036)
Deleting Files and Directories (3036)
System administration would be so much easier if nothing ever moved or changed. Unfortunately, it is very rare for anything to stay static any more, and changes take place at a nonstop pace. Linux offers two powerful utilities for copying and moving files: cp and mv, respectively, and a third utilityddthat combines features of both.
cp works with both files and directories and can move multiple entities at a time using wildcards. After the name of the utility, you must specify the source, followed by the target. The simplest use of all can be illustrated as follows:
$ ls -l fileone onefile ls: onefile: No such file or directory -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone $ $ cp fileone onefile $ ls -l fileone onefile -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 edulaney users 85 Aug 30 16:18 onefile $
Notice that the original entry (source) remains unchanged, but now there is a second entry (target) as well. On the second entry, the contents are identical, but the date and time are those of the present (when cp was executed), not those of the source. Notice as well that the owner and group of the new file became that of the user executing the command. This is the same action that would take place if you were creating the target file completely from scratch.
To be able to copy a filecreate a new entity equal in content to anotheryou need only read permission to the source.
The -p option can be used to force as many of the old variables to remain the same. It preserves what it can in terms of attributes:
$ ls -l fileone nextfile ls: nextfile: No such file or directory -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone $ $ cp -p fileone nextfile $ ls -l fileone nextfile -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 edulaney users 85 Aug 22 10:26 nextfile $
Notice that the date and time associated with the source were kept, but the owner and group still must change.
When you do a copy operation, the utility first checks to see if the source file exists. If it does, whatever file is specified as the target is created (with a size of zero), and the contents of the source are copied. The emphasis here is on the fact that the target is always createdregardless of whether it existed before or not. Here's an illustration:
$ ls -l fileone filetwo -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ $ cp fileone filetwo $ ls -l fileone filetwo -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 edulaney users 85 Aug 30 16:18 filetwo $
The original contents of filetwo have been lost, except for any backup tape versions, as filetwo is created to be a copy of fileone. There is a -i (as in inquire) option, which can be used to always ask if you really want to erase the contents of the target file if it already exists:
$ ls -l fileone filetwo -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ $ cp -i fileone filetwo cp: overwrite 'filetwo'?
At the prompt, you can enter "y" to perform the operation, or anything else to stop it.
You can copy a number of files from one directory to another so long as the last item on the command line is a valid directory path into which the files will be copied:
$ ls -l /usr/home/sdulaney -rw-r--r-- 1 root root 85 Aug 22 10:26 exit $ $ cd /usr/home/examples $ ls -l s* q* -rw-r--r-- 1 root root 585 Aug 23 12:16 questions -rw-r--r-- 1 root root 1985 Aug 24 15:17 samples -rw-r--r-- 1 root root 8501 Aug 25 18:30 snapshot01.gif $ cp s* q* /usr/home/sdulaney $ cd ../sdulaney $ ls -l -rw-r--r-- 1 root root 85 Aug 22 10:26 exit -rw-r--r-- 1 root root 585 Aug 31 22:50 questions -rw-r--r-- 1 root root 1985 Aug 31 22:50 samples -rw-r--r-- 1 root root 8501 Aug 31 22:50 snapshot01.gif $
To move an entire directory from one location to another, use the -r or -R option to recursively move the directory as well as any subdirectories and files beneath it. Other options that can be used include -f to force a copy without any prompting (the opposite, so to speak, of -i); -u to copy only when the source file is more recent (updated) than the target; and -v for verbose mode (show all operations, rather than perform them silently).
Lastly, the -P option will reproduce the entire path to a file in another locationcreating directories and subdirectories as needed to do so.
The move utility (mv) can be used for several operations. At the risk of being overly simplistic, this includes the ability to
Rename a file
Rename a directory
Move a file from one directory to another
Move a subdirectory from one directory to another
Move an entity to another partition or media
The simplest operation is to rename a file in its current directory:
$ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ $ mv fileone filethree $ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 filethree -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $
The dates, permissions, and everything else associated with fileone stay with filethree. This is because when a file is moved within the same directory (or even on the same partition), all that changes is the information about the nameno physical operation takes place; only the descriptor is changed. The move has become a simple rename operation.
As simplistic as it may sound, always remember that when you copy a file, you leave the original intact and create something that did not exist beforethus a new set of attributes is created for the new entity. When you move a file, however, the default action is a renameyou are changing only the name of the original entity and not creating anything new.
One way to put it in perspective is that if you copy a file that is 9MB in size, it will take longer than if you copy a file that is 9 bytes in size. With move being used as a rename, it will take the same amount of time to do the operation regardless of the size of the file.
As with the copy operation, if you attempt to move a file to a name that already exists, the contents of the target are lost. Here's an illustration:
$ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ mv fileone filetwo $ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 filetwo $
The -i option (as in inquiry or interactive) can be used to prompt before overwriting, and the opposite of it is -f (for force), which is the default operation. The -u option will only do the move if the source file is newer, and -v turns on verbose mode. The -b option makes a backup of the target file, if it exists, with a tilde as the last characteressentially performing a pseudo-copy operation:
$ ls -l help* -rw-r--r-- 1 root root 85 Aug 22 10:26 helpfile $ mv -b helpfile helpfiletwo $ ls -l help* -rw-r--r-- 1 root root 85 Aug 22 10:26 helpfiletwo $ $ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ mv -b fileone filetwo $ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 filetwo -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo~ $
In the first instance, there was not an existing file with the target name present, so the -b option was ignored. In the second instance, a file by the name of the target was in existence, so the original target file is renamed with a tilde (~) as the last character.
The -b option can also be used with cp to perform the same action during a copy as it does with mv.
The device-to-device (dd) utility is used to copy a file from one device to another. It goes beyond that in functionality, however, for it can convert a file during the copy process from one format to another. It can convert from EBCDIC to ASCII (and reverse), change uppercase to lowercase (and reverse as well), and work with bytes, blocks, or keywords.
The most common use for dd is copying files to and from removable media, and you must use arguments that can include
bsblock file size
Removing Files and Directories
When files are no longer needed, they can be removed from the system with the rm (remove) command. Be careful using this command, for Linux offers no undelete command or function like those found in other operating systems.
$ ls -l file* -rw-r--r-- 1 root root 85 Aug 22 10:26 fileone -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $ rm fileone $ ls -l file* -rw-r--r-- 1 root root 16432 Aug 28 13:43 filetwo $
When used with the -i option, a prompt appears before each file to be deleted. Pressing Y deletes the file, and pressing any other character skips the file.
$ ls -l t* -rw-r--r-- 1 root root 85 Aug 22 10:26 today -rw-r--r-- 1 root root 16432 Aug 28 13:43 tuesday $ rm -i t* rm: remove 'today'?
The -f option forces deletion, and -v puts the utility in verbose mode. The -r or -R option recursively deletes directories (including subdirectories and files beneath). In order to delete a file, you have to have write permissions within the directory where it resides.
Write permission is only required on the directory from which you are deleting the filenot on the file itself.
A safer utility, rmdir, can be used to delete directories that have nothing beneath them. It will only delete empty directories and cannot be used for directories that have files or subdirectories beneath them. The only option that can be used with rmdir is -p to remove parent directories (if empty).
$ ls -R kdulaney kdulaney: docs kdulaney/docs: attempt $ $ rmdir kdulaney rmdir: kdulaney: Directory not empty $
Because there is a file (attempt) within kdulaney/docs, and a subdirectory (docs) beneath kdulaney, the directory kdulaney cannot be deleted with the rmdir utility. There are three possible ways to accomplish the task:
Use the rm -r command.
Delete attempt with rm, and then delete docs with rmdir, andfinallydelete kdulaney with rmdir.
Delete attempt with rm, and then delete kdulaney/docs with rmdir -p.
Because rmdir can only delete empty directories, it is naturally a safer utility to use than rm for cleaning a system.
Now that you've learned how to copy, move, and delete directories, the only order of business left is to make a directory, which you can do by using the mkdir command. Used without options, it creates a child directory (subdirectory) in the current directory. There are two options that work with it as well:
-mTo specify permissions other than the default for the new directory (covered in a later chapter)
-pTo create a parent and child in one command
Here are some examples of the utility:
$ pwd /usr/home $ mkdir edulaney $
This created the subdirectory edulaney beneath /usr/home(/usr/home/edulaney).
$ pwd /usr/home $ mkdir kdulaney/docs mkdir: cannot make directory 'kdulaney/docs': No such file or directory $ $ mkdir -p kdulaney/docs $ cd kdulaney $ cd docs $
In the first attempt, the utility fails as you cannot create multiple directories by default. If you use the -p option, however, the multiple directories are created.