LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-18-2008, 07:55 PM   #1
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Rep: Reputation: 0
BASH: Truncating file names with special character


I have a directory with files in it. They have the following naming convention:

file1.txt;1
file2.txt;3
file3.txt;24

I want to remove the ';' and any number that appears after it. Can I use awk or tr to do this? I am thinking of using something like this.

Code:
for x in `ls`
     do
     if [ ! -f $x ]; then
     continue
     fi
     nf=`echo $x | tr <insert code here>
     if [ $nf != $x ]; then
         mv -i $x $nf
     fi
done
Any help appreciated. Thanks.
 
Old 12-18-2008, 08:14 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
nf=`sed 's/;[^;]*$//' $x`

matched pattern is "; + any # of chars not ;, at the end of the line". This allows something with more than one ";" (only the last will be removed)
 
Old 12-18-2008, 08:28 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
I got a killer one-liner:

Code:
find . -name '*;*' -exec bash -c 'mv "{}" "$(echo "{}" | sed -e "s/;[0-9]*\$//g")"' \;
It works for files with spaces in the name (this was the hardest part to figure out), and won't touch files with a ; followed by digits in the middle of the name (only at the end). It's also fully recursive through the directory structure, or only in the current directory or however you wish by tweaking the find flags. Using find is safer than ls because of the variety of conditions under which ls could produce more information than just the filename, for example if ls is aliased to 'ls -l' or such.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic special character escaping in Bash scripts? wipe Linux - Software 1 06-05-2009 07:41 PM
HowTo: special chars (ŰűŐő) in file names ceetrom Linux - Newbie 1 11-03-2006 01:53 PM
& character in file/directory names paddyjoy Programming 4 03-16-2006 11:41 PM
block special and character special files s_shenbaga Linux - Newbie 1 04-20-2005 04:49 PM
Find and special file names CritterZ Linux - Newbie 2 12-23-2004 10:49 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration