Skip to content

            Lost ?  |  Need an account:
 
Home >> Knowledge Base >> Operating Systems >> Linux >> Working with non-printable filenames
Working with non-printable filenames PDF Print E-mail
(3 votes, average 4.67 out of 5)
Written by Tom Hirt   
Tuesday, 24 February 2009 09:59

Have you ever found yourself working with a file-name that contained non-printable characters?  If you have, then you already know how difficult they can be to work with.  These files present a problem to us because they contain characters or symbols that are not typically found on our keyboard. Usually these files have control characters or other non UTF-8 compliant characters that we have no way of representing.

Non-Printable Filename Examples

So how can we work with such a file?

Fortunately unbeknownst to most *nix administrators, the modern day file-system deals with files and directories not by name, but by inode.  When a file system is created (formated), data structures are created that contain information about each of the files and directories. Each file and directory is associated with an inode that is then identified by an inode number.  Inodes store information on files, such as user and group ownership, access mode (read, write, execute permissions) and type of file.

Most of the commands you probably already use have a command-line switch to either reveal or reference a given inode.  For example, you'll see from the below screen shot that we have issued the command 'ls -ali'.  You'll notice the additional switch of 'i' has produced a directory listing containing a new column that includes the inode number of each file and directory.

Inode Exposed

Now that we know the inode number of the file of interest, we can now use this to help us work with the non-printable file-name.

Lets begin by renaming the first file-name to something that is a little more "friendly" to work with:

File renamed with find

As you can see, we used find's command line switch of -inum to reference the non-printable filename's inode number renaming it to 'PRINTABLE_FILENAME'.  Notice that in doing so, the inode number of the file never changed!  As we said above, the inode is how the file is actually referenced by the filesystem.  Because the human readable file-name we interact with is just another attribute contained in the meta-data of the inode database, changing the file-name has no actual affect on the underlying data referenced by the inode number.  Thus changing the filename does not produce a new inode number.

Now that the filename has been renamed, you can now intereact with the file as you would have done so with any other file using commands like less, cp, and rm to view the contents, copy and remove the file.

But, lets take this one step further, and see how we might remove the file in one quick and easy step:

File renamed with find

Notice we used the initial find as a precautionary step just to ensure we were matching the file of interest.  After we verified the inode number of the file we wanted to remove, we used find optional command line switch of '-exec rm {} \;' to remove the file!  Now how easy was that?  Just remember our golden rule, backup backup backup!  Once this file is removed, its gone forever.


Add this page to your favorite website
AddThis Social Bookmark Button
Comments
Add New Search
Daniel  - Thanks! |17/08/2009 06:47:50
That helped me out of a bind!
thirt |21/08/2009 12:12:37
LOL. Trust me... we have all been there before :)

Best,
Tom
Dieter  - ls cannot list the ugly filename ... |22/09/2009 03:15:51
ls cannot list the ugly filename ... :(

# ls -lsai > xx 2> err
# cat xx
total 16
7024654 8 drwxr-xr-x 3 wgr root 8192 2009-09-22 11:06 .
7024653 8 drwxr-xr-x 3 wgr root 8192 2009-09-21 21:02 ..
2100236 0 -rw-r--r-- 1 root root 292 2009-09-22 11:06 err
2100229 0 -rw-r--r-- 1 root root 0 2009-09-22 11:06 xx
# cat err
/bin/ls: æS§ä•.Ï„*ÂÔÄrQbs–®¡’:‘D^…êT¼Ð`ŸlËÐ≤─Î⎼ý9Ö␋␋FT‡Ý§πÈ1J┌Ì’¸ùÍ8
„‰‘,̹8ðº⎻“ÙâT¢Mîý\‚GPé®Çýâ6⎻ÔTZÖIF±ÓÔ?ŠK˜&ƒ­>¾úýŽ≠=≥½‚␉Ý≥OËÁ␉Úö;÷┤FÔ²+èš,*¯≥ÁÔÆá’[G·±!¿Ç┼0¾·«ÖK³Ç
◆1Hú-! ≠ÎÏå
Ë»␉9E'›‘X£⎽├ åIÞݘ夐Qµ®: N⎺ ⎽┤␌␤ °␋┌␊ ⎺⎼ ␍␋⎼␊␌├⎺⎼≤

# ls | xargs -0 rm
rm: cannot remove `æS\003§ä\225.Ï\024\204*\001\024Â\017Ô\030ÄrQ\235bs\226®¡\222:\221D^\205êT¼Ð\022`\237l\023Ë\016ÐyqÎrý9Ö\026iiFT\207ݧ{È1JlÌ\020\222¸ùÍ\0038\v\0216áeÛä.ó}B\a\206I\203øzÎuË\032*\223\205Íi»Ç\003\r\204\211\221,̹8ðºp\223ÙâT¢Mîý\\\217\202GPé®Çýâ6pÔTZª\bÖIF±ÓÔ?\212K\201\230&\203­>¾úý\216\027|=z\002½\202bÝ\032zOË\006ÁbÚö;÷uFÔ²+è\232,*¯zÁÔÆá\222[G·±!¿Çn0¾C\b·«\aÖK\022³\n`\0261Hú-! |^\bÎÏå\vË»b9E\001\235\'\233\221X}st\tå\001I\027ÞÝ\230å¤\220Qµ®\nerr\nx2\nxx\n': No such file or directory

# echo * > x2 2> err
# vi x2
æS^C§ä~U.Ï^T~D*^A^TÂ^OÔ^XÄrQ~]bs~V®¡~R:~QD^~EêT¼Ð^R`~_l^SË^NÐyqÎrý9Ö^ViiFT~Gݧ{È1JlÌ^P~R¸ùÍ^C8^K^Q6áeÛä.ó}B^G~FI~CøzÎuË^Z*~S~EÍi»Ç^C^M~D~I~Q,̹8ðºp~SÙâT¢Mîý\~O~BGPé®Çýâ6pÔTZª^HÖIF±ÓÔ?~JK~A~X&~C­>¾úý~N^W|=z^B½~BbÝ^ZzOË^FÁbÚö;÷uFÔ²+è~Z,*¯zÁÔÆá~R[G·±!¿Çn0¾C^H·«^GÖK^R³
`^V1Hú-! |^^HÎÏå^KË»b9E^A~]'~[~QX}st å^AI^WÞÝ~Xå¤~PQµ® err x2 xx

Also mc does not work

Any ideas ?
-Dieter
thirt |28/09/2009 10:08:15
Dieter,

I'm not sure the requirements for xargs in your example above, but the reason the command appears to fail is because the filename your passing to the rm command has special characters in it (you probably already realize this.) The easiest way, in my opinion, is to remove the file by the inode number (as in the examples above.) Else, you are going to be fighting the syntax of xargs with delimiters etc., when the inode number doesn't care about the filename.

Send me the output of 'ls -ali' run from the directory containing the file you are trying to remove and tell me which file you want gone and I'll give you the correct syntax.

-Tom
Sam  - Files with no inode number |14/06/2011 01:19:12
Cud anyone say how to delete a file that doe nt ve inode number

ls -i
14 paris
bye

i need to delet bye that doe nt ve inode number

reply asap
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:D:):(:0:shock::confused:8):lol::x:P:oops::cry::evil::twisted::roll::wink::!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 02 March 2009 17:46
 

Forum Activity

Author:
Author: kenny22
Jan.24.12
Author: AaronRiley
Jan.15.12

Online Stats

Guests Online: 69
Members Online: 0