Solved

Shell script for renaming files

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…

696 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