Delete Old Files on Linux - Command Line Guide

Automated File Deletion on Linux Systems
The find utility within Linux provides a powerful mechanism for locating files based on various criteria. A particularly useful feature is its ability to execute commands on each file discovered during a search.
This capability can be leveraged to identify files exceeding a specified age and subsequently remove them using the rm command.
Command Structure
The fundamental syntax for achieving this is as follows:
find /path/to/files* -mtime +5 -exec rm {} \;
It's crucial to observe the spacing between the rm command, the placeholder {}, and the terminating semicolon \;.
Understanding the Components
- The initial argument defines the target path for the file search. This can represent a specific directory, a file path, or utilize wildcards, as demonstrated in the example. It is highly recommended to initially execute the command *without* the -exec rm portion to verify the accuracy of the file selection.
- The -mtime argument specifies the age threshold for file identification. A value of +5, for instance, will locate files that have not been modified in more than 5 days.
- The -exec argument enables the execution of a command, such as rm, on each identified file. The {} \; sequence is essential for correctly terminating the command.
This method is broadly compatible across numerous Linux distributions, including Ubuntu, Suse, and Redhat, among others.
Regularly removing old files can help conserve disk space and maintain system efficiency.
Carefully consider the implications before executing commands that delete files, and always test thoroughly in a non-production environment first.
The find command offers a flexible and efficient solution for automated file management on Linux systems.
Related Linux Commands
Files |
||
Files |
tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr |
|
Processes |
alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap |
|
Networking |
netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld |
RELATED: Best Linux Laptops for Developers and Enthusiasts