1

I am using Ubuntu and want to split a folder of images (30k images) into different folders. However, I want to sort them (not just splitting into smaller subfolders). I have a file which contains information on the folder destination of the image. The file is structured as:

  • 123.jpg, 1
  • 124.jpg, 2
  • 125.jpg, 3

This should be interpreted as: image 123.jpg to folder 1, image 124.jpg to folder 2, image 125.jpg to folder 3.

Is there an efficient way to do this in Ubuntu?

Ismail
  • 11
  • 2
  • Sure, iterate over lines of the CSV files, split each line by the comma and move the file (first column) to the target directory (second column). Have you tried something like this already? – Pavel Dec 15 '17 at 14:55
  • @Pavel My bad, I am new to Ubuntu and have very little experience. Could you be more specific? – Ismail Dec 15 '17 at 14:56
  • https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/ – Pavel Dec 15 '17 at 14:58
  • Thank you! I understand it. – Ismail Dec 15 '17 at 15:09

2 Answers2

1

You can move the files based on the description provided in the file in the following way:

awk -F"," '{ print "mv",$1,$2}' description.txt | sh

This assumes that the destination directories listed in description.txt already exist.

The awk reads the description.txt file as comma (,) separated (set by the -F",") and add the necessary strings to the 1st ($1) and 2nd ($2) fields. First use the above command without the | sh part in order to test the output. If everything is fine then add the | sh in order to execute them, that pipe (|) the output of awk into shell.

The description.txt file contains your example:

123.jpg, 1
124.jpg, 2
125.jpg, 3
marcell
  • 1,498
  • 1
  • 10
  • 22
0

Assuming your file containing your files list and destinations is named list.txt

You can do it like this in a bash shell:

while IFS=, read -r file destination
do
    echo "mv $file $destination/$file"
done < list.txt

This is based on this question and answer: How to parse a CSV file in Bash?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Hadrien Huvelle
  • 382
  • 2
  • 11