Unix: How to delete files listed in a file

后端 未结 13 1650
遥遥无期
遥遥无期 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条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 07:16

    This will allow file names to have spaces (reproducible example).

    # Select files of interest, here, only text files for ex.
    find -type f -exec file {} \; > findresult.txt
    grep ": ASCII text$" findresult.txt > textfiles.txt
    # leave only the path to the file removing suffix and prefix
    sed -i -e 's/:.*$//' textfiles.txt
    sed -i -e 's/\.\///' textfiles.txt
    
    #write a script that deletes the files in textfiles.txt
    IFS_backup=$IFS
    IFS=$(echo "\n\b")
    for f in $(cat textfiles.txt); 
    do 
    rm "$f"; 
    done
    IFS=$IFS_backup
    
    # save script as "some.sh" and run: sh some.sh
    

提交回复
热议问题