Bash command runs in terminal but not in script file

后端 未结 1 938
梦毁少年i
梦毁少年i 2021-01-23 21:16

I\'m trying to delete some files and folders from a directory. The command (from here):

rm -rf !(file_i_don\'t_want_to_remove|other|other_one)

相关标签:
1条回答
  • 2021-01-23 21:39

    You need the extglob extension enabled for this syntax to work:

    #!/bin/bash
    shopt -s extglob
    cd /path/to/dir # See the comment below the answer
    rm -rv !(one|two)
    

    PS: Do not use rm -f in a script.

    PS: If /path/to/dir is coming from a variable, make sure that is is not empty before using it with cd:

    if [ -z "${path_to_delete}" ] ; then
        "path_to_delete is empty! Aborting"
        exit 1
    fi
    cd "${path_to_delete}"
    rm -rv !(one|two)
    
    0 讨论(0)
提交回复
热议问题