tags:

views:

56

answers:

4

Hi,

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!

+2  A: 

you can use tar with --exclude PATTERN option. See the man page for more. To exclude files, you can see this page for examples.

ghostdog74
Thanks! Would you give some examples?
Tim
+2  A: 
unutbu
Thanks! Does it work for files coming from several source paths, and they have same names? Also can it maintain the directories inside the archive?
Tim
If you have multiple source paths, my first inclination would be to name /path/to/source/ as the parent directory of all the source paths. However, if the parent has some files named "filea" which you want, and others that you don't then obviously that is not going to give the desired result. In that case, yes, you could issue multiple `rsync` commands with different source paths, but all the same archive path.And yes, the archive directory maintains the directory structure.
unutbu
Simpler: find <whatever> cpio -pdl <new-directory>(usually you want the l to just create links).
reinierpost
+1  A: 

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.

Space
Thanks Octopus! What does "'{}' \" mean?
Tim
the breakets stores the output of find command and slash semicolon ends the find with exec. you can look at 'man find' for more details.
Space
+2  A: 

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