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/ 
2
  • 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. Commented Aug 7, 2021 at 11:53
  • In Unix systems, NULL and "/' (slash) are both banned in filenames. But nothing else. There are also some length limits, but not on the overall filepath, although a deep enough tree will start making modern shells freak out (the old sh continues to work just fine). Commented Jul 11, 2024 at 4:19

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.