?
Solved

Shell script for renaming files

Posted on 2008-10-01
5
Medium Priority
?
1,297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Assisted Solution

by:wwnosal
wwnosal earned 300 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 1200 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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

741 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