Visit Our UNIX and Linux User Community

copy files with diresctory hirarchy

Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users copy files with diresctory hirarchy
# 1  
Old 07-29-2006
copy files with diresctory hirarchy

I have a directory x and in that I have x.cpp and a directory named Y

and in y I have y.cpp and so on. and some other files too in those directories.

How can I copy only *.cpp files in whole x directory hirarchy to a different location with the same hirarchy?

I mean the whole x directory hirarchy with all *.cpp files and sub-directory names need to be copied to different location.

Any idea?
# 2  
Old 07-29-2006
Below is the correct version which works well as per your needs, I should have edited this post only instead of putting a new post, any ways, you can run the script and modify as per your needs.


Last edited by tayyabq8; 07-30-2006 at 02:03 AM.. Reason: Code deleted, recursive function was missing
# 3  
Old 07-29-2006
This might also work, and is a little cleaner.



# copy only directories with "*cpp" filenames
find $INDIR -type f -name "*cpp" -exec dirname {} \; |
  sed -e "s/^\($INDIR\)\(.*\)/$OUTDIR\2/g" | xargs mkdir -p

# copy all "*cpp" files to new directory hierarchy
for FILE in `find $INDIR -type f -name "*cpp"` ; do
  NEWFILE=`echo $FILE | sed -e "s/^\($INDIR\)\(.*\)/$OUTDIR\2/g"`
  cp -p $FILE $NEWFILE

# 4  
Old 07-29-2006
With recursive function, which was missing in earlier version:
#! /bin/ksh

inDir="/home/admin/temp1"       #your source directory goes here
outDir="/home/admin/temp"       # your taget directory goes here
let count=0

#Copy *.cpp files from your source directory to the target, if any
cp $inDir/*.cpp $outDir 2>/dev/null

#Function, which copies *.cpp of each directory recursively

cpfiles () {
if [[ ! -d $outDir/$dir ]]; then
        mkdir $outDir/$dir


for filename in $inDir/$dir/*; do
        if [[ -d $filename ]]; then
                cpfiles $a
                ext=`echo $filename | cut -d. -f2`
                if [[ $ext = "cpp" ]]; then
                        cp $filename $outDir/$1
                        let count=$count+1

# Only directories are listed, if you have -d option you can only use
# ls -d instead of following command in loop

for dirname in `ls -l $inDir | grep '^d' | awk '{print $9}'` ; do
           cpfiles $dirname

echo "$count files copied" 2>&1

Any suggestions would be highly appreciated.


Last edited by tayyabq8; 07-30-2006 at 06:31 AM..
# 5  
Old 07-29-2006
cd <source directory>
find . -type f -name "*.cpp" | cpio -pmd <destination directory>

Last edited by reborg; 07-30-2006 at 04:14 PM..
# 6  
Old 07-29-2006
With GNU cp
$ cd <source directory>
$ find -type f -exec cp --parents {} <destination directory> \;

# 7  
Old 07-30-2006
Hi ,
All those suggestions worked, except I couldn't test Hitori's, I only have Sun OS 5.7.

Thanks a lot guys, I liked

find. -type f -name "*.cpp" | cpio -pmd <destination directory> by reborg !

I nener knew about cpio until today!

Previous Thread | Next Thread
Test Your Knowledge in Computers #688
Difficulty: Medium
The first computer to implement architectures enabling reverse Polish notation (RPN) was the Apple I computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy all files together

Hi imagine that i have some data files like: test.20150902 test.20150903 test.20150904 i would like to create one single file automatically (like cat test.20150902 test20150903 > output) in order to copy all files togheter with a time order. Thanks! Regards Please use... (2 Replies)
Discussion started by: Board27
2 Replies

2. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

3. Red Hat

Unable to copy files due to many files in directory

I have directory that has some billion file inside , i tried copy some files for specific date but it's always did not respond for long time and did not give any result.. i tried everything with find command and also with xargs.. even this command find . -mtime -2 -print | xargs ls -d did not... (2 Replies)
Discussion started by: before4
2 Replies

4. Shell Programming and Scripting

how to copy files followed by list of names of all the files in /etc?

....... (2 Replies)
Discussion started by: pcbuilder
2 Replies

5. Solaris

How to safely copy full filesystems with large files (10Gb files)

Hello everyone. Need some help copying a filesystem. The situation is this: I have an oracle DB mounted on /u01 and need to copy it to /u02. /u01 is 500 Gb and /u02 is 300 Gb. The size used on /u01 is 187 Gb. This is running on solaris 9 and both filesystems are UFS. I have tried to do it using:... (14 Replies)
Discussion started by: dragonov7
14 Replies

6. UNIX for Dummies Questions & Answers

copy files

Hi Team, I am unable to copy the files, when i run the below script, i am getting error as file not present, not sure what i am missing. # File to be looked upon File_Pattern='*.zip' TMP_FILE=flagfile Check=`find $Directorypath -name $File_Pattern -type f -newer $TMP_FILE -print |... (6 Replies)
Discussion started by: Naveen_5960
6 Replies

7. UNIX and Linux Applications

Copy all files

how i can copy all files " select all " in one step t try command cp -t (2 Replies)
Discussion started by: walidfinder
2 Replies

8. UNIX for Dummies Questions & Answers

copy only new files or files of a different size

hello i would like to copy files from 1 location to a nother, but it has only to copy files which are newer or have a different filesize. all has to be logged to a copy.log file (als skipped files should be in the log) is this possible with the cp command (1 Reply)
Discussion started by: arnoldg
1 Replies

9. Shell Programming and Scripting

Copy all files except one

How can I copy all the files in a given directory except one. cheers (7 Replies)
Discussion started by: spaceship
7 Replies

10. UNIX for Advanced & Expert Users

copy files

Hi, Under the home directory, I want to search for all the *.xml files and move them all into another folder under home. Is it possbile using a single find command . Regards, Chirayu Sutaria (6 Replies)
Discussion started by: chirayus
6 Replies

Featured Tech Videos