Often you may want to use Bash to remove a prefix from all files with a specific extension in a directory.
You can use the following syntax to do so:
for file in *.txt; do mv -v "$file" "${file#*_}"; done
This particular example will remove all characters at the start of each filename up to and including the underscore for each .txt file in the current directory.
The following example shows how to use this syntax in practice.
Example: Remove Prefix from All Filenames 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:
- 0045GAH3_owners.txt
- A000B5534_coaches.txt
- AB8839_players.txt
- employees.csv
- interns.csv
Suppose that we would like to remove the prefix leading up to and including the underscore ( _ ) in each .txt file in this directory.
We can use the following syntax to do so:
for file in *.txt; do mv -v "$file" "${file#*_}"; done
The following screenshot shows how to use this syntax in practice:

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 prefix leading up to and including the underscore removed from the filename.
All other files in the current directory that did not have a .txt extension have been left unchanged.
Note: Feel free to replace the underscore with any other character or pattern that you would like depending on the specific prefix that you would like to remove from your filenames.
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
