ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT

Unix Tip: How-to rename an oddball file

ITworld 02/06/2008

Sandra Henry-Stocker, ITworld.com

Send in your Unix questions today! | See additional Unix tips and tricks

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.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
FREE SECURITY AUDIT RESOURCES
Take a Risk Assessment, get White Papers on the Latest Threats, listen to Malware Expert Webcasts.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Used and Refurbished Nortel Routers
Purchase Your Routers From Network Liquidators. Savings of Up to 90% with a Lifetime Warranty!
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Computers and Peripherals  Computing systems  Servers  Enterprise  Unix enterprise servers
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.