LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash for loop to copy files (https://www.linuxquestions.org/questions/linux-newbie-8/bash-for-loop-to-copy-files-4175514159/)

mamunm 08-10-2014 03:57 PM

bash for loop to copy files
 
I want to copy job-5, job-6, job-7..... to LA-1, LA-2 LA-3 ...
My command is : for subdir in LA-*; do (cd "$subdir"; cp -r /scratch/job-$(i+4) .); done But linux is not taking this command, can i do it in other ways? Thanks

kbnuts 08-10-2014 04:22 PM

well you've not defined a few things there. just putting LA-* doesn't invoke a filesystem command for starters so you'll need define it. I'm about to go to bed so can't give you a full answer but your first part will be more like:
for subdir in $(ls -l |awk '{print $9}' |grep LA); do...etc
Also you're pulling a command i+4 which doesn't make any sense in that context. If I had time I'd tell you the rest but my bed is calling me.

mamunm 08-10-2014 09:54 PM

I think my questions was not clear. I have folders named job-5 to job-7 and they are running in scratch directory. after the job is finished i want to copy them in folders named LA-1 to LA-4. Actually my main question was can i use $(i+4) for file name? If it was job-1 to job-4 and i had to copy them to LA-1 to LA-4, there was no problem, my above command can do that easily:for subdir in LA-*; do (cd "$subdir"; cp -r /scratch/job-$i .); done

evo2 08-10-2014 10:30 PM

Hi,

does the following do what you want?
Code:

for i in {1..9} ; do j=$((i+4)) ; echo cp -r /scrach/job-$j LA-$i; done
If not, please explain again. Perhaps write out the commands that you want run (for a single value of i), and we can show you how to automate it.

Cheers,

Evo2.

PS. Remove the "echo" to actually make it work.

mamunm 08-10-2014 10:35 PM

Thanks, i think that would do the job. At first i tried two loops using i and j but than i realized that would be a mess. thanks for the idea.

propofol 08-10-2014 10:39 PM

How about:

Code:

cd scratch
for i in {1..3}; do echo "cp -r LA-$((i+4)) job-$i"; done

Remove echo & " " if it looks right.

Regards,
Stefan

Oops, Evo2 beat me to it.


All times are GMT -5. The time now is 12:03 PM.