Operating System - HP-UX
1849261 Members
5894 Online
104042 Solutions
New Discussion

Re: remove certain characters from file name

 
SOLVED
Go to solution
Ratzie
Super Advisor

remove certain characters from file name

I have a directory that has about 200 files, I need to go thru and remove any " - " (dash) from the files, how can I do this.
16 REPLIES 16
Mel Burslan
Honored Contributor
Solution

Re: remove certain characters from file name

for file in `ls -1`
do
newname=`echo $file | sed -e "1,1s/-//g"`
mv $file $newname
done
________________________________
UNIX because I majored in cryptology...
Ratzie
Super Advisor

Re: remove certain characters from file name

Better yet, how about just removing the first - it sees in the filename
Mel Burslan
Honored Contributor

Re: remove certain characters from file name

Actually,

for file in `ls -1 *-*`
do
newname=`echo $file | sed -e "1,1s/-//g"`
mv $file $newname
done

is better as the first version will complain for the files without dashes in their names, although it will do the job as well.
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: remove certain characters from file name

to remove the first occurance of "-" just drop the "g" at the end of the sed directive as such:

for file in `ls -1 *-*`
do
newname=`echo $file | sed -e "1,1s/-//"`
mv $file $newname
done
________________________________
UNIX because I majored in cryptology...
Sandman!
Honored Contributor

Re: remove certain characters from file name

To remove the first occurence of "dash" in the filename you could use the awk construct below:

# ls *-* | awk -F"-" '{x=$0;sub("-","",$0);system("mv "x" "$0)}'
Sandman!
Honored Contributor

Re: remove certain characters from file name

To remove all occurences of "dash" in a filename here's a slightly modified version of the awk construct posted previously:

# ls *-* | awk -F"-" '{x=$0;gsub("-","");system("mv "x" "$0)}'
Muthukumar_5
Honored Contributor

Re: remove certain characters from file name

You can do it as,

cd
for file in `find . -type f`
do

mv $file $(echo $file | tr -d '-')

done

hth.
Easy to suggest when don't know about the problem!
Ratzie
Super Advisor

Re: remove certain characters from file name

This is all good, but I have spaces in my file names, so it trys to do it per word...
Tim Nelson
Honored Contributor

Re: remove certain characters from file name

you can change your IFS to something else i.e. | or ,

Make sure you change it back or only set for your session.

Tim Nelson
Honored Contributor

Re: remove certain characters from file name

Another thought. you can use the same SED commands to replace the space with \space

Worth a try.
john korterman
Honored Contributor

Re: remove certain characters from file name

or you can use Mel's suggestion - remember to use plenty of qoutes, e.g.:

for file in "`ls -1 *-*`"
do
newname=`echo "$file" | sed -e "1,1s/-//g"`
mv "$file" "$newname"
done

regards,
John K.
it would be nice if you always got a second chance
Mel Burslan
Honored Contributor

Re: remove certain characters from file name

to remove all the dashes

ls -1 *-* | while read file
do
newname=`echo "$file" | sed -e "1,1s/-//g"`
mv "$file" "$newname"
done

to remove the first occurance of dash

ls -1 *-* | while read file
do
newname=`echo "$file" | sed -e "1,1s/-//"`
mv "$file" "$newname"
done


________________________________
UNIX because I majored in cryptology...
TwoProc
Honored Contributor

Re: remove certain characters from file name

This one works in sh, ksh.

IFS="|"
for i in *; do mv $i ${i/-/}; done
unset IFS
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: remove certain characters from file name

The above one works with file names that have spaces in the file name (forgot to mention that).
We are the people our parents warned us about --Jimmy Buffett
Sandman!
Honored Contributor

Re: remove certain characters from file name

To remove all dashes from a filename that has spaces too use the awk construct below. This won't remove spaces from a filename, only dashes:

# ls *-*\ * | awk '{x=$0;gsub(" ","\ ");gsub("-","");gsub(" ","\ ",x);system("mv "x" "$0)}'

awk construct below to remove the first "dash" from a filename that has spaces. Again, this won't remove spaces from a filename, only the first dash:

# ls *-*\ * | awk '{x=$0;gsub(" ","\ ");sub("-","");gsub(" ","\ ",x);system("mv "x" "$0)}'

cheers!
Ratzie
Super Advisor

Re: remove certain characters from file name

Thanks for all the help