9

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.

11

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.

  • bro, nothing happened 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 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
  • 1
    I had to use rename $'\r' '' wget_* for it to work – rshdev Aug 25 '16 at 15:43
0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.