Delete files on the Linux Operating System is very easy, with the command rm (remove) your file can be deleted quickly. However, what if we want to delete files with certain conditions, for example, the file size is above x MB. Or I want to delete files over the age x days. Here is how to delete files with certain conditions.
The commands below apply to all types of Linux, including Raspberry Pi.
Delete files with the rm command
The rm command is a standard command for deleting files. Here is how to delete files with the rm command.
Delete single file
Use the following command to delete a single file. Suppose you want to delete the file myfile.txt.
rm myfile.txt
Alternatively, if you want to delete files with specific directories such as /home/teknotut/aku.txt. Use the following command.
rm /home/teknotut/myfile.txt
Delete without Confirmation
The previous command will delete the file with a confirmation. You will be asked if you really want to delete the file. If you are sure, you can press the Y key. Sometimes we want to delete the file without being asked. Use the following command.
rm myfile.txt -f
The -f option is a force, meaning it doesn't need to be asked, immediately delete it.
Delete Directory
To delete a directory, you must use the -r option which means recursive. Suppose you want to delete my data folder and all its contents, use the following command.
rm -rf mydata
If you use the folder -f (force) option and its contents, it is immediately deleted without confirmation.
Delete multiple files
To delete several files, you only need to separate the file names with spaces. Suppose you want to delete the file aku.txt, kamu.txt in the current directory, use the following command.
rm -f me.txt you.txt
If you are unsure, you can remove the -f (force) option.
Delete file with Wildcard
Deleting certain files can use wildcards, which are represented by a * (Asterisk). Suppose we want to delete all files with the extension .txt, use the following command;
rm -f *.txt
If you want to delete files with data prefix, for example in one directory, you have a file with the name dataaku.txt, your datakamu.xlsx, and you want to delete all files that start with data. Use the following command.
rm -f data*
If you want to delete all files in the current directory, use the following command.
rm -f *
Be careful when using the command above. Your files in the selected directory will be deleted.
Delete old file
To delete certain old files, for example, you want to delete files that were one year old in the /home/teknotut/dataku folder. Use the following command.
find /home/teknotut/dataku/* -mtime +365 -delete
The above command will search for files with the age of 365 days and before, after being found, then deleted.
Change -mtime with the age of the file you want (in days).
Delete files of a certain size
To delete files of the specified size, for example, you want to delete files above 1GB in the current directory, use the following command.
find . -type f -name "*" -size +1G -delete
Sign . (dot) Indicates the current directory, if you want to delete it in another directory, you only have to change the sign with the desired directory path.
The -name option is a search by file name. An asterisk indicates a wildcard, which means all files. If you want to delete files with certain extensions, for example, you want to delete files with Extensions .mp3, .tar, and .gz with file sizes above 4MB, use the following command.
find -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -size +4M -delete
Alternatively, you can exclude files. For example, delete all files above 5MB that are not .mp3 and .mp4, you can use the following command.
find . -type f ! -name '*.mp3' ! -name '*.mp4' -size +5M -delete
The -size option is the file size criterion, the + sign to indicate above, and the - sign will indicate below. To -size +1G means you will search for files with a size above 1GB. If you type size -1G, then you search for files under 1GB.
The -delete option will execute the deletion when the criteria file that we specify is found.
I hope this helps :).