Linux (mv or cp) specific files from a text list of files?

Solution 1:

In order to avoid a useless use of cat (and if you don't use rsync):

xargs -a file_list.txt mv -t /path/to/dest

This will handle any valid filename, unless it contains a newline, if the files are listed one per line.

Solution 2:

rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).

For example, this will do the trick:

rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory

Solution 3:

for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done

assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!


Solution 4:

rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/

Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.


Solution 5:

This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:

$ cat your_text_file | xargs cp -t /path/to/destination

Also, you can use find command with -exec option. to copy/move the files.

Tags:

Linux

Mv

Cp

Files