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
  • 549
  • 4
  • 18
  • One idea: use `mapfile` to load each file into an array. Iterate from 0 to N and use that as the index into the two arrays to get a pair of names to use with `mv`. – Shawn Feb 04 '20 at 18:16
  • 1
    also asked at https://unix.stackexchange.com/q/565763/4667 – glenn jackman Feb 04 '20 at 18:28
  • Something like `paste <(sed 's/^/mv /' files) dirs | bash`? Some adjustment required if any file or directory names have to be quoted. If you show some example input, answers can be more specific. – Benjamin W. Feb 04 '20 at 18:30

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,295
  • 1
  • 20
  • 30