Often you may want to use Bash to remove specific characters from filenames.
You can use the following syntax to do so:
for file in *; do mv "${file}" "${file/2024/}"; done
This particular example will remove the pattern 2024 from each filename in the current directory.
Note that this method uses a for statement to loop through each file in the current directory, then uses a mv statement to “rename” each file by simply removing 2024 from each filename.
The following example shows how to use this syntax in practice.
Example: How to Remove Specific Characters from Filenames in Bash
We can use the ls command to first view all files in our current directory:

We can see that our current directory contains the following files:
- athletes2024.txt
- managers2024.txt
- owners2024.txt
- teams2024.txt
Suppose that we would like to remove the pattern 2024 from each filename in the current directory.
We can use the following syntax to do so:
for file in *; do mv "${file}" "${file/2024/}"; done
The following screenshot shows how to use this syntax in practice:

Notice that when we use the ls statement again we can see the updated filenames in the current directory:
- athletes.txt
- managers.txt
- owners.txt
- teams.txt
Notice that the pattern 2024 has been removed from each of the filenames.
Feel free to replace 2024 with any specific pattern that you would like to remove from each of your own filenames.
Note: If a file is missing the pattern 2024 then the filename will simply remain unchanged.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Check if String Starts with Specific Value
Bash: How to Check if String Contains Substring
Bash: How to Remove First and Last Characters from String
