Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers 5

up vote 7 down vote accepted

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 example:

~$ ls -i myweirdfile
183435818 myweirdfile
~$ find . -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 . -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 first to verify which file will be deleted. Something like the following:

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

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

share|improve this answer
    
why -i, what is the problem of list all files and try to remove the file by name? –  lzprgmr Sep 10 '13 at 13:23
    
Izprgmr it does work with escaping some chars –  mahatmanich Sep 10 '13 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. –  Michael Kjörling Sep 10 '13 at 15:04
add comment

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.

share|improve this answer
    
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
add comment

Easy way:

ls -i Test_*

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

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.
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.