Kalyan Dey
Kalyan Dey

Reputation: 1

How to write a shell script to move several files from different sub-directories having same name to a different location?

I have a file structure like that

/home/code/AXP1/file.dat
/home/code/AXP2/file.dat
/home/code/AXP3/file.dat
/home/code/AXP4/file.dat

I want to move all the files with .dat extension under the sub-directories APX1, APX2, APX3, APX4, etc. to a specific location, say /home/BOX with the name like file1.dat, file2.dat, file3.dat, file4.dat, etc.

So, could you please give a an idea how to write a Linux shell script to do the above task.

Regards

Upvotes: 0

Views: 1548

Answers (1)

fedorqui
fedorqui

Reputation: 290415

What about this?

mv /home/code/APX[1-4]/file.dat /home/BOX/

If you want to keep the number of the folder they were, you can do this:

for i in {1..4}
do
  mv /home/code/APX$i/file.dat /home/BOX/file$i.dat
done

If you have more than 4 folder, let's say n, change 4 for n.

Upvotes: 1

Related Questions