Move all files from one folder to another, based on a list

You could use rsync to move them, assuming your list of files is one filename per line.

rsync -av --remove-source-files --files-from filelist.txt sourceDir/ targetDir/

If your files are absolute names (i.e. the names begin with /) the sourceDir should be /. Otherwise it should be the root of the relative names.

Example

$ mkdir src dst
touch src/{one,two,three}
$ cat >filelist.txt <<EOF
one
two
EOF

$ ls src
one  three  two
$ ls dst

$ rsync -av --files-from filelist.txt --remove-source-files src/ dst/
building file list ... done
one
two

sent 165 bytes  received 70 bytes  470.00 bytes/sec
total size is 0  speedup is 0.00

$ ls src
three
$ ls dst
one  two

If you gave GNU core utilities (or other implementations with these specific features) you can use xargs to build an argument list for mv based on the file list:

cd A
xargs -rd '\n' -- mv -t B -- < file-list.txt

Without GNU utilities you can still use a while-read loop. In Bash that could be:

while IFS= read -r file; do
    mv "A/$file" "B/$file"
done < file-list.txt

You can easily solve this in ViM with these 10 simple steps:

  1. Open the long list of filenames in ViM.
  2. Type qa to start recording a macro named "a".
  3. Type y$ to yank (copy) the filename.
  4. Type imv A/ to write "mv A/" in front of the filename, then press Escape.
  5. Type A B/ to write " B/" at the end of the line, then press Escape.
  6. Type pj^ to paste the filename and move to the beginning of the next line.
  7. Press q to stop recording the macro.
  8. Type VG:normal @a to replay the "a" macro until the end of the file.
  9. Type :wq rename.sh to save as a bash script named "rename.sh" and quit.
  10. Then finally, at the bash prompt, type chmod +x rename.sh; ./rename.sh to mark the script as executable and run it.

Tags:

Scripting

Mv