LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Removing abnormal character DEL from a file name (https://www.linuxquestions.org/questions/linux-general-1/removing-abnormal-character-del-from-a-file-name-893095/)

linuxlover.chaitanya 07-22-2011 02:49 AM

Removing abnormal character DEL from a file name
 
Hello all

I have got certain files which somehow contain abnormal character "Del" "0x7f" or 177 which represents Del. And this is causing SVN to reject these files and abruptly end the process. I need to remove those characters from the file names but am not able to. find or grep do not search the files. This is how the file looks like with ls or find

Code:

Overview_Troms?_2-thumb-144xauto-40672.jpg

phaemon 07-22-2011 05:01 AM

Try renaming using the inode number. You can get it using the -i switch for ls:
Code:

ls -ali
You'll get something like:
Code:

192371 -rw-r--r-- 1 phaemon phaemon 0 2011-07-22 10:59 odd.file
The inode number is 192371 in this case (the first number), so you can do:
Code:

find . -inum 192371 -exec mv {}  new.filename \;
which will mv your file to "new.filename".

David the H. 07-22-2011 08:09 AM

This should work as well. It removes all control characters (ascii octal 000-037,177).

Code:

file="Overview_Troms?_2-thumb-144xauto-40672.jpg"
mv "$file" "${file//[[:cntrl:]]}"

You probably need to use some kind of globbing or other automatic file matching to get the filename into the variable; a simple for loop or somesuch.

You can specify a replacement character as well. Just use "${file//[[:cntrl:]]/-}" to change them to to hyphens, for example.

Edit: I just noticed what you said about having trouble searching for such files. You can use the [:cntrl:] character class in a regex or glob as well.

Code:

for file in ./*[[:cntrl:]]* ; do
  mv "$file" "${file//[[:cntrl:]]}"
done

You can also use ansi-c quoting to include specific non-printing characters in a string, in octal or hex.

Code:

ls *$'\177'*
ls *$'\x7f'*


linuxlover.chaitanya 07-25-2011 12:11 AM

Thanks David, this is what was required.


All times are GMT -5. The time now is 08:35 AM.