filmov
tv
How can I recursively copy files by file extension, preserving directory structure? (7 Solutions!!)
Показать описание
How can I recursively copy files by file extension, preserving directory structure?
The Question: At the Linux command line, I'd like to copy a (very large) set of .txt files
from one directory (and its subdirectories) to another.
I need the directory structure to stay intact, and I need to ignore files
except those ending in .txt.
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 1 person ==
how about you first copy it over with
cp -r /old/folder /new/folder
then go to the new folder and run
find . -type f ! -iname "*.txt" -delete
or just
cp -r /old/folder /new/folder && find . -type f ! -iname "*.txt" -delete
Edit: ok you want one command which filters (I have not tested this because my
system doesn't have the cpio command!). Here is where I found it: http://
Files
find . -name "*.txt" -print0 |
cpio -pmd0 /dest-dir
Please test this first, because I haven't tried it yet. If someone would
verify, that would be great.
== This solution helped 99 people ==
cd /top/level/to/copy
find . -name '*.txt' | cpio -pdm /path/to/destdir
(-updm for overwrite destination content.)
== This solution helped 8 people ==
cd /source/path
find -type f -name *.txt -exec install -D {} /dest/path/{} ;
== This solution helped 1 person ==
Easiest way that worked for me:
cp --parents -R jobs/**/*.xml ./backup/
one catch is you have to navigate to the "desired" directory before so the
"parent path" is correct.
Also make sure that you enabled recursive globs in bash:
shopt -s globstar
== This solution helped 3 people ==
Another approach
find . -name '*.txt' -exec rsync -R {} path/to/dext ;
Navigate to directory:
find . -regex '<regexp_to_get_directories_and_files_you_want>' | xargs -i cp -
r --parents {} path/to/destination
It s a bit more straight forward and mighty, if you manage regular expressions.
The Question: At the Linux command line, I'd like to copy a (very large) set of .txt files
from one directory (and its subdirectories) to another.
I need the directory structure to stay intact, and I need to ignore files
except those ending in .txt.
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 1 person ==
how about you first copy it over with
cp -r /old/folder /new/folder
then go to the new folder and run
find . -type f ! -iname "*.txt" -delete
or just
cp -r /old/folder /new/folder && find . -type f ! -iname "*.txt" -delete
Edit: ok you want one command which filters (I have not tested this because my
system doesn't have the cpio command!). Here is where I found it: http://
Files
find . -name "*.txt" -print0 |
cpio -pmd0 /dest-dir
Please test this first, because I haven't tried it yet. If someone would
verify, that would be great.
== This solution helped 99 people ==
cd /top/level/to/copy
find . -name '*.txt' | cpio -pdm /path/to/destdir
(-updm for overwrite destination content.)
== This solution helped 8 people ==
cd /source/path
find -type f -name *.txt -exec install -D {} /dest/path/{} ;
== This solution helped 1 person ==
Easiest way that worked for me:
cp --parents -R jobs/**/*.xml ./backup/
one catch is you have to navigate to the "desired" directory before so the
"parent path" is correct.
Also make sure that you enabled recursive globs in bash:
shopt -s globstar
== This solution helped 3 people ==
Another approach
find . -name '*.txt' -exec rsync -R {} path/to/dext ;
Navigate to directory:
find . -regex '<regexp_to_get_directories_and_files_you_want>' | xargs -i cp -
r --parents {} path/to/destination
It s a bit more straight forward and mighty, if you manage regular expressions.