In my present directory I have many folders like F1, F2, F3, F4, F5, F6, F7, F8, F9, F10.

I have a csv file "dan.csv" in the present directory which has a column with folder names.

csv file looks like below: Column name is "id"


Based on their match in the csv file I would like to move the folders to a new directory "DAN".

To do this I tried like below:

xargs -a <(tail -n +2 dan.csv) -I{} sh -c '[ -d "{}" ] && mv -t DAN "{}"'

This didn't throw any error and also didn't move the folders into new directory "DAN".


A simple bash while loop is sufficient:

while read othercolumn1 othercolumn2 ... id ... othercolumnx
    mv $id DAN/
done < dan.csv

That reads every line from the CSV file, splits the line by the character set in the IFS variable, and stores each value of that line in the corresponding variables specified in the header of the while loop.

Make sure to fit the header of the while loop (where all columns are listed) to the header of your CSV file. You probably don't need the values of the other columns, but it's important to read them nevertheless, otherwise every value in the CSV would be treated as an id value. If your CSV file is seperated by something other than ;, make sure to set the specific character as the IFS=-value before the while loop.

If your dan.csv had four columns where the id is the third one, you'd do it like this:

while read othercolumn1 othercolumn2 id othercolumn4
    mv $id DAN/
done < dan.csv
  • In my csv file there is only one column "id" with folder names. So giving this way works? while read id; do mv $id DAN/; done < dan.csv – beginner Mar 27 '18 at 16:09
  • Yep, in case you have just one column, you just need to do while read i; do ... – LukeLR Mar 27 '18 at 16:09
  • I got an error like this. mv: missing destination file operand after ‘DAN/’ Try 'mv --help' for more information. – beginner Mar 28 '18 at 8:20
  • Replace mv $id DAN/ by echo $id for testing purposes. It should print all the folder names that are stored in the csv file without any empty lines in between. If there are empty lines, mv of course fails, because it needs at least 2 operands. – LukeLR Mar 28 '18 at 23:54
  • it prints empty lines – beginner Mar 29 '18 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.