0

I have two files of file and directory names. I want to map mv command from each row of a filenames file to each row in the directory names file. The files are small.

If it helps I have files named sequentially (say f1, f2, f3...f1000). Is there any way to do it in a loop reading one file and one directory?

There can be 3 use cases: One file to many directories, Many files to one directory and many files to many directories (1 file/line = 1 dir/line in my case). My use case pertains to the last one. I have seen xargs being used in some of the use cases but I am not sure how to modify for my use case.

Following questions do not help: Moving large number of files

Coddy
  • 467
  • 3
  • 14

1 Answers1

2

Assuming you have a file named files that contains file names, each in a new line, and a file named dirs with directories each in a new line, both having the same number of entries eg:

files

file1
file2
file3

dirs

dir1
dir2
dir3

Then to move file1 to dir1, file2 to dir2 and so on you can use the command:

paste dirs files | xargs -n2 mv -t

paste joins the lines from both files, then xargs takes two arguments and calls the mv command with them. The -t option selects the destination directory. Below is the relevant fragment from the mv documentation.

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

-t, --target-directory=DIRECTORY
          move all SOURCE arguments into DIRECTORY
Dzienny
  • 3,165
  • 1
  • 19
  • 29