Bash: How to Add Suffix to All Files in Directory


Often you may want to use Bash to add a suffix to all files with a specific extension in a directory.

You can use the following syntax to do so:

for file in *.txt; do mv "$file" "${file%.txt}_new.txt"; done 

This particular example will add the suffix _new to the end of each .txt file in the current directory.

The following example shows how to use this syntax in practice.

Example: Add Suffix to All Files in Directory in Bash

We can use the ls command to first view all files in our current directory:

We can see that the current directory contains the following files:

  • coaches.txt
  • employees.csv
  • interns.csv
  • managers.txt
  • owners.txt
  • players.txt

Suppose that we would like to add the suffix _new to the end of each .txt file in this directory.

We can use the following syntax to do so:

for file in *.txt; do mv "$file" "${file%.txt}_new.txt"; done 

The following screenshot shows how to use this syntax in practice:

Bash add suffix to all files

Notice that when we use the ls command again to view the updated files in the current directory, we can see that each .txt file has had the suffix _new appended to it.

All other files in the current directory that did not have a .txt extension have been left unchanged.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Size
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files Matching a Specific Pattern

Leave a Reply