As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:
find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar {} +
Iocnarz's answer of using tar
's --null
and -T
options works as well. If you have cpio
installed, camh's answer using it is also fine. And if you have zsh
and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)
) seems the most straightforward.
The key was the -maxdepth
option.
Final answer, dealing with spaces appropriately:
find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar
This should also work:
find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar {} +
If your pax
supports the -0
option, with zsh
:
print -rN dir/**/*(D/e:'reply=($REPLY/*(ND^/[1,10]))':) |
pax -w0 | xz > file.tar.xz
It includes the first 10 non-directory files of each directory in the list sorted by file name. You can choose a different sorting order by adding the om
glob qualifier (order by modification time, Om
to reverse the order), oL
(order by length), non
(sort by name but numerically)...
If you don't have the standard pax
command, or it doesn't support -0
but you have the GNU tar
command, you can do:
print -rN -- dir/**/*(D/e:'reply=($REPLY/*(ND^/[1,10]))':) |
tar --null -T - -cjf file.tar.xz
If you can't use zsh
, but have access to bash
(the shell of the GNU project), you could do:
find dir -type d -exec bash -O nullglob -O dotglob -c '
for dir do
set -- "$dir/*"; n=0
for file do
if [ ! -d "$file" ] || [ -L "$file" ]; then
printf "%s\0" "$file"
(( n++ < 10 )) || break
fi
done
done' bash {} + | pax -0w | xz > file.tar.xz
That would be significantly less efficient though.
Best Answer
Have a look first at
git help archive
.archive
is a git command that allows to make archives containing only git tracked files. Probably what you are looking for. One example listed at the end of the man page: