Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-05-2008, 02:19 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2008
Posts: 5
Rep:
|
copy directories and files recursively using C shell or bash shell
Hi all,
I am trying to copy couple of directories and their files to another place. Each directory have 25-30 files and numbered accordingly, the problem I am facing is, I have to copy files numbered from 4-22 only and leave rest all.
I have written a code but its only copying one directory and its files and not others.
Overall /test1/step_1/prodinit_1....30 and I want to copy to /test2/step_1/prodinit_4...22
#!/bin/csh -f
set dir1 = /home/rr/copy/test1
set dir2 = /home/rr/copy/test2
set step = 1
set stepmax = 30
set num = 4
set nummax = 22
mkdir $dir2/step_${step}
echo $dir2/step_${step}
while ($step < 30)
cd $dir2/step_${step}
cp -r $dir1/step_${step}/prodinit_${num}*.* $dir2/step_${step}
echo "Copying file .."
@ num = ${num} + 1
if ($num == $nummax ) exit
cd ..
end
I am new to shell programming and any help will be appreciated.
Bostonuser
|
|
|
|
06-05-2008, 05:31 PM
|
#2
|
|
Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,817
|
Is this part of some assignment where you need to write a loop to do this?
I ask because you can copy ranges of files with normal Bash expressions, the command to copy prodinit_4 to prodinit_22 would be something like:
Code:
cp prodinit_[4-22]*
|
|
|
|
06-05-2008, 05:52 PM
|
#3
|
|
Member
Registered: Mar 2006
Location: Ohio, USA
Distribution: Red Hat, Fedora, Knoppix,
Posts: 542
Rep:
|
add a -r to the cp command to make it recursive.
cp -r prodinit_[4-22]* /path_to_target_dir/
|
|
|
|
06-05-2008, 06:35 PM
|
#4
|
|
Member
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47
Rep:
|
lol so I guess this might be an overkill
or what... hmm tried your cp command and it does not work
only copies file 2 and 20-29?
ls -1 |grep -E '_[4-9]|_[1][0-9]|_[2][0-2]'
Code:
#!/bin/bash
dir1=~/sms/dir1
dir2=~/sms/dir2
for file in ` ls -1 $dir1|grep -E '_[4-9]|_[1][0-9]|_[2][0-2]'`; do
cp $dir1/$file $dir2
done
I'm a newbie scripter so i'm sure there is a better way to do this
Last edited by Uxinn; 06-05-2008 at 06:41 PM.
|
|
|
|
06-05-2008, 10:01 PM
|
#5
|
|
LQ Newbie
Registered: Jun 2008
Posts: 5
Original Poster
Rep:
|
Thanks to all of you for your time and suggestions.
In the mean time I wrote the program in Perl and it worked perfectly.
The only thing I was not able to do using shell scripting was to make directories recursively and then copy the content from source to destination. Any good tutorial for bash shell scripting??
Once again thanks to all.
Bostonuser 
|
|
|
|
06-05-2008, 11:31 PM
|
#6
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
|
|
|
|
06-06-2008, 01:21 AM
|
#7
|
|
Member
Registered: Nov 2006
Location: Melbourne Australia
Distribution: CentOS
Posts: 125
Rep:
|
when you ran mkdir, use the -p switch
$mkdir -p /test/loc1/dir2/blah/foo/bar
it will fill in all the other directories recursively to make the path correct.
|
|
|
|
06-06-2008, 01:24 AM
|
#8
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
If you use the bash shell, and thanks to the names not using leading zero's, you can use brace expansion to select the files to copy:
cp dir1/files_{4..22} $dest/dir1/
cp dir2/files_{4..22} $dest/dir2/
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:31 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|