Search code examples
bashcsvfindrm

How to delete files from directory using CSV in bash


I have 600,000+ images in a directory. The filenames look like this:

1000000-0.jpeg
1000000-1.jpeg
1000000-2.jpeg
1000001-0.jpeg
1000002-0.jpeg
1000003-0.jpeg

The first number is a unique ID and the second number is an index.

{unique-id}-{index}.jpeg

How would I load the unique-id's in from a .CSV file and remove each file whose Unique ID matches the Unique ID's in the .CSV file?

The CSV file looks like this:

1000000
1000001
1000002

... or I can have it separated by semicolons like so (if necessary):

1000000;1000001;1000002

Solution

  • You can set the IFS variable to ; and loop over the values read into an array:

    #! /bin/bash
    while IFS=';' read -a ids ; do
        for id in "${ids[@]}" ; do
            rm $id-*.jpg
        done
    done < file.csv
    

    Try running the script with echo rm ... first to verify it does what you want.