Commands for Bulk Deleting Files and Empty Files in Linux

In Linux, the command to delete files or directories is rm (remove).

Function Description: Deletes files or directories.

Syntax: rm [-dfirv][–help][–version][file or directory…]
Additional Notes: Executing the rm command can delete files or directories. To delete a directory, you must add the ‘-r’ parameter; otherwise, it will only delete files by default.

Parameters:

  • -d or –directory: Directly delete the hard link data of the directory to 0 and remove the directory.
  • -f or –force: Forcefully delete files or directories.
  • -i or –interactive: Ask the user before deleting existing files or directories.
  • -r or -R or –recursive: Recursively process, handling all files and subdirectories under the specified directory.
  • -v or –verbose: Display the execution process of the command.
  • –help: Online help.

Method to bulk delete empty files (files with size equal to 0) in Linux:

Code Example:

find . -name '*' -type f -size 0c | xargs -n 1 rm -f

This can also delete files of a specified size; just modify the corresponding -size parameter, for example:

Code Example:

find . -name '*' -type f -size 1024c | xargs -n 1 rm -f

This deletes files of size 1k. (But be careful not to use -size 1k, as this refers to 1k of disk space used, not a file size of 1k.)

If you only want to delete folders or name links, you can adjust the -type parameter accordingly; for specific details, see man find.

Deleting files older than N days:

In Linux, to bulk delete files based on time (delete files older than N days):

Code Example:

find /opt/Oracle/admin/CCXE/bdump/ -mtime +10 -name '*.*' -exec rm -Rf {} ;

/opt/Oracle/admin/CCXE/bdump/: The directory you want to clean.

-mtime: Standard syntax
+10: Find files older than 10 days; here, the number represents days; +30 indicates files older than 30 days.

‘*.*’: The type of data you want to find; ‘*.jpg’ means find all files with a jpg extension, ‘*’ means find all files.
-exec: Fixed syntax.

rm -rf: Forcefully delete files, including directories.
{} ;: Fixed syntax; a pair of curly braces + space + / + ;

Completely deleting files:

Sometimes, we want to completely delete certain files; we can use the shred command to achieve this. Shred is part of coreutils, so this command is generally available in Linux.

Method to completely delete files using shred:

Code Example:

$ shred -u file

Shred will overwrite the file’s inode and data blocks with random content and delete the file (-u parameter).

If you want to clear it more thoroughly, you can add the -z parameter, which means to first fill with random data, and finally fill with 0.

Code Example:

$ shred -u -z file

Additionally, shred can also clear an entire partition or disk; for example, to completely clear the contents of the /dev/sdb1 partition, you can do this:

$ shred /dev/sdb1 (Be careful not to add the -u parameter)

Detailed parameters for shred:

  • -f, –force: Change permissions to allow writing (if necessary).
  • -n, –iterations=N: Overwrite N times, default is 3 times.
  • –random-source=FILE: Read data from the specified file.
  • -s, –size=N: Shred to a fixed size (you can use suffixes like K, M, C, etc.).
  • -u, –remove: Overwrite, truncate, and remove the file.
  • -v, –verbose: Display progress.
  • -z, –zero: Add 0 to overwrite data.
  • ?help: Show help.
  • ?version: Show version information.

Leave a Comment