Solved

Shell script for renaming files

Posted on 2008-10-01
5
1,285 Views
Last Modified: 2013-12-26
Dear experts,

Please help me creating a bash/ksh shell script which will rename all .txt and .TXT files to .csv file in a particular directory and with in it's sub directories.

Thanks in advance.







0
Comment
Question by:mohona
5 Comments
 
LVL 6

Assisted Solution

by:wwnosal
wwnosal earned 100 total points
ID: 22612799
#!/bin/bash
#run this script in top directory for which you want to subdirs searched or change "." in find to your #starting dir
for F in `find . -name "*.txt"`; do
   CSV=`echo $F| sed s/\.txt$/\.csv/`;
   mv $F $CSV;
 done
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22613318
try

find /dir -name "*.txt' -o -name "*.TXT" | while read file
do
   nfile=`echo $file | sed 's/\....$//'`
   mv $file $nfile.csv
done
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 400 total points
ID: 22613333
to avoid problem with file names that have space in them:

find /dir -name "*.txt' -o -name "*.TXT" | while read file
do
   nfile=`echo $file | sed 's/\....$//'`
   mv "$file" "$nfile.csv"
done
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 22613627
if you  have Python, you can use the script here.
eg usage:


# ls -1

test.txt

test1.TXT
 

# filerenamer.py -p ".txt" -e ".csv" -l -I "tes*.*"

==>>>>  [ /home/test.txt ]==>[ /home/test.csv ]

==>>>>  [ /home/test1.TXT ]==>[ /home/test1.csv ]
 

# filerenamer.py -p ".txt" -e ".csv"  -I "tes*.*" #remove -l to commit

/home/test.txt  is renamed to  /home/test.csv

/home/test1.TXT  is renamed to  /home/test1.csv
 

# ls -1

test.csv

test1.csv

Open in new window

0
 

Author Closing Comment

by:mohona
ID: 31501899
Thank you all.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now