Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file on my external hard disk named ._Icon^M.I ended up with this after using my hard disk on old Mac platform machine. I want to delete this file but unable to.

For the command 'ls -al' it shows as

dr-xr-xr-x 1 root root 8192 Mar 6 19:53 ..
-????????? ? ?    ?       ?           ? ._Icon?

Seeing this I tried to add ownership (using chown) and modify the permissions(using chmod) but the commands are not recognising ._Icon as file or a directory.

I tried deleting the file using the command -

find . -name '._*' -exec rm '{}' ';'

rm is not able to remove as it is not interpreting it as a file or a directory The console after running the above command is

rm: cannot remove './._Icon\r': No such file or directory

How do I delete such a file?

share|improve this question
    
Are you able to move via mv command? If so, do you take the ownership so that you can delete it? –  Laurent C. Mar 6 at 14:57
    
@LaurentC. I tried that but its not working either. Edit: It isn't recognising it as a file or a directory –  ganesh737 Mar 6 at 14:58
1  
@fedorqui the file is not ._Icom? but ._Icom\r and it is a filesystem problem, not a regular file as you can see from the ls -l output. You won't be able to replicate this unless you use your disk on a mac. –  terdon Mar 6 at 15:39
4  
You've got a directory entry that points to an unallocated inode, you'll want to run a fsck. –  Stéphane Chazelas Mar 6 at 15:53
1  
@StephaneChazelas Thanks. I ran fsck first. Then the perl script suggested by terdon. It worked. –  ganesh737 Mar 6 at 16:05

4 Answers 4

up vote 3 down vote accepted

A few things you can try:

  • Try to complete the file using tab autocompletion. For example

    rm .[TAB]
    
  • Move all other files from this directory somewhere else and then delete the directory. That should get rid of the file.

  • Move all other files and just run (assuming GNU find) this:

    find . -type f -delete
    
  • Delete all files in the directory that start with a dot:

    rm -r .*
    
  • Get the file's inode and delete it using that. ls -i should show you the inode. Alternatively, run

    find . -printf "%i %f\n"
    

    Once you have the inode, try deleting using find again:

    find . -inum XXX -delete
    
  • Try this Perl script. Change dirname for the name of the directory containing the file and run this from the parent directory. So, if your file is ~/foo/file run this in ~/ and change dirname to foo.

    perl  -e 'use File::Path qw(remove_tree); remove_tree("dirname")'
    
share|improve this answer
    
I tried but its not working. For the first method you mentioned, after autocompletion it is displayed as "rm ._Icon^M" but it isn't working. Also I have moved all files from the directory and tried using find with -delete option but its not working for me. –  ganesh737 Mar 6 at 15:37
    
@ganesh737 doesn't deleting the directory itself work? Also try rm -r .* –  terdon Mar 6 at 15:37
    
I tried that as "rm -R directory" but it isn't working. –  ganesh737 Mar 6 at 15:42
    
@ganesh737 really? What error message? And rm .*? How about mv the directory somewhere else? Does that work? –  terdon Mar 6 at 15:43
2  
the perl script worked after running fsck... Thank you so much!!! –  ganesh737 Mar 6 at 16:01

It may be that you cannot remove it because of the CR (\r) character. You can try with the following:

echo -ne "._Icon\r" | xargs rm

And see if it removes it.

share|improve this answer
    
I tried it but i'm still getting the same output on console from rm command –  ganesh737 Mar 6 at 15:28
    
You don't want the -0 that makes xarg expect null separated strings and that's not what you are passing it. –  terdon Mar 6 at 15:30

When I had similar problems I was able to delete files by inode

ls -li

first column is inode

To delete it

find . -inum num_inode | xargs rm
share|improve this answer
    
Here, the ???? indicate that the lstat fails, so ls -li won't make any difference. –  Stéphane Chazelas Mar 6 at 16:02

Such symptoms are normally due to not being able to give the correct file name to rm(1). One solution is doing something like, in this case:

rm -i .*

(i.e., ask for removal of all files called .*, but ask for each one).

To find out what the exact name is, ls -ba (give C escapes for "strange" characters, list all files) might help. There are other (GNU) ls flags that might be of use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.