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:
- Open the long list of filenames in ViM.
- Type
qa
to start recording a macro named "a". - Type
y$
to yank (copy) the filename. - Type
imv A/
to write "mv A/" in front of the filename, then pressEscape
. - Type
A B/
to write " B/" at the end of the line, then pressEscape
. - Type
pj^
to paste the filename and move to the beginning of the next line. - Press
q
to stop recording the macro. - Type
VG:normal @a
to replay the "a" macro until the end of the file. - Type
:wq rename.sh
to save as a bash script named "rename.sh" and quit. - Then finally, at the bash prompt, type
chmod +x rename.sh; ./rename.sh
to mark the script as executable and run it.