How to Compress Directory Excluding Large Files Ubuntu
This article provides a straightforward method for compressing a directory in Ubuntu while excluding files that exceed a specific size threshold. You will learn how to combine the find and tar commands to generate a compressed archive containing only smaller files, saving space and processing time.
To achieve this, you need to use the command line terminal. The
process involves using the find utility to locate files
below your size limit and piping that list directly into the
tar command for compression. This method ensures that large
logs, videos, or database dumps are left out of the final archive.
Open your terminal and navigate to the parent directory of the folder you wish to compress. Use the following command structure, adjusting the path and size as needed:
find /path/to/directory -type f -size -100M -print0 | tar -czvf backup.tar.gz --null -T -
In this command, /path/to/directory is the folder you
want to archive. The -size -100M flag tells the system to
select files smaller than 100 Megabytes. You can change
100M to 50M, 1G, or any other
value depending on your requirements. The -print0 option
ensures file names with spaces are handled correctly.
The second part of the command uses tar with the
-czvf flags to create a gzipped archive named
backup.tar.gz. The --null -T - arguments
instruct tar to read the file list from the previous command safely.
Once the command finishes executing, you will have a compressed file
containing only the data that met your size criteria.
You can verify the contents of the new archive by listing the files
inside it without extracting them. Run
tar -tzvf backup.tar.gz to see the included files. This
confirms that any file larger than your specified limit has been
successfully excluded from the compression process.