rm fails to delete files by wildcard from a script, but works from a shell prompt

前端 未结 6 613
广开言路
广开言路 2021-01-30 19:51

I\'ve run into a really silly problem with a Linux shell script. I want to delete all files with the extension \".bz2\" in a directory. In the script I call

rm \         


        
6条回答
  •  旧巷少年郎
    2021-01-30 20:34

    TL;DR

    Quote only the variable, not the whole expected path with the wildcard

    rm "$archivedir"/*.bz2
    

    Explanation

    • In Unix, programs generally do not interpret wildcards themselves. The shell interprets unquoted wildcards, and replaces each wildcard argument with a list of matching file names. if $archivedir might contain spaces, then rm $archivedir/*.bz2 might not do what you

    • You can disable this process by quoting the wildcard character, using double or single quotes, or a backslash before it. However, that's not what you want here - you do want the wildcard expanded to the list of files that it matches.

    • Be careful about writing rm $archivedir/*.bz2 (without quotes). The word splitting (i.e., breaking the command line up into arguments) happens after $archivedir is substituted. So if $archivedir contains spaces, then you'll get extra arguments that you weren't intending. Say archivedir is /var/archives/monthly/April to June. Then you'll get the equivalent of writing rm /var/archives/monthly/April to June/*.bz2, which tries to delete the files "/var/archives/monthly/April", "to", and all files matching "June/*.bz2", which isn't what you want.

    The correct solution is to write:

    rm "$archivedir"/*.bz2

提交回复
热议问题