How to remove carriage returns from directory names

filenamesrename

I created a file in Excel and ftp'd the file over to my Linux machine. In the file were a bunch of mkdir commands. Now all of the newly created directories have a carriage return at the end of them. I can find the directories using this command:

find . -type d -name *$'\r'

but when I attempt to remove them using this command:

find . -type d -name *$'\r' | xargs rm-rf

it doesn't work – nothing gets removed. The directories are still there and they still have carriage returns on them.

Can you help me create a command that will remove those pesky '\r's? Thanks.

P.S. I'm using RHEL 5.3

Best Answer

EDITED: forgot to double escape the \r in the sed line

either of these should work for you

for i in $(find . -type d -name '*\r'); do mv "$i" "$(echo $i | sed -e 's/\\r//g')"; done

find . -type d -name '*\r' -exec mv "{}" "$(echo {} | sed -e 's/\\r//g')" \;

this will find all directories named *$\r under your currently directory

it will then mv(rename) them to the same name minus the \r

Related Question