Copying excluding some files and folders


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Copying excluding some files and folders
# 1  
Old 05-24-2013
Copying excluding some files and folders

I have a main folder.
Inside that i have many(50) subfolders.
In each subfolder, there are a no of large files(500 files ) present.
I want to do copy operation for some files from some of the subfolders to /usr/tmp.
I have the list of the subfolders and list of of files which i dont want to copy.
Could you please let me know how to do it without using rsync. it would not hamper the performance as file size are large.I m using solaris .Thank you in advance.

Last edited by joeyg; 05-24-2013 at 03:46 PM..
# 2  
Old 05-24-2013
You could either copy everything and then delete what you don't want (probably rather wasteful) or you could use a find command. Something like:-
Code:
cd sourcedir

find . -type d | grep -vf exclude-file | while read dir
do
   echo "Creating directory based on $dir"
   mkdir targetdir/$dir
done

find . -type f | grep -vf exclude-file | while read file
do
   echo "Copying $file"
   cp $file targetdir
done

Your exclude file would need to be in the right format, e.g.
Code:
^./not_this_file
^./not_this_directory
not_a_file_or_directory_ending_this$
not_a_file_or_directory_with_this_anywhere

etc.

If you are running AIX (and perhaps a few others) it allows the -L flag for the tar command. You can use find to create your list of files that you do want, then:-
Code:
cd sourcedir
tar -cvf - -L filelist | ( cd targetdir ; tar -xvf - )

Similar processes could be done with cpio, and perhaps backup/restore; dump/restore; ufsdump/ufsrestore etc. as appropriate to your OS.



Robin
# 3  
Old 05-24-2013
Quote:
Originally Posted by rbatte1
Code:
find . -type d | grep -vf exclude-file ...

That's an unsafe grep. If the exclude file is a list of pathnames and not regular expressions, you must use fixed-string whole-line matches, otherwise a literal character could be interpreted as a regular expression metacharacter, and/or a pathname can match a substring in a longer pathname.

A possible alternative solution:
Code:
find source -type d | grep -vxFf exclude-file | pax -rw destination

It would be simplest if absolute paths are used in the file and with find.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 4  
Old 05-28-2013
I am working on solaris.
Here thre is no -f option for grep.
Is there any alternative i can use for solaris?

---------- Post updated at 02:58 AM ---------- Previous update was at 01:51 AM ----------

Thank you guys..it worked

i tried with /usr/xpg4/bin/grep

---------- Post updated at 11:24 PM ---------- Previous update was at 02:58 AM ----------

Hi Robin,

i was trying this in Solaris but i am getting below error as "do unexpected".
Could you pls let me know where this code went wrong.

Code:
 
$ cat test1.sh
find /usr/tmp/SB/reports -type f -name *.rdf | /usr/xpg4/bin/grep -F -v -f /usr/tmp/SB1/exclude.txt |read filename
do
        echo "Copying $filename to /usr/tmp/SB1"
        cp $filename /usr/tmp/SB1
done

 
$ sh test1.sh
test1.sh: syntax error at line 3: `do' unexpected


If i run below...it is giving me correct filename.

Code:
 
find /usr/tmp/SB/reports -type f -name *.rdf | /usr/xpg4/bin/grep -F -v -f /usr/tmp/SB1/exclude.txt

# 5  
Old 05-28-2013
To start the loop, you need a while read statement. You just have read which will grab the output in one go and results can be a little odd.


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copying folders with only certain files

Hello gurus, quick question I have a bunch of folders each having subfolders that have 3 files with the same name but different in content. So I want to copy the subfolders with only those 3 files to another location. Just making up some names here, Folder1 SubfolderX1 SubfolderY1 ... (1 Reply)
Discussion started by: ritakadm
1 Replies

2. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

3. Shell Programming and Scripting

Copying files excluding some files

Hi, I have a folder which contains files in this format. abc-bin.000001 abc-bin.000002 abc-bin.000003 abc-bin.000004 abc-bin.000005 abc-bin.000006 abc-bin.000007 abc-bin.000008 abc-bin.000009 abc-bin.000010 abc-bin.index I want to copy all the files between abc-bin.000004... (6 Replies)
Discussion started by: arijitsaha
6 Replies

4. Programming

Copying folders from one location to another..

Hi all, I need a suggestion in the following case, I have folder1, folder2 ,folder3 and file1 inside /home/test/source .. I need to copy all the folders and files to another location /home/test/destination Pls suggest any way to program this in C++.. :confused: (1 Reply)
Discussion started by: selvarajvs
1 Replies

5. UNIX for Dummies Questions & Answers

finding files in unix and excluding certain folders

hi good day I am using redhat linux. Is there a better utility than the 'find' command to search for files I am trying to search through and enitre directory such as /repos for a files that have 'UAP' in it. However there are some '.step' folders littered throughout the /repos folder at varying... (8 Replies)
Discussion started by: johnstrong
8 Replies

6. OS X (Apple)

Automated command ; extracting files from folders and copying them into a single folder

Hello everyone, I'm running Mac OS X Leopard (10.5.8) and I want to use the Terminal to help automate this tedious and laborious command for me: I need to extract all of the .m4p files in my "iTunes Music" folder which reside in folders of the artist, and then subfolders for the albums and... (2 Replies)
Discussion started by: qcom
2 Replies

7. UNIX for Advanced & Expert Users

Copy Structure excluding some folders

Hi I want to copy the structure from one place to another. -> cd /hol/; -> find . -type d | cpio -pvdm /abc/cat; while copying the structure I want to exclude some directories like test1 and Test. I have read somewhere that this can be done with -prune option. Could anyone... (2 Replies)
Discussion started by: soumodeep123
2 Replies

8. Shell Programming and Scripting

Copying specific files from remote m/c to specific folders

Hi All, I am trying to rsync some of the latest files from remote m/c to my local linux box. Folder structure in my remote m/c looks like this /pub/Nightly/Package/ROLL/WIN /pub/Nightly/Package/SOLL/sol /pub/Nightly/Package/SOLL/linux Each of the folder contains gzip files which on daily... (0 Replies)
Discussion started by: jhoomsharabi
0 Replies

9. UNIX for Dummies Questions & Answers

Copying Folders without some folders... ;-)

I am in a fix....... I have to write a backup script to backup say Folder A. Folder A contains n folders 1,2 ,3 .....n. my script should copy A without folder 2 & 3. Is there anyway I can do it without writing individual copy commands???? Please help.... (5 Replies)
Discussion started by: chimpu
5 Replies

10. UNIX Desktop Questions & Answers

tar backup with excluding some folders

Hi , I want to backup the root file system but the size of / is very huge so I want to exclude some file systems.Man page of tar says X option excludes files but I could not do that.I use this command $ tar -cvf deneme.tar -X exc . $ cat exc sql kkm I think there... (2 Replies)
Discussion started by: kudret_gulcan
2 Replies
Login or Register to Ask a Question