How to Find Files, Print Modification Date, and Show Size in MB on Linux

When working on Linux, there are scenarios where you need to find specific files, print their modification dates, and display their sizes…

How to Find Files, Print Modification Date, and Show Size in MB on Linux

When working on Linux, there are scenarios where you need to find specific files, print their modification dates, and display their sizes in a readable format, such as megabytes (MB). This guide will walk you through the process step by step.

Step 1: Understanding the find Command

The find command is a powerful utility in Linux that allows you to search for files and directories based on various criteria, such as name, type, size, and more.

To find files with a specific extension, such as .IXF, use the following command:

find / -name '*.IXF' -type f
  • /: Specifies the root directory as the starting point for the search. You can replace / with a specific directory path.
  • -name '*.IXF': Searches for files with the .IXF extension.
  • -type f: Ensures that only files (not directories) are included in the results.

Step 2: Printing File Details with stat

The stat command provides detailed information about files, including their modification date and size. To use stat with find, you can combine the two commands using -exec:

find / -name '*.IXF' -type f -exec stat --format='%y|%s|%n' {} \;

Here:

-exec: Executes a command on each found file.

stat --format='%y|%s|%n':

  • %y: Prints the file’s modification date and time.
  • %s: Prints the file size in bytes.
  • %n: Prints the file name.

{} \;: Placeholder ({}) for each found file and terminator (\;) for the -exec command.

This command outputs each file’s modification date, size (in bytes), and name separated by a |.

Step 3: Converting File Size to MB

To make the file size more readable, we can convert it from bytes to MB. This can be done using awk, a text processing tool. Here’s the updated command:

find / -name '*.IXF' -type f -exec stat --format='%y|%s|%n' {} \; | awk -F'|' '{printf "%s %.2fMB %s\n", $1, $2 / 1024 / 1024, $3}'
  • -F'|': Specifies | as the field separator in the output from stat.
  • $1: Refers to the first field (modification date and time).
  • $2 / 1024 / 1024: Converts the file size (second field) from bytes to megabytes (1 MB = 1024 * 1024 bytes).
  • $3: Refers to the third field (file name).
  • %.2fMB: Formats the size to two decimal places, followed by MB.

Step 4: Understanding the Output

When you run the above command, the output will look like this:

2025-01-01 12:34:56 12.34MB /path/to/file1.IXF 
2024-12-31 20:25:56 5.67MB /path/to/file2.IXF

Each line contains:

  1. The modification date and time of the file.
  2. The file size in MB, formatted to two decimal places.
  3. The full path to the file.

Step 5: Putting It All Together

Here is the complete command:

find / -name '*.IXF' -type f -exec stat --format='%y|%s|%n' {} \; | awk -F'|' '{printf "%s %.2fMB %s\n", $1, $2 / 1024 / 1024, $3}'

This command:

  1. Searches for all .IXF files starting from the root directory.
  2. Prints their modification date, size (converted to MB), and path.

Step 6: Running the Command Safely

Since searching from the root directory (/) can be resource-intensive and may encounter permission issues, you can limit the search to a specific directory or use sudo for elevated permissions:

sudo find /specific/directory -name '*.IXF' -type f -exec stat --format='%y|%s|%n' {} \; | awk -F'|' '{printf "%s %.2fMB %s\n", $1, $2 / 1024 / 1024, $3}'

Replace /specific/directory with the path to the directory you want to search.

Conclusion

Using the find command combined with stat and awk, you can efficiently locate files, print their modification dates, and display their sizes in a user-friendly format. This approach is flexible and can be tailored to meet various file search and reporting needs.