Skip to main content
added 155 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

If you have a list of hundreds of filenames and you want to delete them, there is no reason to write separate rm commands for each of them. Assuming your file names saved in a file called list.txt, one per line, and that they do not contain newline characters, you could just do

while read -r file; do rm "$file"; done < list.txt 

###Explanation

  • The while read variable; do something; done < file construct will read each line from a file and save it as variable (in the example above, the variable's name is file). The -r is needed to allow for file names containing things like \r or \t. With the -r they will be treated literally while without it they will be expanded to a carriage return or a tab respectively.
  • rm "$file" : this will remove each file in the list as it is read.

Or, you can use bash's mapfile builtin which lets you save the lines of a file in an array:

mapfile -t filelist < list.txt && rm "${filelist[@]}"

If you have a list of hundreds of filenames and you want to delete them, there is no reason to write separate rm commands for each of them. Assuming your file names saved in a file called list.txt, one per line, and that they do not contain newline characters, you could just do

while read -r file; do rm "$file"; done < list.txt 

###Explanation

  • The while read variable; do something; done < file construct will read each line from a file and save it as variable (in the example above, the variable's name is file). The -r is needed to allow for file names containing things like \r or \t. With the -r they will be treated literally while without it they will be expanded to a carriage return or a tab respectively.
  • rm "$file" : this will remove each file in the list as it is read.

If you have a list of hundreds of filenames and you want to delete them, there is no reason to write separate rm commands for each of them. Assuming your file names saved in a file called list.txt, one per line, and that they do not contain newline characters, you could just do

while read -r file; do rm "$file"; done < list.txt 

###Explanation

  • The while read variable; do something; done < file construct will read each line from a file and save it as variable (in the example above, the variable's name is file). The -r is needed to allow for file names containing things like \r or \t. With the -r they will be treated literally while without it they will be expanded to a carriage return or a tab respectively.
  • rm "$file" : this will remove each file in the list as it is read.

Or, you can use bash's mapfile builtin which lets you save the lines of a file in an array:

mapfile -t filelist < list.txt && rm "${filelist[@]}"
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

If you have a list of hundreds of filenames and you want to delete them, there is no reason to write separate rm commands for each of them. Assuming your file names saved in a file called list.txt, one per line, and that they do not contain newline characters, you could just do

while read -r file; do rm "$file"; done < list.txt 

###Explanation

  • The while read variable; do something; done < file construct will read each line from a file and save it as variable (in the example above, the variable's name is file). The -r is needed to allow for file names containing things like \r or \t. With the -r they will be treated literally while without it they will be expanded to a carriage return or a tab respectively.
  • rm "$file" : this will remove each file in the list as it is read.