2

I have a folder filled with 100 log files, some of them have data, some dont. I have a script that picks out the files with the data and have the names saved in a text file named "goodfiles.txt" I would like to know how i can read the files in the text file and move them to another folder. I have tried this:
cat /root/testing2/goodfiles.txt | xargs mv -f /root/testing2/moved/
but it gives me an error saying

mv: Target (log1508.log) must be a directory in order to move
mv: directories or multiple files to it.

can someone please help me?

1
  • It looks like you're confusing the -f and -t options of GNU mv Oct 1, 2019 at 13:21

3 Answers 3

2

This is a slightly different, but conceptually simple approach (avoiding pipes):

for fileName in `cat goodfiles.txt`; do mv $fileName /root/testing2/moved/; done
1
  • Thank you! this worked perfectly! Oct 19, 2019 at 14:25
0

I think it helps to remember that you can change the part that xargs replaces using the --replace option and specify the replacement string. Otherwise it appends the line on the end.

I was able to do roughly the same with

cat filelist |xargs --replace='{}' mv {} ./targetdir/

For you maybe try

cat /root/testing2/goodfiles.txt |xargs --replace '{}' /root/testing2/moved
2
  • I tried that but it gave me this error: xargs: illegal option -- - Oct 1, 2019 at 16:32
  • Can you put the specific command you tried?
    – Woodsman
    Oct 2, 2019 at 0:57
0

It looks like you're confusing the -f option of GNU mv with the -t option, which is what you want here in order to move multiple sources to a single target with xargs

   mv [OPTION]... -t DIRECTORY SOURCE...

Note that plain xargs breaks input into whitespace delimited arguments, so your command would break if any of the filenames contains whitespace. Also it is a UUOC.

So

xargs -d'\n' mv -t /root/testing2/moved/ </root/testing2/goodfiles.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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