Remove all files except some from a directory

前端 未结 19 1162
生来不讨喜
生来不讨喜 2020-11-30 16:36

When using sudo rm -r, how can I delete all files, with the exception of the following:

textfile.txt
backup.tar.gz
script.php
database.sql
info.         


        
19条回答
  •  无人及你
    2020-11-30 16:52

    You can use GLOBIGNORE environment variable in Bash.

    Suppose you want to delete all files except php and sql, then you can do the following -

    export GLOBIGNORE=*.php:*.sql
    rm *
    export GLOBIGNORE=
    

    Setting GLOBIGNORE like this ignores php and sql from wildcards used like "ls *" or "rm *". So, using "rm *" after setting the variable will delete only txt and tar.gz file.

提交回复
热议问题