LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Cant delete file with invalid name (https://www.linuxquestions.org/questions/linux-general-1/cant-delete-file-with-invalid-name-474134/)

fiveseven 08-15-2006 02:00 PM

Cant delete file with invalid name
 
Hey,

I created a few files with ascii values from 0 onward - ie non-alphanumeric characters - (forgetting to convert the integers to their character values) and now im unable to delete them.

http://img109.imageshack.us/img109/9...eerrorskp6.png

Any help getting rid of these demented files is appreciated!

regards,
fiveseven

dannystaple 08-15-2006 02:08 PM

Quote:

Originally Posted by fiveseven
Hey,

I created a few files with ascii values from 0 onward - ie non-alphanumeric characters - (forgetting to convert the integers to their character values) and now im unable to delete them.

http://img109.imageshack.us/img109/9...eerrorskp6.png

Any help getting rid of these demented files is appreciated!

regards,
fiveseven

When you use *, the shell expands them, and then passes them on to the program, here being rm. You may want to try a different approach, as the shell may even be munging (that is - destroying) these character codes on the way.

I presume you programatically created the files (I could see few other ways of creating files with such names), so I suggest you also delete them in a similar manner, using the posix unlink (man 3 unlink) command, which is available in c, perl and probably plenty of other languages. I cannot guarantee that the posix layer wont bork at this point, but its worth a shot.

If you actually managed (kudos points here) to get a character 0 into a filename, that is quite an acheivement, and may be a complete pain in the backside being that it is used as the standard string terminator, which may be exactly how the posix libraries see it.

Your option then may be to try and list the directories contents in terms of their inodes, and try to unlink them by their inodes - this will not be easy.

Danny

fiveseven 08-15-2006 02:45 PM

I used standard c++ file streams to write the files as i'm very new to linux, and comming from a windows background i dont know my POSIX API yet.

To add to my problem, the invalid characters were from a timestamp so i have no idea as to what they would be, though i asume the mechanism used to write the filename would detect a null and termiate the string in the same way a reading command would. Is there a POSIX function that will enumerate all the files of a folder for me, so i can call unlink() manually as you instructed?

Big thanks for your help Danny,
fiveseven

soggycornflake 08-17-2006 11:51 AM

Yeah, looks like shell is bodging the names. Have you tried just rm -r the entire directory?

dannystaple 08-17-2006 05:01 PM

Extreme, but soggycornflakes method might work. Maybe what you should do then, is move out the files you do want to a temporary folder, delete the parent folder, and then recreate it with the bits you want.

Matir 08-17-2006 05:08 PM

Quote:

Originally Posted by dannystaple
Extreme, but soggycornflakes method might work. Maybe what you should do then, is move out the files you do want to a temporary folder, delete the parent folder, and then recreate it with the bits you want.

I agree. This seems likely to be the most reliable solution.

fiveseven 08-18-2006 04:23 AM

Unfortunatly trying to delete the directory gives me the same error

Quote:

five@nuclear-pussy:~/testarea/debug$ ls src
src/MusicAudit - src/MusicAudit -
five@nuclear-pussy:~/testarea/debug$ rm -r src
rm: cannot remove `src/MusicAudit - ': No such file or directory
five@nuclear-pussy:~/testarea/debug$
Ill eventually get around to poking around the net and see if i can find an API to enumerate files.

regards,
fiveseven

timmeke 08-18-2006 06:46 AM

Maybe you could try using echo -e to save the unreadable ASCII chars into a variable and then doing an rm using the variables in the filename.
It's probably a long shot though, as the shell may still interfere.

Example:
Code:

char=`echo -e -n \001`; #\001 indicates that the char with (ASCII) octal number 001 is to be printed.
rm "myfile${char}"; #or something like that.


soggycornflake 08-18-2006 09:24 AM

How about perl?

Code:

find src -print0 | perl -0wne 'unlink'

fiveseven 08-18-2006 05:07 PM

The perl script does find 2 of the invalid files and deletes them (the two that are visible from nautilis and 'ls'), but upon deleting the directory i still get an error saying that it cant find a file in the dir. There were 5 files in total, so i have to asume the other 3 are still there but arent being displayed.

// edit: i tried rm -r 'ing the directory again after nautilis wouldnt delete the dir and it worked. At least the directory isnt showing up anymore, which ill take as a sign that its inode has been free'd for good and i can forget about this now :P

Thanks for all your help guys, i really appreciate it.

regards,
fiveseven

iXneonXi 05-28-2007 10:03 PM

Scan for Invalid Filenames
 
I have files with invalid characters as well. However I can't pinpoint every single one of these files. Is there a way to scan for these files and either rename or delete them? They occasionally cause seemingly minor filesystem errors that the fsck fixes with ease.

masinick 12-05-2007 11:39 AM

Remove files using their inode number
 
Quote:

Originally Posted by fiveseven (Post 2380835)
Hey,

I created a few files with ascii values from 0 onward - ie non-alphanumeric characters - (forgetting to convert the integers to their character values) and now im unable to delete them.

http://img109.imageshack.us/img109/9...eerrorskp6.png

Any help getting rid of these demented files is appreciated!

regards,
fiveseven

First find out file inode number by using either of the following commands:

Code:

stat {file-name}
or
Code:

ls -il {file-name}
then use find to remove the file
Code:

find . -inum [inode-number] -exec rm -i {} \;
When prompted for confirmation, press Y to confirm removal of the file.

pixellany 12-05-2007 12:13 PM

quick scan--apologies if I missed this being mentioned.

Did you try putting the files in a directory and then rm -rf dirname ?

trickykid 12-05-2007 02:12 PM

Easy, all of the files you wanted Deleted start with Music. I see nothing else that starts with Music, so just do a:

rm -r Music*

Instead of:

rm -r MusicAudit*

Or find the Inode of each and do a rm that way.


All times are GMT -5. The time now is 11:20 PM.