File name contains escape character--how to fix?

Assuming the file is supposed to be named Right is actually named:

Wrong\033[D\033[D\033[D\033[D\033[D\033[D\033[CRight when using the -b option of the ls command. There are a number of other files in that directory which start with Wrong so a move or copy starting with Wrong* isn't an option.

Is there a way to identify this file to copy or move it?

TIA

Try ls -i to get the inode of the file. This will uniquely identify it without depending on the filename. Then you can give that inode to find and have it feed the filename into mv for you.

$ ls -i

1235123 file1
12341 file2
64562 garbagefilename
84308 anotherfile
...

$ # Dry run - print 'mv garbagefilename betterfilename' instead of running it
$ find . -inum 64562 -exec echo mv '{}' 'betterfilename' ';'

mv garbagefilename betterfilename

$

Remove the 'echo' once you've tested and are sure it finds the file you want.

1 Like

Hi,

The inode thing is exactly what I've always done in this situtation. But I just thought I'd have a play about and see if I could find another answer, and there's this approach too (which might be handier if you need to script this on a regular basis, for some really weird reason).

On up-to-date Bash on modern Linux, you can use a regex like this:

[^[:print:]]

to match non-printable characters only. I created a file with the same name as yours, and some other besides, and here's a sample session showing that regex in action.

$ ls -lb
total 0
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:03 Right
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 15:58 Wrong\033[D\033[D\033[D\033[D\033[DRight
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:02 Wrong\ Filename.csv
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:03 Wrongness
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:02 Wrong.txt
$ ls Wrong[^[:print:]]*
Wrong?[D?[D?[D?[D?[DRight
$ rm -v Wrong[^[:print:]]*
removed 'Wrong'$'\033''[D'$'\033''[D'$'\033''[D'$'\033''[D'$'\033''[DRight'
$ ls -l
total 0
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:03 Right
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:02 Wrong Filename.csv
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:03 Wrongness
-rw-rw-r-- 1 unixforum unixforum 0 Mar 15 16:02 Wrong.txt
$

So if you absolutely needed to automate the removal of such things, this might be a useful approach if your shell and/or distro support it.

1 Like

Hi.

If for just this file in this directory:

mv -i "Wrong*Right" Right

Otherwise use the scripts noted in previous posts.

Best wishes ... cheers, drl

1 Like

Corona688: Thanks, information on inodes--I was able to use this.
drysdalk: Thanks for looking at it but we have nothing in production using bash. The only user who experimented with it has a bash_profile over 7.5 years old.
drl: Very good--wish I had thought of that.