To find and tar (create an archive) files into a tar ball (
.tar
file) in Linux, you can use the tar
command in combination with the find
command.Here’s the basic syntax:
$ find [directory] -name [pattern] -exec tar -czf [tarfile.tar.gz] {} +
[directory]
: The directory you want to search for files.[pattern]
: A shell pattern that matches the filenames you want to find. For example,*.txt
matches all text files.[tarfile.tar.gz]
: The name of the tar ball you want to create.
For example, to find all .txt
files in the current directory and its subdirectories and tar them into a tar ball named text_files.tar.gz
, you can run the following command:
find . -name "*.txt" -exec tar -czf text_files.tar.gz {} +
The -c
option tells tar
to create a new archive, the -z
option compresses the archive with gzip, and the -f
option specifies the name of the tar ball. The {}
and +
options tell find
to pass all matching files to the tar
command at once, rather than one file at a time.
You can also use other options with the tar
command, such as -v
to show verbose output, -r
to add files to an existing archive, or -x
to extract files from an archive. For more information, you can run man tar
in your terminal to see the manual pages for the tar
command.