Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file the contain filenames, for example:

/tmp/list.txt (it is with the spaces at the start of each line):

  /tmp/file.log
  /app/nir/home.txt
  /etc/config.cust

I want, using one line, to move all the files listed in /tmp/list.txt to /app/dest

so it should be something like that:

cat /tmp/list.txt | xargs mv /app/dest/
share|improve this question

4 Answers 4

up vote 6 down vote accepted

You are just missing the -t option for mv (assuming GNU mv):

cat /tmp/list.txt | xargs mv -t /app/dest/

or shorter (inspired by X Tian's answer):

xargs mv -t /app/dest/ < /tmp/list.txt

the leading (and possible trailing) spaces are removed. Spaces within the filenames will lead to problems.

If you have spaces or tabs or quotes or backslashes in the filenames, assuming GNU xargs you can use:

sed 's/^ *//' < /tmp/list.txt | xargs -d '\n' mv -t /app/dest/
share|improve this answer

Assuming your file names are relatively sane (no newlines or weird characters):

while read file; do mv "$file" /app/dest/; done < list.txt 

To deal with weird file names (breaks if a file name has a newline):

while IFS= read -r file; do mv "$file" /app/dest/; done < list.txt 
share|improve this answer
for i in $(cat /tmp/list.txt); do mv "$i" /app/dest/; done
share|improve this answer

Pure xargs reading directly from file

xargs -l -i < flist  mv -v {} /app/dst

edit 1 -- after @Anthon 's comment below,

xargs -I{} < flist  mv -v {} /app/dst
share|improve this answer
    
-i is deprecrated, and it, or it replacement -I imply -l/--max-lines=1. And it causes mv to be executed for each file separately. –  Anthon Feb 18 at 16:24

Your Answer

 
discard

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.