Unix Tip: How-to rename an oddball file

February 6, 2008, 11:02 AM —  ITworld.com — 

Every now and then I come across a file that just doesn't display properly in a file listing. In fact, it can be a little tricky to determine the name of a file that contains odd characters. You might think a file's name is "myfile.txt" only to find out that it's really "myfile.txt " (note the extra blanks). Even so, I was a little surprised to see a file listing that looked like this:

-rw-r--r--   1 root     other        180 Nov 30 10:2q

In this file listing, part of the timestamp has been obliterated by some unprintable characters in the file's name.

My usual strategy for dealing with oddly named files is to list the file's inode number. After that, I can use the inode number in a file command to remove the file or give it a more well behaved file name. That strategy works well with files that have blanks in their names. In this case, the listing of the file with its inode number looked a little odd too.

	> ls -i *
        15098q

To determine whether 15098 was the complete inode number, I could run a find command in the directory like this:

	> find . -inum 15098 -print

If the find command worked, I could then rename the file with a similar command:

	> find . -inum 15098 -exec mv {} newname \;

The find command will work but will complain that "./newname and newname are identical".

On the other hand, inode numbers can have six, seven, eight or more digits, so this would only be a guess. I wasn't sure that I wanted to make what might turn out to be hundreds of additional guesses.

Another option is to leave worrying about the actual name of the file to the system. If there aren't too many files in the current directory, you can loop through them and rename the troublemaker when you come to it. This can be useful if you want to look at the file and not simply remove it from the system.

for file in `ls`
do
     echo $file
     echo -n "rename?> "
     read ans
     if [ $ans == "y" ]; then
         mv $file newname
     fi
done

If you're really curious about the file's name, unprintable characters and all, you can list your files and pipe the file list to an od command. Since I knew the oddly named file had at least a "q" in it, I narrowed my targets and got this output:

> ls *q* | od -bc
0000000 177 177 161 012
         177 177   q  \n

Use "xc" if you prefer to see the output in hex.

From this output, I could tell that the file's name was backspace backspace q, the 177 being the octal code for the backspace character. How this file came to have such an odd name is still something of a mystery, but at least it now displays normally in my file listings.

ITworld.com

I like it!
Comments

This is very helpful

This is very helpful especially the 177 octal value. We were abel to fix the issue in our production system through this.
| reply

This is fine as long as the

This is fine as long as the filenames aren't dangerous. What if the filename was foo`rm -rf /`bar.jpg ? When you put it on the command line of mv as an environment variable it may be interpreted and will remove files beginning at root (of course, it will only remove files in directories to which you have write access, but your system will be damaged none the less).

So, handle "odd" filenames with care. For example, rename them from perl using the rename function, which doesn't submit the filename for command expansion the way a shell would.
| reply
Free stuff

Win an Amazon Kindle!
This month's giveaway gadget - Amazon's Kindle - will keep you entertained on the long trip home to visit family and friends over the holidays. Enter the drawing now!

Applied Security Visualization
By Raffael Marty
Published by Addison-Wesley Professional
Learn more!

 

IT Manager's Handbook
By Bill Holtsnider and Brian D. Jaffe
Published by Morgan Kaufmann
Learn more!

 

Windows Vista Resource Kit
By Mitch Tulloch, Tony Northrup, and Jerry Honeycutt
Published by Microsoft Press
Learn more!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources