Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-05-2008, 02:19 PM   #1
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 ..

I am new to shell programming and any help will be appreciated.

Old 06-05-2008, 05:31 PM   #2
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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:

cp prodinit_[4-22]*
Old 06-05-2008, 05:52 PM   #3
Registered: Mar 2006
Location: Ohio, USA
Distribution: Red Hat, Fedora, Knoppix,
Posts: 545

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, 06:35 PM   #4
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]'



for file in ` ls -1 $dir1|grep -E '_[4-9]|_[1][0-9]|_[2][0-2]'`; do
  cp  $dir1/$file $dir2
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.
Old 06-05-2008, 10:01 PM   #5
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.

Old 06-05-2008, 11:31 PM   #6
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,456

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
There's these:

Old 06-06-2008, 01:21 AM   #7
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, 01:24 AM   #8
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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
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 10:25 PM
bash: list recursively directories only kpachopoulos Programming 1 06-18-2007 02:00 AM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 04:24 AM
Very simple shell script - move files to different directories beley Programming 7 11-02-2006 05:24 AM
recursively copy php.ini to www directories osio Programming 3 07-01-2005 04:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 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