LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-22-2011, 02:49 AM   #1
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
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
 
Old 07-22-2011, 05:01 AM   #2
phaemon
Member
 
Registered: Jul 2011
Posts: 40

Rep: Reputation: 5
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".

Last edited by phaemon; 07-22-2011 at 05:01 AM. Reason: fix broken code tag
 
Old 07-22-2011, 08:09 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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'*

Last edited by David the H.; 07-22-2011 at 08:21 AM. Reason: addendum
 
1 members found this post helpful.
Old 07-25-2011, 12:11 AM   #4
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Original Poster
Rep: Reputation: Disabled
Thanks David, this is what was required.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Greping special character and removing it goelvish Programming 4 07-01-2010 09:52 AM
removing new line character using sed Fond_of_Opensource Linux - Newbie 4 03-26-2010 02:19 PM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
Removing "#" character in a huge txt file tmaxx Linux - General 3 10-24-2006 03:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration