Sponsored Content
Top Forums Shell Programming and Scripting How to copy a directory without specific files? Post 302910791 by bakunin on Monday 28th of July 2014 07:28:58 AM
Old 07-28-2014
Quote:
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.

Code:
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:

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

bakunin
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
cp(1)							      General Commands Manual							     cp(1)

NAME
cp - copy files and directory subtrees SYNOPSIS
extarg] file1 new_file extarg] file1 [file2 ...] dest_directory extarg] directory1 [directory2 ...] dest_directory extarg] file1|directory1 [file2|directory2 ...] dest_directory DESCRIPTION
copies: o file1 to new or existing new_file, o file1 to existing dest_directory, o file1, file2, ... to existing dest_directory, o directory subtree directory1, to new or existing dest_directory. or o multiple directory subtrees directory1, directory2, ... to new or existing dest_directory. fails if file1 and new_file are the same (be cautious when using shell metacharacters). When destination is a directory, one or more files are copied into that directory. If two or more files are copied, the destination must be a directory. When copying a single file to a new file, if new_file exists, its contents are destroyed. If the access permissions of the destination dest_directory or existing destination file new_file forbid writing, aborts and produces an error message "cannot create file". To copy one or more directory subtrees to another directory, the option is required. The option is ignored if used when copying a file to another file or files to a directory. If new_file is a link to an existing file with other links, overwrites the existing file and retains all links. If copying a file to an existing file, does not change existing file access permission bits, owner, or group. When copying files to a directory or to a new file that does not already exist, creates a new file with the same file permission bits as file1, modified by the file creation mask of the user if the option was not specified, and then bitwise inclusively ORed with S_IRWXU. The owner and group of the new file or files are those of the user. The last modification time of new_file (and last access time, if new_file did not exist) and the last access time of the source file1 are set to the time the copy was made. Options (interactive copy) Causes to write a prompt to standard error and wait for a response before copying a file that would overwrite an existing file. If the response from the standard input is affirmative, the file is copied if permissions allow the copy. (force copy) Forces existing destination pathnames to be removed before copying, without prompting for confirmation. This option has the effect of destroying and replacing any existing file whose name and direc- tory location conflicts with the name and location of the new file created by the copy operation. When the flag is set and if the destination file exists and the user has write permission on the destination file, then the desti- nation file is not removed. (preserve permissions) Causes to preserve in the copy as many of the modification time, access time, file mode, user ID, and group ID as allowed by permissions. (recursive subtree copy) Causes to copy the subtree rooted at each source directory to dest_directory. If dest_directory exists, it must be a directory, in which case creates a directory within dest_directory with the same name as file1 and copies the subtree rooted at file1 to dest_direc- tory/file1. An error occurs if dest_directory/file1 already exists. If dest_directory does not exist, creates it and copies the subtree rooted at file1 to dest_directory. Note that cannot merge subtrees. Usually normal files and directories are copied. Character special devices, block special devices, network special files, named pipes, symbolic links, and sockets are copied, if the user has access to the file; otherwise, a warning is printed stating that the file cannot be created, and the file is skipped. dest_directory should not reside within directory1, nor should directory1 have a cyclic directory structure, since in both cases attempts to copy an infinite amount of data. Under the UNIX Standard environment (see standards(5)), will exit with error if multiple sources are being copied to a non-existing directory. (recursive subtree copy) The option is identical to the option. With the and options, in addition to regular files and directories, also copies FIFOs, character and block device files and sym- bolic links. Only superusers can copy device files. All other users get an error. Symbolic links are copied so the target points to the same location that the source did. Warning: While copying a directory tree that has device special files, use the option; otherwise, an infinite amount of data is read from the device special file and is duplicated as a special file in the destination directory occupying large file system space. Causes to traverse the symbolic link specified as an operand and copy it to the destination. Causes to traverse the symbolic link specified as an operand or any symbolic links encountered during traversal of a file hierarchy, and copy it to the destination. Causes to copy the symbolic link specified as an operand and any symbolic links encountered during traversal of a file hierarchy. This option will not follow any symbolic links. Specifies the handling of any extent attributes of the file[s] to be copied. extarg takes one of the following values. Issues a warning message if extent attributes cannot be copied, but copies the file anyway. Does not copy the extent attributes. Fails to copy the file if the extent attribute can not be copied. Extent attributes can not be copied if the files are being copied to a file system which does not support extent attributes or if that file system has a different block size than the original. If is not specified, the default value for extarg is Specifies "safe" mode. This option minimizes the impact of on the performance of the system by opening the target file using the flag. This is recommended when the total size of the copy (not of a single file) is a significant portion of the size of system buffer cache. Usage of this option increases copy time. Specifying more than one of the mutually-exclusive options and will not be considered an error. The last option specified will determine the behavior of the utility. Access Control Lists (ACLs) If new_file is a new file, or if a new file is created in dest_directory, it inherits the access control list of the original file1, file2, etc., altered to reflect any difference in ownership between the two files (see acl(5) and aclv(5)). In JFS file systems, new files cre- ated by do not inherit their parent directory's default ACL entries (if any), but instead retain the ACLs of the files being copied. When copying files from a JFS file system to an HFS file system or vice versa, optional ACL entries are lost. EXTERNAL INFLUENCES
For information about the UNIX standard environment, see standards(5). Environment Variables determines the interpretation of text as single and/or multi-byte characters. and determine the local language equivalent of (for yes/no queries). determines the language in which messages are displayed. If is not specified in the environment or is set to the empty string, the value of is used as a default for each unspecified or empty vari- able. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multi-byte character code sets are supported. EXAMPLES
The following command moves the directory sourcedir and its contents to a new location (targetdir) in the file system. Since creates the new directory, the destination directory targetdir should not already exist. The option copies the subtree (files and subdirectories) in directory sourcedir to directory targetdir. The double ampersand causes a con- ditional action. If the operation on the left side of the is successful, the right side is executed (and removes the old directory). If the operation on the left of the is not successful, the old directory is not removed. This example is equivalent to: To copy all files and directory subtrees in the current directory to an existing targetdir, use: To copy all files and directory subtrees in sourcedir to targetdir, use: Note that directory pathnames can precede both sourcedir and targetdir. To create a zero-length file, use any of the following: DEPENDENCIES
NFS Access control lists of networked files are summarized (as returned in by but not copied to the new file. When using or on such files, a is not printed after the mode value when asking for permission to overwrite a file. AUTHOR
was developed by AT&T, the University of California, Berkeley, and HP. SEE ALSO
cpio(1), ln(1), mv(1), rm(1), link(1M), lstat(2), readlink(2), stat(2), symlink(2), symlink(4), acl(5), aclv(5), standards(5). STANDARDS CONFORMANCE
cp(1)
All times are GMT -4. The time now is 01:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy