Command
to create a standard archive file.
find my_directory/ -maxdepth 1 -printf "%P\n" | tar -cvf my_archive.tar -C my_directory/ -T -
Packed files and dirs are in the root of the archive without path info and deeper files have relative path.
There are no weird looking './' in front of files and dirs. ('./file')
No special files '.' are included.
It seems that another tool, like find
or ls
(ls -A -1
) is needed to accomplish these goals and tar
using just its arguments is unable to pick files and create an archive with such requirements.
Using above command creates an archive tar file which can be further processed or delivered to someone without looking weird or needing an explanation or a tool to unpack.
Arguments description
-maxdepth 1
Descend at most 1 level - No recursing.
-printf
print format on the standard output
%P
File's name with the name of the starting-point under which it was found removed.
\n
Newline
printf does not add a newline at the end of the string. It must be added here
tar:
-C DIR
, --directory=DIR
change to directory DIR
-T FILE
, --files-from=FILE
get names to extract or create from FILE
-
that FILE from above is the standard input, from the pipe
Comments on other solutions.
The same result might be achieved using solution described by @aross.
The difference with the solution here is in that which tool is doing the recursing. If you leave the job to find
, every filepath name, goes through the pipe. It also sends all directory names, which tar with --no-recursion ignores or adds as empty ones followed by all files in each directory. If there was unexpected output as errors in file read from find
, tar would not know or care what's going on.
But with further checks, like processing error stream from find, it might be a good solution where many options and filters on files are required.
I prefer to leave the recursing on tar, it does seem simpler and as such more stable solution.
With my complicated directory structure, I feel more confident the archive is complete when tar will not report an error.
Another solution using find
proposed by @serendrewpity seems to be fine, but it fails on filenames with spaces. Difference is that output from find
supplied by $() sub-shell is space-divided. It might be possible to add quotes using printf, but it would further complicate the statement.
There is no reason to cd into the my_directory and then back, while using ../my_archive.tar for tar path, because TAR has -C DIR
, --directory=DIR
command which is there just for this purpose.
Using .
(dot) will include dots
Using * will let shell supply the input file list. It might be possible using shell options to include dot files. But it's complicated. The command must be executed in shell which allows that. Enabling and disabling must be done before and after tar command. And it will fail if root dir of future archive contains too many files.
That last point also applies to all those solutions which are not using pipe.
Most of solutions are creating a dir inside which are the files and dirs. That is barely ever desired.
tar -czf
? In my case it's only storing the files and not the directory. When I justtar
the directory it includes it but withtar -czf
it is only adding the files.