Shell command to tar directory excluding certain files/folders


📝 Shell Command to Tar Directory, Excluding Certain Files/Folders
Are you looking for a simple shell command or script to exclude specific files or folders from being archived? You've come to the right place! In this blog post, we'll address this common issue and provide you with easy solutions to help you backup your directory without including unnecessary files or folders. Let's dive in! 💪
The Challenge
You have a directory that needs to be archived, but there is a subdirectory within it containing large files that you don't want to backup. You've attempted to use the tar --exclude=PATTERN
command, which excludes files based on a pattern match. However, in your case, you need to exclude specific files and folders by their full file path, as excluding based on patterns may inadvertently exclude valid files.
Another potential solution you've explored is using the find
command to generate a list of files and manually excluding the ones you don't want to include in the archive. However, this approach becomes impractical when dealing with tens of thousands of files. 🤔
A More Efficient Solution
After considering alternative approaches, you're beginning to think that creating a file with a list of files and folders to be excluded may be the best solution. Here's a step-by-step guide to help you achieve this using rsync
and tar
.
Create a file:
touch exclude.txt
Open the file using a text editor:
nano exclude.txt
Add the full file paths of the files and folders you want to exclude, each on a new line. For example:
/folder_to_backup/folder /folder_to_backup/upload/folder2
Save and close the file.
Use the following command to copy all the remaining files to a temporary directory using
rsync
with the--exclude-from
option:rsync -av --exclude-from=exclude.txt /folder_to_backup /tmp/backup
Finally, use
tar
to create the archive of the temporary directory:tar -zcvf /backup/filename.tgz -C /tmp/backup .
And that's it! You've successfully created an archive of your directory while excluding the specified files and folders. 🎉
Note: Ensure that the --exclude='./folder'
option is placed at the beginning of the tar
command for it to work correctly.
Your Turn! Engage with us! 📣
We hope this guide has helped you find an efficient solution to exclude specific files and folders while archiving your directory. Now it's your turn to engage with us!
👉 Do you have an alternative solution that you find simpler or more effective? Let us know in the comments below!
👉 Are you experiencing any difficulties with the provided steps? We're here to help! Drop your questions in the comments, and we'll get back to you with the answers you need.
👉 Have you faced any other challenges while working with shell commands or archiving? We'd love to hear about them! Feel free to share your experiences and insights.
Remember, your engagement helps us learn from each other and build a stronger tech community. Together, we can tackle any tech challenge! 💪💻
So go ahead and share your thoughts, questions, and solutions in the comments section. We can't wait to hear from you! 🚀
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
