Linux – Removing file which doesn’t have any permissions and attributes

fileslinux

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?

Best Answer

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")'
    
Related Question