To copy everything except 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To copy everything except 2 files
# 1  
Old 10-07-2008
To copy everything except 2 files

Hi all,

I would want to copy everything in a particular directory. However would want to exclude 2 files:
DIMStemp01.dbf
DIMSts01.dbf

I tried to:
(1) ls files except these 2 files into abc.txt
(2) Read from abc.txt and start copying.

It works, however is there any easier way? Eg. directly copy those files [excluding these 2]?

Thanks.
# 2  
Old 10-08-2008
MySQL

Several ways. In ksh and bash you can use
Code:
cp !(foo|bar) /my/destination/dir

to exclude files named "foo" and "bar".

Otherwise, you can do something like this:
Code:
echo cp `/bin/ls -1 . |grep -v "^foo$" | grep -v "^bar$" ` /my/destination/dir

There are more elegant and faster ways, but I'm not sure about portability. Also, this will pick up directory names, but rm will not remove them. Finally, when you are sure it's what you want to do, remove the "echo".

If your directory is really large, you'll need to use "xargs":
Code:
 ls -1 |grep -v ^foo$ | grep -v ^bar$ |xargs -i sh -c "test -f {} && echo cp {} /my/destination/dir "

This last one actually takes care to only remove files, and not hardlinks, sockets, directories, etc. Remove the "echo" to actually execute the copy.

Finally, there's always find:
Code:
 find  . -maxdepth 1 -type f \! \( -name foo -o -name bar -o -name foo*bar \) -exec echo cp "{}" /my/destination/dir ";"

Once again, this only touches files. You can make it completely recurse all directories by removing "maxdepth". Again, remove the "echo" to actually do it.

Last edited by otheus; 10-08-2008 at 12:21 PM.. Reason: oops you wanted to copy, not remove. put first example in code tags.
# 3  
Old 10-08-2008
instead of 2 greps, you could use both the patterns separated by a pipe character or a single egrep would do
# 4  
Old 10-09-2008
Thanks for the reply.

There's an error when:
cp !(foo|bar) /my/destination/dir
root@rskcs3 # cp !(abc.txt|bcd.txt) /tmp/test1
syntax error: `(' unexpected

I'm using ksh, seems like not working.Smilie

Shall try out the next following codes.

What is the ^bar$ --^ & $ for? I'm still quite new in scripting

Last edited by *Jess*; 10-09-2008 at 12:21 AM..
# 5  
Old 10-09-2008
Quote:
What is the ^bar$ --^ & $ for? I'm still quite new in scripting
^ - start of the pattern
$ - end of the pattern

^bar$ - starts with 'b' and ends with 'r' with in 'a' in between.
Literally, it should match the pattern 'bar'
# 6  
Old 10-09-2008
Thanks for the explanation. Really appreciate it~Smilie
# 7  
Old 11-01-2008
Quote:
Originally Posted by otheus
Several ways. In ksh and bash you can use
Code:
cp !(foo|bar) /my/destination/dir

to exclude files named "foo" and "bar".
I had this dilemma also..
This worked like a charm. It's easy to understand as well. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. SCO

How do you copy files from CD

How do you copy files fra a cd-rom to a directory on the computer? (2 Replies)
Discussion started by: Schnell
2 Replies

9. UNIX for Advanced & Expert Users

copy of files

hi all i have a script,which when executed must copy 3 files from a directory on boxA to the same directory on boxB.I'm using scp to copy these files,the problem is out ofthe 3 files only1 is been copied and not the other 2, i have permissons for the files,any ideas are appreciated thnks (2 Replies)
Discussion started by: bkan77
2 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
Login or Register to Ask a Question