Linux rename file with Dates in Batches

Introduction

In this tutorial, we’ll explore the process of batch Linux rename file with the inclusion of dates. You can use the rename command or a combination of find commands and mv commands to batch rename files in a directory with new filenames containing dates in Linux.

Linux rename file with Dates

Today I want to share how to batch rename files in a directory to new filenames with dates in Linux only with one command

For example, in my directory, I have files named like this:

  • test123.txt
  • test456.txt
  • test789.txt
Linux rename file with Dates in Batches

The result after running the command looks like this:

20230805” is the execution date.

  • test123_20230805.txt
  • test456_20230805.txt
  • test789_20230805.txt

Command to batch rename files in a directory to new filenames with dates

find -name "test[0-9][0-9][0-9].txt" -type f -exec sh -c 'mv -f {} "$(dirname {})/$(basename {} .txt)_$(date +%Y%m%d).txt"' \;

Explain the command for Linux rename file

  1. find: This command is used to search for files and directories within a specified path. It’s followed by various options that define the search criteria.
  2. -name "test[0-9][0-9][0-9].txt": This option specifies a pattern to match filenames. The pattern test[0-9][0-9][0-9].txt will match filenames like test001.txt, test002.txt, up to test999.txt.
  3. -type f: This option tells find us to only search for regular files (as opposed to directories or other types of files).
  4. -exec: This option is used to execute a command on each file that matches the search criteria.
  5. sh -c 'mv -f {} "$(dirname {})/$(basename {} .txt)_$(date +%Y%m%d).txt"': This part is the command that is executed for each matched file. Let’s break it down further:
    • sh -c: This runs the sh shell with the -c option, which allows you to provide a shell command as a string.
    • mv -f {} "$(dirname {})/$(basename {} .txt)_$(date +%Y%m%d).txt": This is the shell command being executed. It consists of several parts:
      • mv -f {}: This uses the mv command to move (rename) the matched file indicated by {}. The -f flag forces the move without prompting.
      • "$(dirname {})/$(basename {} .txt)_$(date +%Y%m%d).txt": This part constructs the new filename. Here’s how it works:
        • "$(dirname {})": The dirname command extracts the directory path from the matched file’s path.
        • "$(basename {} .txt)": The basename command extracts the filename without the extension. The .txt extension is removed.
        • "_$(date +%Y%m%d).txt": This adds an underscore followed by the current date in the format YYYYMMDD and the .txt extension to the filename.
  6. \;: This semicolon signifies the end of the -exec command for each matched file. It needs to be escaped or quoted to prevent the shell from interpreting it prematurely.

The outcome of rename file in Linux is depicted in the image below:

How to batch rename files in a directory to new filenames with dates in Linux

Conclusion

Remember to back up your files before performing batch operations like this, just to be safe. Also, modify the date format and naming convention as per your requirements.

I hope will this be helpful. Thank you for reading the DevopsRoles page!

About Dang Nhu Hieu

I'm Vietnamese. In the past, I'm a software developer, now working in Japan on an Infra team. Skill : AWS, VMware, HA architech,setting Database : Oracle DB, PostgresDB ,.. Programming language : Java, C#, Python, Bash linux, Batch windows, powershell ,... Hobbies: badminton, film photo, travel. https://www.linkedin.com/in/hieu-dang-15a0561a6/
View all posts by Dang Nhu Hieu →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.