File name contains escape character--how to fix?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users File name contains escape character--how to fix?
# 1  
Old 03-15-2017
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
# 2  
Old 03-15-2017
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.

Code:
$ 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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-15-2017
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.

Code:
$ 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.
This User Gave Thanks to drysdalk For This Post:
# 4  
Old 03-15-2017
Hi.

If for just this file in this directory:
Code:
mv -i "Wrong*Right" Right

Otherwise use the scripts noted in previous posts.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 03-15-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function to add escape character before specified character

Hi , I am looking for a function which will do the following. 1. I have a variable which will hold few special chracter like SPECIAL_CHARS="& ;"2. I have an escape character. ESCAPE_CHAR="\"3. Now when I passed some string in the function it will return the same string but now it will... (8 Replies)
Discussion started by: Anupam_Halder
8 Replies

2. Shell Programming and Scripting

ksh escape a character

friends, I have a situation where i am using a $RANDOM function along with the filename, I want this to be escaped by the OS in the first assignment (works as expected) and executed in the second assignment (does not execute $RANDOM) filename1=filename1_\$RANDOM echo $filename1... (3 Replies)
Discussion started by: Balaji M
3 Replies

3. Shell Programming and Scripting

sed - with escape character

i have string as below str=".<date>" in which i need to replace < with /< , when i tried with sed , got the output. --> echo $str | sed 's/</\\</g' .\<date> when i tried to assign it to a variable , i am not getting the same --> a=`echo $str | sed 's/</\\</g'` ; echo $a... (4 Replies)
Discussion started by: expert
4 Replies

4. Shell Programming and Scripting

replace \ (escape character)

All , i have input line as below . abc\ , ewioweioi \, and want the output as below removing the "\" abc , ewioweioi , could anyone help me out (2 Replies)
Discussion started by: expert
2 Replies

5. Shell Programming and Scripting

awk escape character

ll|awk '{print "INSERT INTO SCHEMA.TABLE_NAME VALUES (`"$9 "`,"$5");" }' INSERT INTO SCHEMA.TABLE_NAME VALUES (``,); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.dmp`,7923328); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.log`,1389); But I want ' in... (2 Replies)
Discussion started by: faruque.ahmed
2 Replies

6. Shell Programming and Scripting

Escape character - sed

Hi All, How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character? Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :( Example of etc/passwd file: blah:x:1055:600:blah... (6 Replies)
Discussion started by: c00kie88
6 Replies

7. Shell Programming and Scripting

escape character in tcsh

Hello, I wanted to display command with echo like this in tcsh. output should be "//'test.test1.test2'" (" at both ends also required in output) Please help me. (1 Reply)
Discussion started by: balareddy
1 Replies

8. Shell Programming and Scripting

Escape character

Hi , I want to change space to ' in my script. I tried doing this, sed 's/ /\'/g' filename but i could not get it. can some one help me please. Thanks, Deepak (4 Replies)
Discussion started by: deepakpv
4 Replies

9. Shell Programming and Scripting

awk / escape character

Hi I'm trying to split a dir listing eg /home/foo1/foo2 I'm using ksh I've tried dir=/home/foo1/foo2 splitit=`echo $dir | awk -F '\/' '{print $1}'` echo $splitit nothing is output! I have checked the escape character. The only one I have found is \ BTW `pwd` | awk -F \/... (8 Replies)
Discussion started by: OFFSIHR
8 Replies

10. Shell Programming and Scripting

Escape character in vi?

I want to replace a string which contains "/" in vi but what is the escape character for forward slash? e.g. I have a text file with the contents below and I want to replace "/Top/Sub/Sub1" with "ABC". /Top/Sub/Sub1 The replace command I am using is ... (4 Replies)
Discussion started by: stevefox
4 Replies
Login or Register to Ask a Question