Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The situation isn't really as simple as the title may imply.

I have a .desktop file that execs with bash -c to set an environment variable then load the program. Now I need to add another command before it goes to loading the program.

Sometimes the program creates file called SOFT_REPAIR in ~/path to/*/, and I need to delete it before running the program. * represents whatever version the program maybe, and as /path to/ implies, there's spaces in the folder names within which the SOFT_REPAIR resides.

So here's the thing:

  • Do I need to do /path\ to/?
  • Will rm 'path to/*/SOFT_REPAIR/' work?
  • Can I just use the rm command whether SOFT_REPAIR exists or not, or do I need to use the if statement?
share|improve this question

2 Answers 2

up vote 3 down vote accepted

To solve your original problem:

find ~/'path to'/ -name SOFT_REPAIR -exec rm -f "{}" \;

or, if you have GNU find:

find ~/'path to'/ -name SOFT_REPAIR -delete

To answer your questions:

  • You need to backslash-escape the spaces in the file names if you don't wrap them in quotes.
  • No, because the quotes prevent shell glob expansion. This should work, though:

    rm 'path to'/*/SOFT_REPAIR/
    

    assuming that SOFT_REPAIR resides only two levels below path to/

  • rm on a non-existent file will throw an error. rm -f will fail silently (at least in the GNU implementation of rm).

share|improve this answer
    
Deleting with the find command won't throw a tantrum if SOFT_REPAIR is non-existent, right? –  Oxwivi Feb 16 at 13:07
    
Also, will find recursively look into the subdirectories or do I need to specify exactly where the file is? If that is so, it defeats the purpose, because neither I nor the .desktop file will know the latest version of the program and subsequently the name of the folder with SOFT_REPAIR in it. –  Oxwivi Feb 16 at 13:15
    
@Oxwivi find will not complain. It just applies the actions to any files it finds. Also, it operates recursively. –  Joseph R. Feb 16 at 13:17
    
Trying it out answered my question. Many thanks! –  Oxwivi Feb 16 at 13:23

Yes, this works. Simply use rm -f path\ to/*/SOFT_REPAIR.

But why don't you simply try yourself?

michas@lenny:~/t$ mkdir -p path\ to/{foo,bar}
michas@lenny:~/t$ touch path\ to/{foo,bar}/SOFT_REPAIR
michas@lenny:~/t$ tree
.
`-- path\ to
    |-- bar
    |   `-- SOFT_REPAIR
    `-- foo
        `-- SOFT_REPAIR

3 directories, 2 files
michas@lenny:~/t$ rm path\ to/*/SOFT_REPAIR
michas@lenny:~/t$ tree
.
`-- path\ to
    |-- bar
    `-- foo

3 directories, 0 files
michas@lenny:~/t$ rm path\ to/*/SOFT_REPAIR
rm: cannot remove 'path to/*/SOFT_REPAIR': No such file or directory
michas@lenny:~/t$ rm -f path\ to/*/SOFT_REPAIR
michas@lenny:~/t$ tree
.
`-- path\ to
    |-- bar
    `-- foo

3 directories, 0 files
share|improve this answer
    
Nicely summarized, thank you very much! –  Oxwivi Feb 16 at 13:10
    
No need to thank, just accept the answer. :) –  michas Feb 16 at 13:13
    
Unfortunately the alternative solution suggested by @Joseph suits the situation I described better. Sorry! –  Oxwivi Feb 17 at 15:39

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.