Shell command to tar directory excluding certain files/folders

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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.

  1. Create a file:

    touch exclude.txt
  2. Open the file using a text editor:

    nano exclude.txt
  3. 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
  4. Save and close the file.

  5. 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
  6. 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my