0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Archive Old Files Easily

Use find /path/to/files -type f -mtime +N -print0 | tar -czvf archive.tar.gz --null -T - to archive files older than N days into a compressed tar file.
📋

Examples

InputArchive files older than 7 days in /tmp/logs
OutputCreates archive.tar.gz containing all files in /tmp/logs older than 7 days
InputArchive files older than 30 days in /home/user/docs
OutputCreates archive.tar.gz with files from /home/user/docs older than 30 days
InputArchive files older than 0 days in /var/tmp
OutputArchives all files in /var/tmp (all files older than 0 days means all files)
🧠

How to Think About It

To archive old files, first identify files older than a certain number of days using find with -mtime. Then, pass these files safely to tar to create a compressed archive. This avoids errors with spaces or special characters in filenames.
📐

Algorithm

1
Get the directory path and number of days as input
2
Use <code>find</code> to list files older than the given days
3
Pass the list of files to <code>tar</code> to create a compressed archive
4
Print a message confirming the archive creation
💻

Code

bash
#!/bin/bash

DIR="$1"
DAYS="$2"
ARCHIVE="archive.tar.gz"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

find "$DIR" -type f -mtime +$DAYS -print0 | tar -czvf "$ARCHIVE" --null -T -

echo "Archived files older than $DAYS days from $DIR into $ARCHIVE"
Output
archive.tar.gz Archived files older than 7 days from /tmp/logs into archive.tar.gz
🔍

Dry Run

Let's trace archiving files older than 7 days in /tmp/logs through the code

1

Check inputs

DIR=/tmp/logs, DAYS=7

2

Find old files

find /tmp/logs -type f -mtime +7 -print0 lists files older than 7 days

3

Create archive

tar reads file list from find and creates archive.tar.gz

StepCommandResult
1Check inputsDIR=/tmp/logs, DAYS=7
2find /tmp/logs -type f -mtime +7 -print0List of old files with null separator
3tar -czvf archive.tar.gz --null -T -archive.tar.gz created with old files
💡

Why This Works

Step 1: Finding old files

The find command with -mtime +N selects files modified more than N days ago.

Step 2: Handling filenames safely

Using -print0 with find and --null -T - with tar safely handles filenames with spaces or special characters.

Step 3: Creating compressed archive

The tar -czvf command creates a gzip compressed archive including the selected files.

🔄

Alternative Approaches

Using zip instead of tar
bash
#!/bin/bash
DIR="$1"
DAYS="$2"
ARCHIVE="archive.zip"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

find "$DIR" -type f -mtime +$DAYS -print | zip "$ARCHIVE" -@
echo "Archived files older than $DAYS days from $DIR into $ARCHIVE"
Zip archives are more common on Windows but may be slower for many files.
Moving old files to archive folder
bash
#!/bin/bash
DIR="$1"
DAYS="$2"
ARCHIVE_DIR="$DIR/archive_$(date +%Y%m%d)"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

mkdir -p "$ARCHIVE_DIR"
find "$DIR" -type f -mtime +$DAYS -exec mv {} "$ARCHIVE_DIR" \;
echo "Moved files older than $DAYS days to $ARCHIVE_DIR"
This moves files instead of archiving, freeing space but losing original locations.

Complexity: O(n) time, O(1) space

Time Complexity

The script scans all files once with find, so time grows linearly with number of files.

Space Complexity

No extra memory is used proportional to input size; files are processed in stream.

Which Approach is Fastest?

Using tar with find is efficient; zip may be slower for many files, moving files is fast but changes file locations.

ApproachTimeSpaceBest For
find + tarO(n)O(1)Safe archiving with compression
find + zipO(n)O(1)Windows-friendly archives
find + moveO(n)O(1)Freeing space by relocating files
💡
Always test your script with a small directory and few days before running on important data.
⚠️
Forgetting to handle filenames with spaces causes errors; always use -print0 and --null options.