Commands.page Logo

Move Files Older Than Date to Archive Folder Ubuntu

Managing disk space on Ubuntu often requires organizing or archiving old data. This guide explains how to automatically identify and move files older than a specific date into a designated archive folder using the command line. You will learn the essential find command syntax and how to execute the move operation safely to keep your system organized without deleting important information.

Create the Archive Directory

Before moving files, you must create the destination folder. Open your terminal and run the following command to create a directory named “archive” in your home folder. The -p flag ensures no error occurs if the folder already exists.

mkdir -p ~/archive

Identify Files Older Than a Specific Date

The find command is the standard tool for locating files based on modification time. The -mtime flag measures time in 24-hour periods. To list all files in your Documents folder modified more than 30 days ago, use the following command. The +30 indicates files older than 30 days.

find ~/Documents -type f -mtime +30

Review the output list carefully to ensure only the intended files are selected.

Execute the Move Command

Once you have verified the file list, you can perform the move operation. Add the -exec flag to the find command to execute the mv command on each found file. Replace the source path with your actual directory.

find ~/Documents -type f -mtime +30 -exec mv {} ~/archive/ \;

In this command, {} represents the found file, and \; marks the end of the command execution.

Verify the Transfer

After the command completes, check the archive folder to confirm the files were moved successfully. You can list the contents of the archive directory with the following command.

ls -lh ~/archive

Automate the Process

To maintain this organization automatically, you can schedule the command using cron. Open your crontab editor by typing crontab -e and add a line to run the find command monthly. This ensures old files are archived regularly without manual intervention.