Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have run a command which finds a lot of files based on some search criteria. It returns the files like so:


So I was wondering, if I capture this output into a file (ie. one file per line), can anyone help me write a command which iterates through the file and moves the files one by one to a specified directory?

Thank you in advance.

share|improve this question
up vote 3 down vote accepted
while read -r filename
  mv -- "$filename" directory
done < list_of_files.txt

Not tested (I'm on a work PC running Windows) but should work.

share|improve this answer
The basic approach is sound, but your original version was missing several protections against “unusual” filenames. Always use read -r, as plain read expands some backslashes. Always use double quotes around variable substitutions unless you know why not to; without the quotes, the shell would expand whitespace and \[?* in file names. Finally, -- before the file names on the mv command line protects a file name beginning with a - from being considered an option by the mv command. – Gilles Nov 2 '10 at 23:28
You are absolutely correct. Dashed it off without proper care--thanks for the correction. – CarlF Nov 3 '10 at 12:35
Thanks! I also gave it a try myself and ended up with a 30 line php script. – mqchen Nov 6 '10 at 11:46
your_command | xargs -I% mv "%" dest_dir

If your command is find or you have some way of terminating the filenames with a null:

your_command | xargs -0 -I% mv "%" dest_dir


find [find-args] -print0 | xargs -0 -I% mv "%" dest_dir

Using nulls to terminate filenames allows this to work with filenames that may contain spaces, newlines, etc.

You can also use xargs to process your file if you've already created it:

xargs -a filename -I% mv "%" dest_dir
share|improve this answer
Why are you giving instructions on removing the files when he wants to move them? – frabjous Nov 2 '10 at 16:34
I'm betting Dennis copied that from an example and didn't customize it. – CarlF Nov 2 '10 at 16:36
No, I didn't copy it. I take full credit for the foul-up. I will correct my examples. – Dennis Williamson Nov 2 '10 at 17:13
@mqchen: Note the -I% in Dennis's answer. This is important: without it, xargs expects input quoted in a peculiar way and would fail if your file names contain characters such as whitespace and \'". – Gilles Nov 2 '10 at 23:23

Another option, if the filenames don't contain any whitespace, would be

mv `search_command_here` dest_directory

(The backtick syntax is also a bash-ism, but the question specifically states that this is on Linux and all major Linux distros use bash by default.)

share|improve this answer
The backtick syntax may be a Linux/Unix-ism, but it's not a Bashism. Every major shell uses it. Even the preferred $() syntax is not a Bashism as it's available in most modern shells and is specified by POSIX. – Dennis Williamson Nov 2 '10 at 17:17
Not only whitespace, but also wildcards (*?[) and backslashes in file names will be expanded by the shell with this command. – Gilles Nov 2 '10 at 23:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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