LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-05-2008, 03:19 PM   #1
bostonuser
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Rep: Reputation: 0
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
 
Old 06-05-2008, 06:31 PM   #2
MS3FGX
Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
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]*
 
Old 06-05-2008, 06:52 PM   #3
DotHQ
Member
 
Registered: Mar 2006
Location: Ohio, USA
Distribution: Red Hat, Fedora, Knoppix,
Posts: 542

Rep: Reputation: 33
add a -r to the cp command to make it recursive.
cp -r prodinit_[4-22]* /path_to_target_dir/
 
Old 06-05-2008, 07:35 PM   #4
Uxinn
Member
 
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47

Rep: Reputation: 16
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 07:41 PM.
 
Old 06-05-2008, 11:01 PM   #5
bostonuser
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 06-06-2008, 12:31 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
There's these:

http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://www.comptechdoc.org/os/linux/
http://rute.2038bug.com/index.html.gz

Enjoy
 
Old 06-06-2008, 02:21 AM   #7
grizly
Member
 
Registered: Nov 2006
Location: Melbourne Australia
Distribution: Centos, RHEL, Debian, Ubuntu, Mint
Posts: 128

Rep: Reputation: 16
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.
 
Old 06-06-2008, 02:24 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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/
 
  


Reply


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
how to copy images recursively via bash mindfriction Programming 6 12-13-2007 11:25 PM
bash: list recursively directories only kpachopoulos Programming 1 06-18-2007 03:00 AM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 05:24 AM
Very simple shell script - move files to different directories beley Programming 7 11-02-2006 06:24 AM
recursively copy php.ini to www directories osio Programming 3 07-01-2005 05:24 AM


All times are GMT -5. The time now is 11:35 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration