Reputation: 99418
I have some files with same names but under different directories. For example, path1/filea, path1/fileb, path2/filea, path2/fileb,....
What is the best way to make the files into an archive? Under these directories, there are many other files under these directories that I don't want to make into the archive. Off the top of my head, I think of using Bash, probably ar, tar and other commands, but am not sure how exactly to do it.
Renaming the files seems to make the file names a little complicated. I tend to keep the directory structure inside the archive. Or I might be wrong. Other ideas are welcome!
Thanks and regards!
EDIT:
Examples would be really nice!
Upvotes: 0
Views: 322
Reputation: 879471
To recursively copy only files with filename "filea" or "fileb" from /path/to/source to /path/to/archive, you could use:
rsync -avm --include='file[ab]' -f 'hide,! */' /path/to/source/ /path/to/archive/
'*/'
is a pattern which matches 'any directory'
'! */'
matches anything which is not a directory (i.e. a file)
'hide,! */'
means hide all files
--include='file[ab]'
has precedence, so if a file matches 'file[ab]'
, it is included.
Any other file gets excluded from the list of files to transfer.
find...exec
pattern:
mkdir /path/to/archive
cd /path/to/source
find . -type f -iname "file[ab]" -exec cp --parents '{}' /path/to/archive ";"
Upvotes: 2
Reputation: 21
You may give the find command multiple directories to search through.
# example: create archive of .tex files
find -x LaTeX-files1 LaTeX-files2 -name "*.tex" -print0 | tar --null --no-recursion -uf LaTeXfiles.tar --files-from -
Upvotes: 2
Reputation: 7259
What I have used to make a tar ball for the files with same name in different directories is
$find <path> -name <filename> -exec tar -rvf data.tar '{}' \;
i.e. tar [-]r --append
Hope this helps.
Upvotes: 1
Reputation: 342363
you can use tar
with --exclude PATTERN
option. See the man page for more.
To exclude files, you can see this page for examples.
Upvotes: 2