Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove special characters in file names?

Tags:

linux

bash

When creating playlists I often came across files that would break the playing process. Such would be files with spaces or apostrophes. I would fix it with the following command

for file in *.; do mv "$file" `echo $file | tr " " '_'` ; done **(for spaces)**

Now I more often come across files with commas, apostrophes, brackets and other characters. How would I modify the command to remove such characters?

Also tried rename 's/[^a-zA-Z0-9_-]//' *.mp4 but it doesnt seem to remove spaces or commas

like image 793
Andy Avatar asked Nov 21 '16 03:11

Andy


People also ask

How do I remove special characters from a filename in Python?

The backslash ( \ ) should be escaped. ( '[\\\\/*?:"<>|]' or r'[\\/*?:"<>|]' ). Otherwise backslashes will not be removed.

Can file names have special characters?

Illegal Filename CharactersDon't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

Which code will remove all special characters?

You can use a regular expression and replaceAll() method of java. lang. String class to remove all special characters from String.


1 Answers

for file in *; do mv "$file" $(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g'); done &
like image 138
Jairo Bernal Avatar answered Oct 25 '22 20:10

Jairo Bernal