Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-10-2014, 04:57 PM   #1
LQ Newbie
Registered: Apr 2014
Posts: 12

Rep: Reputation: Disabled
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
Old 08-10-2014, 05:22 PM   #2
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
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.
Old 08-10-2014, 10:54 PM   #3
LQ Newbie
Registered: Apr 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
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
Old 08-10-2014, 11:30 PM   #4
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288

does the following do what you want?
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.



PS. Remove the "echo" to actually make it work.
1 members found this post helpful.
Old 08-10-2014, 11:35 PM   #5
LQ Newbie
Registered: Apr 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
Old 08-10-2014, 11:39 PM   #6
Registered: Nov 2007
Location: Seattle
Distribution: Debian Wheezy & Jessie; Ubuntu
Posts: 331

Rep: Reputation: 59
How about:

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


Oops, Evo2 beat me to it.

Last edited by propofol; 08-10-2014 at 11:40 PM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need a BASH for loop for processing many files and naming the output files Jeff9 Linux - Newbie 13 05-02-2014 01:36 PM
[bash] Copy files, move files but not added ones errgrdgds Programming 4 05-24-2013 07:46 AM
bash programming - rename files in a loop turki_00 Programming 10 03-31-2011 10:41 PM
[SOLVED] BASH Question: Using a loop to download a series of files roystonlodge Programming 2 02-02-2010 03:18 PM
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 08:09 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration