UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 6: File Management |
|
||||||||||||||||||||||||||||||||||||
|
System administrators are always working with large groups of files and directories. Moving them around and renaming them doesn't have to be a difficult task.
Flavors: AT&T, BSD
Syntax:
mv [file|dir] [file|dir]
The mv command is very commonly used on UNIX systems. Knowing when to use the mv command can make more difficult tasks easy. A normal everyday task of moving a file or a directory from one place to another is made simple for the UNIX administrator. If a newer version of an application or a file is to be used on a system, the old version can be moved out of the way.
# mv /usr/local/foo /usr/local/foo.OLD
In this case, foo can be a file or a directory. The mv command cannot handle masking or multiple files or directories. When a file or directory is moved across a filesystem, mv must copy the file to the new location first. After this is finished, the mv command removes the original file or directory. If a file or a directory is not moved to another filesystem local or remote to the system, the original name is simply changed to the new name and the inode points to the new name.
Warning - Keep in mind that any linking relationship where there is a symbolic link to a file or directory that has been moved will be lost. |
lrwxrwxrwx 1 root sys 22 Nov 23 18:09 /var/local/foo ->/usr/local/foo
If /var/local/foo was linked to /usr/local/foo, the link would be lost because /usr/local/foo was renamed to /usr/local/foo.OLD. Any attempt to change into a directory with a lost link displays an error.
# cd /var/local/foo foo - No such file or directory
Using the mv command to move a file or directory across NFS mount points can be done. However, better transfer rates can be achieved if the file is copied directly from the local host to a remote host (see section 6.2, "Copy Files Remotely"). Depending on the traffic load of the network, NFS has been known to drop or timeout during a file transfer and this slows the file transfer down.
Flavors: AT&T, BSD
Shells: sh
Although the mv command can be used only on individual files or a specific directory, you can still use the mv command to move or rename a series of files. Use a simple script that processes each file on an individual basis. To rename any type or all the files in a directory from one name to another, use the script renameall.
rocket 52% renameall * #!/bin/sh for i in $* do echo "$i -> $i.OLD" mv $i $i`.bak done
Line 1: Define the shell to be used by the script.
Line 2: Process through all the files in the directory.
Line 4: Display the file being renamed and the new name for the file.
Line 6: Move the file to the new filename.
Line 7: Take the next file in the directory until all the files in the directory have been processed.
When the program is executed all you see is the list of current files to be renamed and a pointer to the new filename. If you have a series of old tar files that you want to rename, the renameall script could change the names of all the tar files.
rocket 53% ls -1 1.tar 2 3.tar 4 5.tar rocket 54% renameall *.tar 1.tar -> 1.tar.OLD 3.tar -> 3.tar.OLD 5.tar -> 5.tar.OLD rocket 55% ls -1 1.tar.OLD 2 3.tar.OLD 4 5.tar.OLD
Flavors: AT&T, BSD
Shells: sh
In today's mix of operating systems, there comes a time when files reside on a UNIX system whose name is all in uppercase. Because UNIX is case-sensitive, you generally want to rename all the files to names with lowercase. The mv command makes this an easy process when a script is built around the command. You can mask any part of the files or use an asterisk ( *) to attempt to process all the files in a directory.
rocket 56% up2low #!/bin/sh for i in $* do file=`echo $i | tr "[A-Z]" "[a-z]"` echo "$i -> $file" mv $i $file done
Line 1: Define the shell to be used in the script.
Line 2: Start processing the files that are passed to the script.
Line 4: Convert any of the capital letters (A-Z) in the current file to lowercase (a-z) in the new lowercase filename from the variable called file.
Line 5: Display the current name and the new name that is all lowercase.
Line 6: Rename the current uppercase file to the new lowercase filename that is stored in the variable called file.
Line 7: Continue to process the next file until all the files have been converted to lowercase.
rocket 57%ls -1 Alias PASSWD RC.LOCAL SendMail.CF rocket 58% up2low * Alias -> alias PASSWD -> passwd RC.LOCAL -> rc.local SendMail.CF -> sendmail.cf rocket 59% ls -1 alias passwd rc.local sendmail.cf
Files can be downloaded or in a state that does not work well in an individual environment. Renaming or moving files is one of the best ways to get various files into the state that is most useful to us.
With more and more DOS and NT boxes popping up on the network, transferring files can put a file in the wrong case or can limit the number of characters that make up the filename. There is a need for moving or renaming these files into a naming convention that you as UNIX administrators can work with.
Man pages:
mv, tr
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 6: File Management |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.