[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Shell script for renaming files

Posted on 2008-10-01
5
Medium Priority
?
1,301 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 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
Suggested Courses

868 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