3

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?

10
  • Are you able to move via mv command? If so, do you take the ownership so that you can delete it? Commented Mar 6, 2014 at 14:57
  • @LaurentC. I tried that but its not working either. Edit: It isn't recognising it as a file or a directory Commented Mar 6, 2014 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. Commented Mar 6, 2014 at 15:39
  • 4
    You've got a directory entry that points to an unallocated inode, you'll want to run a fsck. Commented Mar 6, 2014 at 15:53
  • 2
    @StephaneChazelas Thanks. I ran fsck first. Then the perl script suggested by terdon. It worked. Commented Mar 6, 2014 at 16:05

4 Answers 4

4

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")'
    
10
  • 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. Commented Mar 6, 2014 at 15:37
  • @ganesh737 doesn't deleting the directory itself work? Also try rm -r .* Commented Mar 6, 2014 at 15:37
  • I tried that as "rm -R directory" but it isn't working. Commented Mar 6, 2014 at 15:42
  • @ganesh737 really? What error message? And rm .*? How about mv the directory somewhere else? Does that work? Commented Mar 6, 2014 at 15:43
  • 2
    the perl script worked after running fsck... Thank you so much!!! Commented Mar 6, 2014 at 16:01
1

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.

2
  • I tried it but i'm still getting the same output on console from rm command Commented Mar 6, 2014 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. Commented Mar 6, 2014 at 15:30
0

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
1
  • Here, the ???? indicate that the lstat fails, so ls -li won't make any difference. Commented Mar 6, 2014 at 16:02
0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.