How to delete multiple files at once in Bash on Linux?

前端 未结 7 745
有刺的猬
有刺的猬 2021-01-29 19:09

I have this list of files on a Linux server:

abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
ab         


        
7条回答
  •  感动是毒
    2021-01-29 19:32

    If you want to delete all files whose names match a particular form, a wildcard (glob pattern) is the most straightforward solution. Some examples:

    $ rm -f abc.log.*             # Remove them all
    $ rm -f abc.log.2012*         # Remove all logs from 2012
    $ rm -f abc.log.2012-0[123]*  # Remove all files from the first quarter of 2012
    

    Regular expressions are more powerful than wildcards; you can feed the output of grep to rm -f. For example, if some of the file names start with "abc.log" and some with "ABC.log", grep lets you do a case-insensitive match:

    $ rm -f $(ls | grep -i '^abc\.log\.')
    

    This will cause problems if any of the file names contain funny characters, including spaces. Be careful.

    When I do this, I run the ls | grep ... command first and check that it produces the output I want -- especially if I'm using rm -f:

    $ ls | grep -i '^abc\.log\.'
    (check that the list is correct)
    $ rm -f $(!!)
    

    where !! expands to the previous command. Or I can type up-arrow or Ctrl-P and edit the previous line to add the rm -f command.

    This assumes you're using the bash shell. Some other shells, particularly csh and tcsh and some older sh-derived shells, may not support the $(...) syntax. You can use the equivalent backtick syntax:

    $ rm -f `ls | grep -i '^abc\.log\.'`
    

    The $(...) syntax is easier to read, and if you're really ambitious it can be nested.

    Finally, if the subset of files you want to delete can't be easily expressed with a regular expression, a trick I often use is to list the files to a temporary text file, then edit it:

    $ ls > list
    $ vi list   # Use your favorite text editor
    

    I can then edit the list file manually, leaving only the files I want to remove, and then:

    $ rm -f $(

    or

    $ rm -f `cat list`
    

    (Again, this assumes none of the file names contain funny characters, particularly spaces.)

    Or, when editing the list file, I can add rm -f to the beginning of each line and then:

    $ . ./list
    

    or

    $ source ./list
    

    Editing the file is also an opportunity to add quotes where necessary, for example changing rm -f foo bar to rm -f 'foo bar' .

提交回复
热议问题