Recursive Copy of a Specific File Type Maintaining the File Structure in Unix/Linux

Recursive copy of a specific file type maintaining the file structure in Unix/Linux?

rsync is useful for local file copying as well as between machines. This will do what you want:

rsync -avm --include='*.jar' -f 'hide,! */' . /destination_dir

The entire directory structure from . is copied to /destination_dir, but only the .jar files are copied. The -a ensures all permissions and times on files are unchanged. The -m will omit empty directories. -v is for verbose output.

For a dry run add a -n, it will tell you what it would do but not actually copy anything.

Recursively move files of certain type and keep their directory structure

It depends slightly on your O/S and, more particularly, on the facilities in your version of tar and whether you have the command cpio. It also depends a bit on whether you have newlines (in particular) in your file names; most people don't.

Option #1

cd /old-dir
find . -name '*.mov' -print | cpio -pvdumB /new-dir

Option #2

find . -name '*.mov' -print | tar -c -f - -T - |
(cd /new-dir; tar -xf -)

The cpio command has a pass-through (copy) mode which does exactly what you want given a list of file names, one per line, on its standard input.

Some versions of the tar command have an option to read the list of file names, one per line, from standard input; on MacOS X, that option is -T - (where the lone - means 'standard input'). For the first tar command, the option -f - means (in the context of writing an archive with -c, write to standard output); in the second tar command, the -x option means that the -f - means 'read from standard input'.

There may be other options; look at the manual page or help output of tar rather carefully.

This process copies the files rather than moving them. The second half of the operation would be:

find . -name '*.mov' -exec rm -f {} +

Bash: Copy named files recursively, preserving folder structure

Have you tried using the --parents option? I don't know if OS X supports that, but that works on Linux.

cp --parents src/prog.js images/icon.jpg /tmp/package

If that doesn't work on OS X, try

rsync -R src/prog.js images/icon.jpg /tmp/package

as aif suggested.

Copy all files with a certain extension from all subdirectories

--parents is copying the directory structure, so you should get rid of that.

The way you've written this, the find executes, and the output is put onto the command line such that cp can't distinguish between the spaces separating the filenames, and the spaces within the filename. It's better to do something like

$ find . -name \*.xls -exec cp {} newDir \;

in which cp is executed for each filename that find finds, and passed the filename correctly. Here's more info on this technique.

Instead of all the above, you could use zsh and simply type

$ cp **/*.xls target_directory

zsh can expand wildcards to include subdirectories and makes this sort of thing very easy.

UNIX How to copy entire directory (subdirectories and files) from one location to another and retain permissions

from man cp

-p    Cause cp to preserve the following attributes of each source file
in the copy: modification time, access time, file flags, file mode,
ACL, user ID, and group ID, as allowed by permissions.

so cp -pr

Copy every file of entire directory structure into base path of another

you are looking for ways to flatten the directory

find /images -iname '*.jpg' -exec cp --target-directory /newfolder/ {} \;

find all files iname in case insensitive name mode.

cp copy once to --target-directory named /newfolder/.

{} expand the list from find into the form of /dir/file.jpg /dir/dir2/bla.jpg.

How to `scp` directory preserving structure but only pick certain files?

rsync with and -exclude/include list follwing @Adrian Frühwirth's suggestion would be a to do this.

Unix shell file copy flattening folder structure

In bash:

find /foo -iname '*.txt' -exec cp \{\} /dest/ \;

find will find all the files under the path /foo matching the wildcard *.txt, case insensitively (That's what -iname means). For each file, find will execute cp {} /dest/, with the found file in place of {}.



Related Topics



Leave a reply



Submit