Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Move files if ID in CSV file

We have a directory with files in the following format.

[id].mp4

We also have a list of ID’s in the form of a CSV file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

We need to move all files with the corresponding ID to a different directory.

Can someone please explain how to do this with a bash script?

>Solution :

it’s easy, first of all extracts the IDs, and then moves the corresponding files to the desired directory.

something like this :

#!/bin/bash

csv_file="path/to/ids.csv"
source_dir="path/to/source"
destination_dir="path/to/destination"

while IFS=',' read -r id; do
  #to remove double quotes from the ID
  id=$(echo "$id" | tr -d '"')
  
  file="${source_dir}/${id}.mp4"
  if [[ -f "$file" ]]; then
    mv "$file" "$destination_dir"
    echo "Moved file $file to $destination_dir"
  else
    echo "File $file not found"
  fi
done < "$csv_file"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading