Managing your files as units
Last revision August 3, 2004
|Table of Contents:|
These programs (commands) do not affect the contents of a file, but rather deal with the file as a single unit.
The cp command makes a new identical copy of an existing file in a new location, leaving the original file untouched. Basic syntax:
cp existingfile newfile
This command will copy existingfile to newfile. You can also specify a directory as the location for the copy, for example,
cp existingfile newdir
in which case a copy of existingfile will be made in this newdir directory, that is, the copy will be named newdir/existingfile. Using this syntax, you can actually specify several files to be copied at once to a new directory. You can use wildcard characters to match a set of files to be moved. List the files (or wildcard matching pattern) to be copied as arguments to cp, with the directory where the copies will be placed as the last argument.
Useful options for cp:
This option allows you to copy an entire directory and all its files and
sub-directories, etc. The "destination" file must be a directory. The
directory being copied is made as a subdirectory of the destination directory.
For example, the command:
cp -r /scr/farrell/tapestuff /play/sysop/farrell
copies the entire directory /scr/farrell/tapestuff and makes the copy as
This option sets the creation date on the copy to match the original exactly.
Otherwise, creation date is set to current date. Permission settings on the
copy are set the same as the original in either case.
|-i||This option tells cp to first prompt for confirmation before overwriting an existing file.|
The mv command just renames (moves) a file. It changes the filename or the directory in which the filename is located. Basic syntax:
mv existingfile newfilename
This command will rename existingfile to be newfilename.
Just like the cp command, mv will accept a directory name as the last argument, which tells it to simply move (rename) all the files listed in the other arguments into the directory listed as the last argument.
Logically, mv is like a cp command, but without leaving the original file behind. Actually, it just removes the old link between inode and name and creates a new one, unless the new name would be in a directory on a different physical disk, in which case it also has to move the contents of the file (the inode and data).
Because mv basically just works with links, it can also be used to change the name of a directory or its location in the tree structure.
mv also obeys the -i option to prompt you for confirmation before overwriting any existing file.
WARNING: If there is already a file with the same name as the one you want to create, cp or mv silently removes it and puts its new copy there. Be careful! Or use the -i option to either command. This option causes cp or mv to prompt for permission before over-writing an existing file.
The rm command removes a file. Basic syntax:
rm file1 [
This command will remove one or more files specified as arguments.
Actually, it just removes the filename that is linked to the file. If there are other filenames linked to the same inode, they are not affected and the file contents are still there and accessible under the other name(s). If this filename is the only link to the inode, then the inode is erased and the area of disk occupied by the file contents is considered free space by the system.
WARNING: For ordinary files with only a single link, the rm command permanently removes the file. It cannot be recovered, except from a backup medium. There is no such thing as undelete on Unix.
rm cannot remove a directory file. Use rmdir to do that, but first remove or move all files listed in the directory.
Useful options for rm:
This option will recursively erase an entire directory tree, including all files
in the directory, plus all sub-directories and their files, etc.
This option is very
|-i||This "interactive" option prompts you to verify each file that is to be removed.|
For novices who are worried about accidentally removing the wrong file or over-writing an existing file, we have created system-wide aliases on pangea for the cp, mv and rm commands that automatically include the -i option. That is, if you use the following aliases instead of the base commands, you will always be prompted for permission before any existing file would be removed or over-written.
-- aliases for
move -- alias for mv -i
copy -- alias for cp -i
command creates a new sub-directory.
mkdir subdir1 [
It will make one or more empty subdirectories with names specified as arguments.
You must have write permission in the parent directory to make a sub-directory. You can use either absolute or relative pathnames to specify the directory name.
mkdir may not initialize the permissions for the directory as you would like. Initial permissions (who can read and write in the directory) are controlled by a shell environment variable that is normally set to allow read access by everyone but write access by the owner only. You may want to use the ls -l command to check the permissions that are actually set after creating a new directory.
To remove a directory, use the program rmdir, listing the directories to remove as arguments. These directories must be empty first. Use the rm program to remove all files listed in the directory before removing the directory with rmdir
If you have nested directories, and want to remove all of them, it can be tedious to first go to the deepest level, use rm to remove all the files in that directory and then rmdir to remove the directory, then go up to the next higher level and repeat, etc. In this case, you can remove an entire set of nested directories and all their files at once with the rm -r command. But this is very dangerous See more about the rm program above.