Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Loop over files listed in a text file and move them

I have a directory (directory A) with 10,000 files in it. I want to move some of them to directory B and the others to directory C. I made a text file that contains the names of all the files I want to move to directory B and another one with the names of all the files that I want to move to directory C. How can I write a bash for loop to move these files to the new directories.

Pseudocode:

for file in textfileB:
move file from directory A to directory B

for file in textfileC:
move file from directory A to directory C

Sorry if this is asked somewhere else, but I've spent hours trying to learn bash and I just don't get it. I wasn't able to find something similar enough in another thread that I could understand (maybe I just don't know the right search words).

I got something like this, but I couldn't get it working:

FILES=[dont' know what goes here? An array? A list?  

Can I just state the text file name and if so what format do the files have to be? name1.ext, name2.ext, or name1.ext name2.ext]

for f in $FILES; do mv $f /B/$f [not sure about the second argument for mv]; done

thx

BTW Mac OSX 10.6.8 (Snow Leopard) Apple Terminal v. 2.1.2 / 273.1 Bash 3.2

like image 430
PatentDeathSquad Avatar asked Jul 04 '11 01:07

PatentDeathSquad


1 Answers

cat file-list.txt | while read i; do
   # TODO: your "mv" command here.  "$i" will be a line from
   # the text file.
done
like image 81
asveikau Avatar answered Oct 13 '22 20:10

asveikau