Command
find my_directory/ -maxdepth 1 -printf "%P\n" | tar -cvf my_archive.tar -C my_directory/ -T -
This creates a standard archive file. Standard in a way that files and dirs you want to pack are in the root of the archive. There are no extra special files as '.' and dirs as ./
before each './file'.
Trying so many solutions which are already here with only one successfully, seems to me that using find
or possibly other commands like ls -A -1
on the left side of the pipe (-1 as 'one', not -l as 'L') is the only way to achieve above goal.
If tar file is further processed, or delivered to someone as a product of my work, then I don't want to have some weirdness there.
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. – Durga Swaroop