LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Moving files from parent path to multiple child path using bash in efficient way (https://www.linuxquestions.org/questions/programming-9/moving-files-from-parent-path-to-multiple-child-path-using-bash-in-efficient-way-4175541517/)

Karthikgv417 05-03-2015 05:05 PM

Moving files from parent path to multiple child path using bash in efficient way
 
Hi All,

Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created.

/Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt
/Path/AdminUser/User1/2222/Reports/bbb.txt to /Path/User1/2222/Reports/bbb.txt
/Path/AdminUser/User2/3333/Reports/ccc.txt to /Path/User2/3333/Reports/ccc.txt
/Path/AdminUser/User2/4444/Reports/ddd.txt to /Path/User2/4444/Reports/ddd.txt

I don't want to move directory structure. I want to move files from that source path to destination path on daily basis. I would need pointer to write a bash script to make sure aaa.txt would exactly go and sit only from /Path/AdminUser/User1/1111/Reports/ to /Path/User1/1111/Reports/........

Ex:

#!/bin/bash

dir1="/Path"
subs= `ls $dir1/AdminUser`
for i in $subs; do
mv $dir1/AdminUser/$i/*/Reports $dir1/$i/*/Reports
done

Karthikgv417 05-03-2015 05:49 PM

One of geek folk helped provided with inputs to start as below, It still threw error, can we also write it in an efficient way?

#!/bin/bash
dir1=/Path
for i in "$dir1"/AdminUser/*; do
if [[ -d $i && ! -L $i ]]; then
dir2="${i##*/}"
for j in "$i"/*; do
if [[ -d $j && ! -L $j ]]; then
j="${j##*/}"
mv "$i"/"$j"/Reports "$dir1"/"$dir2"/"$j"/
fi
done
fi
done

syg00 05-03-2015 06:57 PM

Perhaps you should talk to your "geek folk" then - much easier for the original author to follow up. Although "it still threw error" is useless to anyone trying to help.
Also, why are you so fixated on "efficient way". What are you worried about - define what you mean by efficient.

Karthikgv417 05-03-2015 08:32 PM

If there are multiple child directories, considering performance can any thing like xargs play a major role.

syg00 05-03-2015 09:11 PM

Moving files within the same filesystem is simply a matter of updating the directory entry - no I/O for copying the data. It is (very) efficient by its nature.

NevemTeve 05-03-2015 10:05 PM

please use [code] and [/code] tags when quoting code.


All times are GMT -5. The time now is 08:27 PM.