How to copy a directory without specific files?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy a directory without specific files?
# 1  
Old 07-27-2014
How to copy a directory without specific files?


I need to copy a huge directory with thousands of files onto another directory but without *.WMV files (and without *.wmv - perhaps we need to use *.[wW][mM][vV]).

Pls advise how can I do that.

# 2  
Old 07-27-2014
Originally Posted by reddyr
I need to copy a huge directory with thousands of files onto another directory but without *.WMV files (and without *.wmv - perhaps we need to use *.[wW][mM][vV])
Use "find":

cd /path/to/sourcedir
find . ! -name "*.[wW][mM][vV]" -exec cp {} /path/to/targetdir \;

Note that you have to change into the directory first, because "find" will find ./file, which can used in the cp-command directly, while with an absolute path you'd get the absolute path /path/to/sourcedir/file which you'd need to modify first.

I hope this helps.

This User Gave Thanks to bakunin For This Post:
# 3  
Old 07-27-2014
Should you use a recent bash with extended pattern matching, you could try:
shopt -s extglob
ls !(*.wmv|*.WMV)

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 07-27-2014
Thanks bakunin and RudiC for your replies.

Your solutions are correct as per the requirement i mentioned in my post. My bad, I missed to mention that the directory has sub-directories (and further sub-directories) too which have *.wmv files. The solution you offered working only for non-recursive directories. I tried cp -r with find but it copies all wmv as its parent directory doesn't wmv.

Please suggest a solution for the scenario where I have *.wmv files in sub-directories (any depth) too. Thanks a lot.
# 5  
Old 07-27-2014
Unless there is something else you haven't told us, bakunin's script does exactly what you have requested: find all files in or under a source directory with names not ending in *.wmv or *.WMV and copy them into a single destination directory. (I would have used + instead of \; to terminate the -exec to make it run much faster when copying thousands of files, but that only affects speed; not which files will be copied.)

Oops, I take it back, he missed a key point. His script copied directories as well as regular files. And, you can't use -exec ... + when {} is not the last argument. Try:
cd /path/to/sourcedir
find . -type f ! -name "*.[wW][mM][vV]" -exec cp {} /path/to/targetdir \;

Last edited by Don Cragun; 07-27-2014 at 09:25 PM.. Reason: Missed that bakunin's script was copying subdirectories... and + won't work in this case.
# 6  
Old 07-27-2014
You should also have a look at some of the options available to the cp command in the manual.

You will need to make some decisions on what you want to do with any file links, file/directory permissions/ACLs, date/time stamps owners and groups for directories and files in the tree.

gnutar has quite a sensible set of defaults for all this stuff and the following has a good chance of doing what you want:

cd /path/to/sourcedir
tar -c -f - --ignore-case --exclude=*.wmv . | (cd /path/to/targetdir ; tar xf - )

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 07-28-2014
Originally Posted by Don Cragun
Oops, I take it back, he missed a key point. His script copied directories as well as regular files.
True - my bad. My only excuse is one shouldn't answer threads before the second coffee.

cd /path/to/sourcedir
find . -type f ! -name "*.[wW][mM][vV]" -exec cp {} /path/to/targetdir \;

This will probably not work either, because now the directory hierarchy will not become copied and some files (the one in sub-subdirectories) will have no proper targets. I suggest to do it in a two-pass way:

cd /path/to/sourcedir
find . -type d  -exec mkdir -p /path/to/targetdir{} \;
find . -type f ! -name "*.[wW][mM][vV]" -exec cp {} /path/to/targetdir +

The first pass creates a copy of the directory structure, the second copies all the files. If you need to copy filemodes, ownerships, etc. too, you need to modify the "cp"-command accordingly: "cp -p ...".

@Chubler_XL: using "tar" for that was my first impulse, but it would limit the solution to a system where the GNU-tar is available.

I hope this helps.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy Specific Files Recursively

Is it possible to only copy selected files+its directories when you are copying recursively? find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print From the above line, I want to only copy *txt and *ini files from /OriginalFolder/* ... (4 Replies)
Discussion started by: apacheLinux
4 Replies

2. Shell Programming and Scripting

Rsync to copy specific subfolders and files to new directory

RootFolderI: RootFolderI/FolderA/Subfolder1/Subsub1/JPG1.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub1/JPG2.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub2/JPG3.jpg . . . RootFolderI/FolderB/Subfolder1/Subsub1/JPG4.jpg -> want this jpg ... (1 Reply)
Discussion started by: blocnt
1 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

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

6. Shell Programming and Scripting

copy specific files and count them - not as easy as it seems!

Hi all: Here's my dilemma: to identify files of a specific type, copy them to a new location while preserving the original file attributes (date, time, full path, etc), and at the same time capture the count of the number of files identified as a variable for later reporting. Here's where I... (9 Replies)
Discussion started by: Yamaha Evans
9 Replies

7. Shell Programming and Scripting

How to copy specific files when you don't know the file name?

I hope this isn't as silly as it sounds from the title of the thread. I have software that outputs files where the name starts with a real number followed by underscore as a prefix to an input file name. These will list in the directory with the file with the smallest real number prefix as the... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

8. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies

10. Solaris

To copy the files newer than specific date

Dear all, Can you help me in copying files newer than speciifc date Thanks in advance, Rajesh (3 Replies)
Discussion started by: RAJESHKANNA
3 Replies
Login or Register to Ask a Question

Featured Tech Videos