Thursday, October 1, 2015

Batch removal of special characters from file names in Linux



I am seeking to remove all special characters from several files' worth of downloaded .pdfs, and came across exactly the solution I was looking for, albeit in an OS X environment:



function to automatically remove special characters from file names during saving in MacOS X.



Could a similar method--either using sed or some other function--be implemented in a Linux environment?


Answer



You can do this with the rename command. If you are in the folder with the .pdf files with special characters:




rename 's/[^a-zA-Z0-9]//g' *.pdf


This will remove any characters from files ending in .pdf that are not A-Z in either case, or numbers. You can add to this list:



rename 's/[^a-zA-Z0-9_]//g' *.pdf


This version allows underscores.



No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...