1

Provided my admin with a shell script to rename a few folders, but for some reason those folders now contain carriage returns in the name (the script worked fine in UAT, and I'm not quite sure what the difference is between the two environments.) My application creates a folder if it can't find it, so now I have two folders containing files that need to be merged.

So if I have folders: testfolder\r and testfolder, how would I correctly write the following command to move all files from the "CR" folder into the correct folder, preserving the contents of the correct folder in the event of any filename collisions?

mv testfolder\r/* testfolder/ 
1
  • Just a comment on why this probably happened: File names are typically NULL-terminated strings, and can contain anything, including \\r (CR), \\n (LF), and even "/" or "\\", which are usually used as path name separators. SOME API libraries will treat special characters like these as special, while others will not. You probably used an API library somewhere, or used Windows'-style line endings (CRLF instead of Un*x LF), that interpreted the names differently.
    – C. M.
    Aug 7, 2021 at 11:53

1 Answer 1

0

Using bash (or ksh or zsh) and GNU mv for its -n or --no-clobber option:

for file in testfolder$'\r'/*; do mv -n "$file" testfolder; done

This uses $' ... ' ANSI C quoting to get the name of the folder with the carriage return in it; the rest is a simple loop to move -- and not clobber -- the files.

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.