20

I have a file that contains file names. 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 this:

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

5 Answers 5

31

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/
3
  • Thank you , for the answer . with this i can make a list of lot of files with [!] in their name , and move to another folder , with the follow : ls | grep -e ".[!]]" | tee 001.txt ; sed 's/^ *//' < 001.txt | xargs -d '\n' mv -t /destinypath/
    – inukaze
    Commented Feb 14, 2016 at 16:49
  • These don't work on OS X. "mv: illegal option -- t"
    – John Smith
    Commented Dec 15, 2020 at 9:47
  • @JohnSmith OS X is not using GNU versions of these utilities, AFAIK they use BSD versions which in general have fewer fancy options.
    – Anthon
    Commented Dec 16, 2020 at 7:37
8

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 
6
  • Hi terdon, how to move folders into new directory based on their match in the csv file? Could you please help me with this [unix.stackexchange.com/questions/433068/… Thank you !! Commented Mar 26, 2018 at 9:59
  • @user3351523 your question has been closed. Asking random people for help won't change that. Instead, edit the question, and explain how the solutions in the duplicate didn't help you. If something "didn't work", explain how it failed. The solutions should work for you, so you need to explain what happens when you try them.
    – terdon
    Commented Mar 26, 2018 at 11:23
  • I did that. you can have a look. Commented Mar 26, 2018 at 11:30
  • @user3351523 you haven't explained why the solutions of the dupe fail for you. And you haven't explained how the answers you have gotten fail. You might just need to use cp -Hr but I don't know if OSX cp supports that.
    – terdon
    Commented Mar 26, 2018 at 11:41
  • Nope. On OS X (Mojave, at least), both these command lines do absolutely nothing at all.
    – John Smith
    Commented Dec 15, 2020 at 9:43
4
for i in $(cat /tmp/list.txt); do mv "$i" /app/dest/; done
1
  • Can't handle spaces in filenames.
    – John Smith
    Commented Dec 15, 2020 at 9:46
1

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

edit 2 -- after @John Smith comment below

xargs -I{} < flist mv -v "{}" /app/dst

Quoting replace-str (see man xargs for explanation of replace-str) ensures filenames with blanks are treated as one argument. Newline becomes the input field terminator. However blank lines are ignored.

-I{} Implies the field separator is newline, and implies -L1 (use max one line for each output invocation). So mv is invoked for each input line.

2
  • 1
    -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
    Commented Feb 18, 2014 at 16:24
  • 1
    Nope. "mv: rename {} to /app/dst/{}: No such file or directory"
    – John Smith
    Commented Dec 15, 2020 at 9:41
0
mv `cat /tmp/list.txt` /app/dest/

(spaces at start are ignored)

1
  • Can't handle spaces in filenames.
    – John Smith
    Commented Dec 15, 2020 at 9:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.