Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-18-2008, 07:55 PM   #1
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:


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.

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

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

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

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.


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
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 03:16 AM
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
Find and special file names CritterZ Linux - Newbie 2 12-23-2004 10:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 AM.

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