LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-13-2011, 11:04 AM   #1
lee_can
Member
 
Registered: Mar 2011
Posts: 118

Rep: Reputation: 1
Script to change the extension in folders


Hi,
i want to write a script to change the extension of a file to new extension.

i already wrote a script which i can change all the files in one folder, but the thing that i have more then 300 folders, and i want to go to each folder and run the script.
is there a way, to run the script, so it can go to each folder and change the extension.
for example, the main folder is "main" located in /root/home/main
in main folder, i have the folders: f1, f2, f3, ..., f100.
the files located in "f1, f2, ..." are like this: "1.txt;1", "2.tta;1", etc,,
the extension is ".txt;1", "tta;1".
so, my script is:

Code:
#!/bin/bash
for name in `ls -1 | grep -v $0`
do
         file_ext_1=`echo $name |cut -d';' -f1`
        mv $name $file_ext_1
done
how can i change this script to start by itself going to each folder and change the ext, but i just need to to do this only on "main" and its folders.

Thanks in advance.
regards

Last edited by lee_can; 11-13-2011 at 11:06 AM.
 
Old 11-13-2011, 11:56 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Please be aware of
0) proper quoting,
1) the difference between "for" and "while" loops,
2) problems due to spaces in file names and
3) using the wrong tool for the right job (like using 'ls' instead of 'find'):

Code:
find /root/home/main -type f -iname "*.txt;*" | while read ITEM; do
 NEWNAME="${ITEM//;1/}"; echo mv "${ITEM}" "${NEWNAME}"
done

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
1 members found this post helpful.
Old 11-13-2011, 06:25 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I am surprised the original script worked at all as a semi colon is deemed the end of the previous command.
A slight edit you can do on unSpawn's script is:
Code:
NEWNAME="${ITEM//;1/}"

NEWNAME="${ITEM%;1}"
Not sure there is any advantage of one over the other.
 
1 members found this post helpful.
Old 11-14-2011, 10:02 AM   #4
lee_can
Member
 
Registered: Mar 2011
Posts: 118

Original Poster
Rep: Reputation: 1
[QUOTE=unSpawn;4523007]Please be aware of
0) proper quoting,
1) the difference between "for" and "while" loops,
2) problems due to spaces in file names and
3) using the wrong tool for the right job (like using 'ls' instead of 'find'):

Code:
find /root/home/main -type f -iname "*.txt;*" | while read ITEM; do
 NEWNAME="${ITEM//;1/}"; echo mv "${ITEM}" "${NEWNAME}"
done

Very big thanks for you, i really appreciate your help, it save me lot of time.
and also thanks for the advise.

Regards
 
  


Reply



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
bash script to create folders including making recursive folders.... linux-bandit Linux - Software 8 11-28-2009 01:50 AM
change file extension umesh_linuxq Linux - Newbie 2 06-24-2009 05:11 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 07:29 PM
change permissions of all folders and sub folders cad Linux - General 3 01-28-2007 10:21 AM
Need script to add extension stefaandk Linux - General 1 10-05-2005 10:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 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
Open Source Consulting | Domain Registration