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.

mahatmanich
  • 2,954
  • 3
  • 22
  • 23

5 Answers5

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).

user
  • 4,335
  • 4
  • 34
  • 71
  • why -i, what is the problem of list all files and try to remove the file by name? – Baiyan Huang Sep 10 '13 at 13:23
  • Izprgmr it does work with escaping some chars – mahatmanich Sep 10 '13 at 13:25
  • http://www.cyberciti.biz/tips/delete-remove-files-with-inode-number.html – mahatmanich Sep 10 '13 at 13:27
  • 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 '13 at 15:04
5

Easy way:

ls -i Test_*

rm -i [inode number]
jscott
  • 24,484
  • 8
  • 79
  • 100
Paul Rudnitskiy
  • 413
  • 2
  • 5
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.

rickhg12hs
  • 394
  • 2
  • 9
  • because 'rm anything*' will get you in deeper trouble faster than you might think! I want a pinpoint operation not a wildcard one! – mahatmanich Sep 13 '13 at 12:08
  • I don't understand your "grand scale" requirement. How can this requirement be achieved without some "globbing"? – rickhg12hs Sep 13 '13 at 16:17
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.
Znik
  • 348
  • 1
  • 3
  • 12
1

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

pratik
  • 11
  • 1