D. Lynch D. Lynch - 1 month ago 7x
Linux Question

Move multiple files to multiple to multiple directories

I have 5 files called file1.txt, file2.txt ... file5.txt and I would like to move each one into a respective directory called dir1, dir2 ... dir5.

So file1.txt is moved into dir1, file2.txt is moved into dir2 and so on.

Is there a way to do this in one line at the command line, using


I'm only suggesting
because I quite like this answer provided by Robert Gamble to a question asking how to copy one file to multiple directories.

echo dir1 dir2 dir3 | xargs -n 1 cp file1


I would personally prefer a solution that relies on a for loop, e.g.:

for n in {1..5}; do echo mv -- "file$n.txt" "dir$n/"; done

This can be done with xargs but I find the solution to be less elegant:

seq 1 5 | xargs -n1 -I{} mv -- "file{}.txt" "dir{}/"