13

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.

arvil
  • 670

2 Answers2

13

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.

dhag
  • 15,736
  • 4
  • 55
  • 65
  • bro, nothing happened http://pastebin.com/NjMg8vTL I am using centos 6 – arvil Mar 12 '15 at 18:34
  • Perhaps the linefeed character you have isn't actually \n? Can you do something like echo *.txt | od -c so we see what actual non-printable character is there? – dhag Mar 12 '15 at 18:40
  • seems like \n http://pastebin.com/wXCDCJjr – arvil Mar 12 '15 at 18:45
  • I'm not sure what's wrong, unless for some reason the file in your previous paste does not match *.txt (which would explain why rename doesn't pick it up). If you set the two patterns to, say, txt instead of $'\n' and '', do you get any output? Does echo $'\n' indeed print two newlines? – dhag Mar 12 '15 at 18:55
  • removing -v to parameter seemed to solved my problem? if you update your answer I can mark your answer correct thanks! – arvil Mar 12 '15 at 19:02
  • Well, that's weird; this must mean your version of rename does not support options and understood your query to mean "replace -v with newline". It's great if it means your problem is solved. Out of curiosity, what does rename --help say about -v, if anything? – dhag Mar 12 '15 at 19:06
  • Oh I see; CentOS 6 rename only supports call: rename from to files.... I will edit my answer to reflect this. – dhag Mar 12 '15 at 19:11
  • 1
    I had to use rename $'\r' '' wget_* for it to work – rshdev Aug 25 '16 at 15:43
6

Building on what was already answered, I've generalized it to rename all files containing line feed in current folder and sub-folder, by combining find command with rename -

find -name $'*\n*' -exec rename  $'s|\n| |g' '{}' \;

Here, find command locates all files containing line feed and rename command replaces every line feed in the name with a space.

The same can be done for any other such problematic characters such as carriage return (\r).

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232