2

I have a folder with a lot of images.

My client sent me a list of rejected images I need to delete from that folder.

The list is like this: _001,_002,_003,_004,_006,_007,RAM08953,RAM08995,RAM08996,RAM09039,RAM09060,RAM09087,RAM09101,RAM09104,RAM09115,RAM09126,RAM09170,RAM09171,RAM09172,RAM09176,RAM09188.

How can I easily run something that delete those files, or move those files inside a new folder or something without doing by hand? It can be an app, shell script or any tip.

0

You can delete a list of comma separated filenames using a CSV by using the following command:

for file in $( cat foo.csv | awk '{gsub(",","\n"); print $0}' ); do rm $file; done

You can make it safer by moving the file to the Trash instead of deleting it:

for file in $( cat foo.csv | awk '{gsub(",","\n"); print $0}' ); do mv $file ~/.Trash; done

How this works

This is a FOR/DO loop that first takes the contents of foo.csv and

  • cat foo.csv | This outputs the entire contents of the CSV file and pipes it to the next command
  • awk '{gsub(",","\n"); print $0} This line takes the output of the previous command and using the awk text processing utility then substitutes the comma (,) for the newline character (\n). It then outputs each item (print $0) on a separate line
  • each line becomes an "item" for the FOR loop to iterate.
  • for every item it will issue the command rm $file
  • You can delete/move/rename/whatever each file by modifying the command after do. (Eg. do mv $file ~/Foo/Bar/$file to move it to another folder.)
  • Needless use of cat and | as awk will read the file as an argument, that is to say,"$(awk '{gsub(/,/, "\n")}1' foo.csv)", is all that's required. – user3439894 Jun 8 '17 at 16:10
  • @user3439894 - Ok...your's is shorter and more efficient, but I like my method since I don't use awk but once in a blue moon and it flows with my coding style. There's nothing "needless" about any of it. – Allan Jun 8 '17 at 16:29
  • You said "There's nothing "needless" about any of it.", well the use of an unnecessary external command and pipe, when it is not at all needed, is indeed needless! I certainly do not mean anything derogatory by what I've said and while you may not use awk that often and while my awk command is more compact, nonetheless, that was not the main point being made. I'm only pointing out what's generally considered to be a very poor programing paradigm. FWIW I too use to code in that same poor manner but learned to do it better as a result of comments like these. – user3439894 Jun 8 '17 at 16:49
  • Point taken. Allow me to share one with you... Beginning your "critique" with the word "needless" is pejorative. – Allan Jun 8 '17 at 17:09
1

To delete a list of comma separated filenames from a folder, you can use the following:

tr , '\n' <<< "file.jpg,second.jpg,…" | xargs -I{} rm /path/to/folder/{}

Replace file.jpg,second.jpg,… with your comma separated list of files.
Replace /path/to/folder with the path to the containing folder of the images.

tr , '\n' replaces commas in the input with new lines. xargs takes the lines and runs the given command for each line, replacing {} with each line input (as instructed by -I{}). tr is used since xargs in macOS does not support -d for a custom delimiter.

However, I notice that your list does not include file extensions. You can add the extension to the end of the command instead.

tr , '\n' <<< "file,second,…" | xargs -I{} rm /path/to/folder/{}.jpg

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .