Unix: How to delete files listed in a file

后端 未结 13 1593
遥遥无期
遥遥无期 2020-12-04 06:31

I have a long text file with list of file masks I want to delete

Example:

/tmp/aaa.jpg
/var/www1/*
/var/www/qwerty.php

I need delet

13条回答
  •  -上瘾入骨i
    2020-12-04 06:57

    Assuming that the list of files is in the file 1.txt, then do:

    xargs rm -r <1.txt
    

    The -r option causes recursion into any directories named in 1.txt.

    If any files are read-only, use the -f option to force the deletion:

    xargs rm -rf <1.txt
    

    Be cautious with input to any tool that does programmatic deletions. Make certain that the files named in the input file are really to be deleted. Be especially careful about seemingly simple typos. For example, if you enter a space between a file and its suffix, it will appear to be two separate file names:

    file .txt
    

    is actually two separate files: file and .txt.

    This may not seem so dangerous, but if the typo is something like this:

    myoldfiles *
    

    Then instead of deleting all files that begin with myoldfiles, you'll end up deleting myoldfiles and all non-dot-files and directories in the current directory. Probably not what you wanted.

提交回复
热议问题