LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-10-2014, 03:57 PM   #1
mamunm
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, 04:22 PM   #2
kbnuts
Member
 
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, 09:54 PM   #3
mamunm
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, 10:30 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
1 members found this post helpful.
Old 08-10-2014, 10:35 PM   #5
mamunm
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, 10:39 PM   #6
propofol
Member
 
Registered: Nov 2007
Location: Seattle
Distribution: Debian Wheezy & Jessie; Ubuntu
Posts: 334

Rep: Reputation: 60
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.

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


Reply



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 12:36 PM
[bash] Copy files, move files but not added ones errgrdgds Programming 4 05-24-2013 06:46 AM
bash programming - rename files in a loop turki_00 Programming 10 03-31-2011 09:41 PM
[SOLVED] BASH Question: Using a loop to download a series of files roystonlodge Programming 2 02-02-2010 02:18 PM
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 07:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:43 PM.

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