1

I'm trying to clean up some files on a Linux NAS. They were transferred over from old storage years ago now files and folders include the newline character "\n" at the beginning and end of the file\folder name.

If I look at them "ls -lab" I get something that looks like this:

\n Folder_Name \n

A similar post helped me remove the "\n" from the beginning of the filename but the one at the end is lingering.

(rename $'\n' '' *)
2

Your system's rename appears to be the util-linux variant1 - in which case you can just run the same command a second time to remove the second newline ex.:

$ ls -Q
"\nfile name\n"
$ rename.ul $'\n' '' *
$ ls -Q
"file name\n"
$ rename.ul $'\n' '' *
$ ls -Q
"file name"

1 see What's with all the renames: prename, rename, file-rename?

| improve this answer | |
  • SMH I want to promise myself that I did this, but apparently I didn't Thank you! – Ian Young Jul 27 '19 at 1:42
  • @IanYoung n/p - glad to help – steeldriver Jul 27 '19 at 1:46
  • Thanks! thought you might want to see the finished product. cd -- "$(find . -name $'[\n]' -type d -printf '%h' -quit | grep -v “/.backups”)” ; rename $'\n' '' * ; rename $'\n' '' * – Ian Young Jul 29 '19 at 23:28
0

You can just mv the file like usual, you just need to put the file name in quotes and hit the return key. So:

$ ls -b
  file\n
$ mv "file
> " file
$ ls -b
  file

That should do the trick for you, it's worked for me in the past. It would have also worked for the original case, you'd have just need a return before the filename as well.

Welcome to Unix StackExchange!

| improve this answer | |
  • I tried that, but the mv command doesn't recognize the file path mv: cannot stat ‘\FILENAME\\n’: No such file or directory [1]+ Exit 1 mv -i "\FILENAME\n" Breaking – Ian Young Jul 27 '19 at 1:32
  • I can manually do one directory at a time when there's only ONE directory mv * NEW_DIRECTORY_NAME but if I try any form of entering the directory name as it's listed (\nDIRECTORY_NAME\n) it can't be found – Ian Young Jul 27 '19 at 1:33
  • What's the exact command you typed out? – Ungeheuer Jul 27 '19 at 1:36
  • ls -b DIRECTORY\n mv "DIRECTORY\n" DIRECTORY mv: cannot stat '\\DIRECTORY\\n' No such file or directory – Ian Young Jul 27 '19 at 2:07
  • @IanYoung Once you open the double-quote, type the filename then hit the return/enter key. It won't recognize the linefeed escape sequence. – Ungeheuer Jul 27 '19 at 2:17

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.