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' '' *)
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

3

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?

steeldriver
  • 81,074
1

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!

Ungeheuer
  • 333
  • 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 01: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 01:33
  • What's the exact command you typed out? – Ungeheuer Jul 27 '19 at 01: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 02:07
  • 1
    @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 02:17