Linux Find Command: Using delete and prune to Resolve "Argument list too long" Error
In this article, we'll discuss how to use the Linux find command to scan a backup drive and remove files older than two years, while skipping a specific folder that contains older files needed. We'll also cover how to resolve the "Argument list too long" error that may arise during this process.
Background
The find command in Linux is a powerful tool that allows you to search for and perform actions on files and directories that match a set of criteria. However, when using the find command with the -delete option to delete files, you may encounter the "Argument list too long" error.
This error occurs when the number of files that match the search criteria exceeds the maximum limit of command-line arguments for the system. This limit is usually set by the system's maximum size for an environment variable, which is typically around 32,000 to 65,000 bytes. When this limit is exceeded, the system will return the "Argument list too long" error and the delete operation will fail.
Using the prune Option
To avoid the "Argument list too long" error, you can use the prune option in conjunction with the find and -delete options. This option allows you to exclude a directory and its subdirectories from the search, which can help reduce the number of files that match the search criteria.
find /backup -type d -name 'folder-to-skip' -prune -o -type f -mtime +730 -deleteIn this example, the /backup directory is being searched for files that are older than two years (-mtime +730). However, any directories (-type d) with the name 'folder-to-skip' will be excluded from the search (-prune). The -o option is used to specify the logical OR operation, so the search will continue even after the excluded directory is encountered.
Resolving the "Argument list too long" Error
If you still encounter the "Argument list too long" error after using the prune option, you can try using the xargs command in combination with the find and -delete options. This command allows you to build and execute command lines from standard input.
find /backup -type f -mtime +730 -print0 | xargs -0 rmIn this example, the find command is used to search for files (-type f) that are older than two years (-mtime +730), and the -print0 option is used to print the file names followed by a null character. The output of the find command is then passed to the xargs command, which executes the rm command to delete the files.
The find command in Linux is a powerful tool that allows you to search for and perform actions on files and directories. By using the prune and xargs options in combination with the find and -delete options, you can effectively scan a backup drive and remove files older than two years, while skipping a specific folder that contains older files needed. This can help you maintain the organization and cleanliness of your backup drive, while avoiding the "Argument list too long" error.
References
- Linux find command man page: https://man7.org/linux/man-pages/man1/find.1.html
- Linux xargs command man page: https://man7.org/linux/man-pages/man1/xargs.1.html
- Linux "Argument list too long" error: https://askubuntu.com/questions/112258/what-does-the-argument-list-is-too-long-mean