5

I want to remove a file from a server via bash rm command.

This is a sample file Test_ Mürz.jgp.

How would one go about removeing files with such chars issues in the filename on a grand scale ... especially when you don't know the position of the chars.

5 Answers 5

16

For single files, or small sets of files, if wildcard globbing doesn't allow you the precision you feel you need, you can combine ls -i (or stat, if available) and find -inum.

For safety, when using find's -inum, always make sure to also use -xdev to constrain the search to a single file system. Not doing so may have unexpected results.

For example:

~$ ls -i myweirdfile
183435818 myweirdfile
~$ find . -xdev -inum 183435818 -exec rm -i '{}' ';'
rm: remove regular file `./myweirdfile'? y
~$

Alternatively, in a single invocation (this might depend on GNU coreutils stat, which should be a fairly safe assumption on Linux, and uses sh-style process substitution):

~$ find . -xdev -inum $(stat -c '%i' 'myweirdfile') -exec rm -i '{}' ';'
rm: remove regular file `./myweirdfile'? y
~$

You can also use find's -delete action rather than -exec'ing rm. For really weird file names, this may be safer. Use -print or -ls first to verify which file will be deleted. Something like the following:

~$ ls -i myweirdfile
183435818 myweirdfile
~$ find . -xdev -inum 183435818 -print
./myweirdfile
~$ find . -xdev -inum 183435818 -delete
~$ find . -xdev -inum 183435818 -print
~$

Do keep in mind that hardlinks use the same inode number for multiple names, so you want to make sure there's no stray additional name anywhere that gets deleted as well (unless you want to do that, obviously).

4
  • why -i, what is the problem of list all files and try to remove the file by name? Sep 10, 2013 at 13:23
  • Izprgmr it does work with escaping some chars Sep 10, 2013 at 13:25
  • 2
    @lzprgmr Because with GNU coreutils rm, rm -i causes rm to prompt once for each file removal, ensuring you don't accidentally delete the wrong file (it's very easy to mistype the inode number!). Since the question is tagged linux, I think assuming GNU coreutils is reasonable.
    – user
    Sep 10, 2013 at 15:04
5

Easy way:

ls -i Test_*

rm -i [inode number]
5
  • Oh this is cool didn't know about it thanks! Sep 10, 2013 at 12:28
  • 7
    What rm program is this? With rm from GNU coreutils, -i means "prompt before every removal".
    – Lekensteyn
    Sep 10, 2013 at 12:37
  • yeah tried it and it does not work! Sep 10, 2013 at 13:14
  • It depends about distro (checked on debian). Sep 11, 2013 at 10:37
  • 1
    Debian certainly does not do that in its rm! Sep 13, 2013 at 12:06
2

What "grand scale" goals do you have?

Why isn't this sufficient?

$ rm Test_*

One way of diving into a directory tree of files:

$ find /your/base/dir -maxdepth 3 -type f -name 'Test_*' -exec rm {} \;

You can tune the find command to zero-in on your files.

2
  • because 'rm anything*' will get you in deeper trouble faster than you might think! I want a pinpoint operation not a wildcard one! Sep 13, 2013 at 12:08
  • I don't understand your "grand scale" requirement. How can this requirement be achieved without some "globbing"?
    – rickhg12hs
    Sep 13, 2013 at 16:17
1

we can also use this command rm ./"Test_ Mürz.jgp"to remove such files

1

there are three methods:

  1. use inode against filename. first do ls command and check inode file, described above
  2. for dummies, use Midnight Commander :)
  3. you must escape special characters. space (_ for visuality) _ you replace as _ , and other characters.
  4. you can use bash expanding. At start write beginning: rm Test_ and press TAB key. They expand escaped characters immediately for unique names, or display possibilities for you for non unique. Then you'll see how shoud you write special characters in command line.
  5. you can escape unicode by escaping: somename\263\261rest_of_name . you can use decimal character codes for escaping.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .