4

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?

2 Answers 2

3

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

4
  • Deleting with the find command won't throw a tantrum if SOFT_REPAIR is non-existent, right?
    – Oxwivi
    Feb 16, 2014 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, 2014 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, 2014 at 13:17
  • Trying it out answered my question. Many thanks!
    – Oxwivi
    Feb 16, 2014 at 13:23
2

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
1
  • Unfortunately the alternative solution suggested by @Joseph suits the situation I described better. Sorry!
    – Oxwivi
    Feb 17, 2014 at 15:39

You must log in to answer this question.

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