Register
It is currently Tue Sep 30, 2014 5:55 pm

Script to change all semicolons to dashes


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat May 14, 2011 12:42 pm   

Joined: Sat May 14, 2011 12:39 pm
Posts: 1
I'd like some instructions on how to write a script that changes every semicolon in the filename of every file in every subfolder of a folder to a space and a dash.

Thanks!


Top
 Profile  
 PostPosted: Sat May 14, 2011 4:55 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
In a script you can call find to search for all files within a directory/subdirectory and replace all semicolons with the characters you need using sed.

The example below worked for me, but I don't know what others with more experience may think. I think its fine.

Code:
find . -type f -print0 | xargs -0 sed -i 's/;/ -/g'


Cheers-


Top
 Profile  
 PostPosted: Sat Jun 11, 2011 9:23 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
If you have bash(1) 4.x, you can use globstar:

Code:
#!/bin/bash
shopt -s globstar

dir=$HOME/path/foo

for f in $dir/**/*; do
    if [[ $f = *\;* ]]; then
        mv -- "$f" "${f//;/ -}"
    fi
done


You can also use a neat tool called rename :

Code:
#!/bin/bash
shopt -s globstar

dir=$HOME/path/foo

rename 's/;/ -/g' dir/**/*


canit0 wrote:
The example below worked for me, but I don't know what others with more experience may think. I think its fine.


He wanted to rename his files, not to edit them.
So, with find(1), you can do :

Code:
find $dir/ -type f -name '*;*' -print0 | while IFS= read -rd '' f; do mv -- "$f" "${f//;/ -}"; done


Last edited by jsz on Sun Jul 10, 2011 8:05 pm, edited 4 times in total.

Top
 Profile  
 PostPosted: Sat Jun 11, 2011 6:05 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
I missed the point of the question, but thanks for bringing this up jsz.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP