Solved

Shell script for renaming files

Posted on 2008-10-01
5
1,293 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert grep lines to perl 6 79
shell script 2 52
looking for a CENTOS ISO to download with x window installed 2 71
Understanding ping command in Ubuntu 5 33
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…

726 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