Rename files and folders to remove forbidden chars
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Rename files and folders to remove forbidden chars
Hello,
I need a bash (or python) script to rename files and folders (and subfolders, subfiles...) in order to remove chars that are not allowed on Windows NTFS Filesystem (see https://gist.github.com/doctaphred/d...-filenames-txt) that should be these:
<>:"/\|?*
Linux allows to use virtually every char (except from slash and NULL, I think) but on Windows chars like semicolon, colon and some kind of quote are infeasible.
I would like to replace Windows forbidden chars with hypen (or underscore, empty space...no matter).
If you have the rename utility, preferably the perl version, then you can do find and replace on file names using patterns.
Code:
rename -n 's/[\<\>\:\"\/\\\|\?\*]/-/g' ./*
The perl version will not only take any perl regular expression, but even any general perl expression so variables and calculations are possible.
It does not work.
I created a file called bi-"touch.txt and frenzy:.txt on a subfolder then I launched the command and the files remained bi-"touch.txt and frenzy:.txt
rename --version states that rename comes from util-linux 2.36.1
I also tried the centos.tips solution but it's not recusive.
... and the files remained bi-"touch.txt and frenzy:.txt.
Then it worked as intended: You have received a defanged example and need to skim the manual page to see how to activated it. Pay attention to the options' descriptions.
In the shell a backslash within single quotes is literal.
echo has problems if a filename looks like an option ("-ne"), use printf instead.
The $( ) must be in double quotes to avoid further expansion (a space causes a word split)
from one side it is really good (OP found a solution) from another side it is not really good the advices were not taken into account:
First of all the perl version of rename can do that job, there is no need to use find, mv, tr, sed ....
But if you wish to use find:
Code:
find | while read -r FILE ; do
new_name="${FILE//[^A-Za-z0-9._-]/_}"
[[ $FILE == $new_name ]] || mv "${FILE}" "${new_name}"
done
would be still much better (although that is not exactly the same).
According to post #1 they want to delete "<>:"/\|?*" - there are no brackets in that list, and there does not need to be!
In addition to those nine characters, there are several specific filenames which are not permitted, along with other restrictions (like not ending with a dot).
There is code which appears to handle all this already in the link in post #1 - it would simply need to be translated from JavaScript to a suitable CLI-friendly language.
The main reason this post doesn't include such a translation is because at some point in the past three decades someone is certain to have written such a function already, and they probably also made it robust with features such as collision detection - not just between incompatibly named files, but between acceptable files which happen to reside in directories that need renaming, not to mention between files/directories that only have acceptable characters but differ in case.
It seems a waste of time playing with scripts rather than identifying that established and tested solution.
rename --version states that rename comes from util-linux 2.36.1
@OP. If you're on CentOS(* then you're out of luck: Perl there doesn't provide the rename script. Rebuild the package prename from Fedora sources or install File::Rename from CPAN.
______
* I doubt this is the case as CentOS 8 Stream currently only has util-linux 2.32.1; util-linux 2.36.1 looks more like Ubuntu 21.04 Hirsute, Debian 11 Bullseye, Mageia 8 or Fedora 33.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.