Sometimes you may need to rename file with special characters in Linux. If you directly use mv or rename command to do this, it will give you an error. In this article, we will learn how to rename file with special characters.
Linux Rename File with Special Characters
You can basically use regular expressions to rename special characters in filename. Here is an example regular expression to replace white space, @ character, etc. with underscore.
Let us say you have a file with filename “a @ test”. Here is the command to rename file with special characters with a file containing underscore instead.
$ touch "a @ test" $ ls a @ test $ rename -n 's/ |\$|@/_/g' * a @ test renamed as a___test
If you also want to escape | character, you can use the following command.
$ rename -n 's/ \|\$\|@/_/g' *
Alternatively, you can also use […] notation to group special characters and replace them with underscore.
$ rename -n 's/[ @\$]/_/g' *
In this article, we have learnt how to rename file with special characters in Linux.
Also read:
How to Disable Shutdown & Reboot in Linux
How to Convert Files to UTF8 Encoding in Linux
How to Reduce Inode Usage in Linux
How to Use Port Knocking to Secure SSH in Linux
How to Enable & Disable Line Numbers in Vim
Related posts:
How to Backup & Restore Hard Disk in Linux
Shell Script to Check if Script is Already Running
How to Change Shutdown Message in Linux
How to Set User Agent with Curl
How to Assign Command Output to Variable in Shell Script
How to Zip Files & Folders in Linux
How to Find Hardware Details in Ubuntu
How to Find Oldest File in Directory

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.