Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Strange Files Deletion and Renaming in Shell

There may come a time that you will discover that you have somehow created a file with a strange name that cannot be removed through conventional means. This section contains some unconventional approaches that may aid in removing such files.

Files that begin with a dash can be removed by typing

    rm ./-filename
A couple other ways that may work are
    rm -- -filename
and
    rm - -filename
Now let's suppose that we an even nastier filename. For example there can be a file with no filename. The solution is to type
    rm -i *
This executes the rm command in interactive mode. Just answer "yes" to the query to remove the nameless file and "no" to all the other queries about the rest of the files.

Another method that can be used  is to obtain the inode number of the strange file using ls: 


    ls -i
and then type
    find . -inum number -ok rm '{}' \;
where number is the inode number.

The -ok flag causes a confirmation prompt to be displayed. This is for your safety in case you mistypes inode number.

If you want to rename the file with the strange name, following modification to the find command works:

    find . -inum number -ok mv '{}' new_filename \;

Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 10, 2009