Open Computing ``Hands-On'': ``Answers to Unix'' Column: October 94

A question about mv, octal values in text, and lots o' feedback

By Ray Swartz

Question: Why is it that copying a file with cp creates a file with a new owner but moving a file with mv leaves the new file with the original owner?

Val Abbott
Carmel, Ind.

Answer: It is clear that copying a file must make a new file on the system, which is the copy requested. Whenever a new file is created on the Unix system, its owner is assigned to be the user who is running the program that created the file. In addition, the new file is given its own set of permissions that may or may not match the original.

The reason why moving a file leaves the moved file with the same owner is because you haven't created a new file. You've simply given an existing file a new name.

Let me explain this event a bit further. On the Unix system, a file is represented by two parts: an i-node and a directory link.

The i-node represents all the information about the file including who owns it, its permissions, and the data blocks holding the file's text, among other details. The two things not identified by a file's i-node are its name and the directory that holds that name. This information is specified in a directory link.

I-nodes are numbered. To make them easier for humans to identify, files are given names by associating a name with an i-node number. A file name is linked to a directory by placing the i-node-file name pair in a directory file. This directory-file-name-i-node combination is called a directory link.

The mv command simply discards the existing directory link and creates a new one, leaving the file's i-node and all its information untouched. Because the i-node is unchanged, so is the file's owner and permissions.

It is worth noting that while the mv command leaves the newly named file unchanged, it does change the directories containing the links, which means that the directory holding the original link is modified by the removal of that directory link as is the destination directory by the addition of the new directory link. Thus, both directories will have their modification and change times updated.

Question: After getting and converting a file from another system, I noticed some funny characters interspersed in the text. I discovered that the character was represented by the code 196. How can I get rid of these characters without having to go through the text by hand to find them?

Tomas Jensen
Mobile, Ala.

Answer: The easiest way to delete a single character from text is to use the tr command. Without options, tr converts one character to another. It also will delete characters, if you use the -d option followed by the character to delete.

Unfortunately, tr is a bit odd, even for a Unix command. It only reads characters from its standard input and only writes to its standard output. Thus, unless used in a pipeline, you must redirect tr's standard input.

In addition, tr doesn't understand decimal values. You must send it ASCII characters (decimal value below 128) or octal values. To delete all the code-196 characters in the file, you must first convert 196 to its octal equivalent. Listing 1 shows the dec2oct script, which takes decimal numbers as input and prints out their octal values. Entering 196 into the dec2oct command told me that 196 is 304 in octal.

The Unix convention is that octal values are identified by putting a backslash in front of the value. Octal 304 is represented by \304. The tr command line is:

tr -d '\304' < input_file > output_file

Note:The single quotes around the \304 are required.

Feedback: In your July 1994 column, you published an 18-line script by Daniel E. Singer that uses cpio to show file permissions in octal. There are two problems with the script. First, it is overkill and slow because cpio must shuffle about data that is thrown away anyway. Second, the script does not work as intended under System V Release 4.2, which is true to the manual. The cpio -itv command indeed looks like an ls -l command, showing file permissions in familiar symbolic (``rwx'') format.

I've written a simple 18-line C program (Listing 2) that is the same length as Mr. Singer's shell script but is immensely faster and works on any Unix system. Indeed, it even works under DOS to the limited extent that DOS supports file permissions.

Andy Levinson
Studio City, Calif.

Feedback: This little Perl program (Listing 3) will list only the directories in current directories.

Stephen P. Potter
Gainesville, Fla.
Last Modified: