Scripting – How to Remove Newlines in File Names

filesrenamescripting

I have a PHP code that generates the file name on which wget will append its logs. I generated 2000+ files, but the problem is I am having trouble working with them because I had a mistake of putting PHP_EOL as part of its name, that code will add
LF/line feed/%0A at its name

Example of such file name (when accessed via browser, when put on /var/www/html) http://xxxx/wget_01_a%0a.txt notice the %0a before the extension name

I messed up, and I wish there's a rename batch that will search through all files and if it found line feed it would rename it without the line feed so it would just be http://xxxx/wget_01_a.txt

I am not pretty sure how to handle this because seems like when I ls on putty all special character not limited to that unwanted char becomes ?, what I only wish to target is that line feed.

Best Answer

Using the utility rename from util-linux, which CentOS 6 provides, and assuming bash:

rename $'\n' '' wget_*

This asks to delete newline characters from the names of listed files. I recommend trying it out on a small subset to ensure it does what you want it to (note that rename on CentOS 7 supports a -v switch to show you what changes it is making).

If instead you were on a distribution that provides the Perl-based rename:

rename -n 's/\n//g' wget_*

And then run without -n to actually perform the renaming.

Related Question