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:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.