renaming


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming
# 1  
Old 05-29-2009
renaming

Hi, I wrote a script to rename all files in a directory from uppercase to lowercase and changing spaces to underscores:

#!/bin/sh

echo "rename"
read pathname
cd $pathname
for f in `ls "$pathname"` ; # listing directory for all files
do
echo "$f"
newfile=`echo "$f" | sed 'y/QWERTYUIOPASDFGHJKLZXCVBNM/qwertyuiopasdfghjklzxcvbnm/' | sed 'y/ /_/'` #changing uppercase filenames to lowercase
mv "$f" "$newfile"
echo "All possible filenames changed from uppercase to lowercase"
done

this script is working fine when converting from uppercase to lowercase, but it does not work when files contain spaces in their names
any ideas??

thank you
# 2  
Old 05-29-2009
Quote:
for f in `ls "$pathname"` ; # listing directory for all files
do
To preserve the filenames containing spaces, try a while loop.

Code:
# listing directory for all files
ls "${pathname}" | while read f 
do

Following a recent dialogue on this forum, you will be advised to check the directory before issuing a "cd".

Code:
if [ ! -d "${pathname}" ]
then
       echo "Invalid directory: ${pathname}"
       exit
fi
cd "${pathname}"


Last edited by methyl; 05-29-2009 at 12:45 PM.. Reason: Typo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple renaming

Hi Having many filenames in a folder containing the string "-DATE" (e.g. : Nbr_Name-DATE_2011.txt). I would like to remove the "-DATE" in each filename to get just: Nbr_Name_2011.txt . Know its easy but somehow it wont work. Tried it like that: for i in `ls *-DATE*`; do mv "$i"... (10 Replies)
Discussion started by: jurgen
10 Replies

2. UNIX for Dummies Questions & Answers

renaming a file

Hi I remember i have create a file "Dechistory" before a month. But i dont remember exact location. I know i need to find out first that fiiles exact location for renaming it .......but i waant to that without going on that location.......how to do that ??? (1 Reply)
Discussion started by: tusharjoshi
1 Replies

3. UNIX for Dummies Questions & Answers

renaming filenames

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (0 Replies)
Discussion started by: vpv0002
0 Replies

4. Shell Programming and Scripting

Renaming a file

I am a complete Unix newbie and I need some help! (Please...) I need to rename a file from the following format: Test_Test_EAR_1234.ear To the following: Test_Test_EAR.ear In other words, I need to remove everything after & including the final underscore up to the "." What is the best... (6 Replies)
Discussion started by: VeloLisa
6 Replies

5. Shell Programming and Scripting

Renaming a file

Hai masters, Suppose i have so many files named xxxx.ksh.tar, yyyy.ksh.tar and so on. My requirement : i want to rename all the files with extension ".ksh.tar" to ".tar" in my directory. Could you please tell me the solution for this.. (5 Replies)
Discussion started by: ecearund
5 Replies

6. Shell Programming and Scripting

renaming file

Dear Friends, Need your help once again. I have this file name e.g.1) report_12.rp_1 e.g.2) remark_mm.rmr_3 I want it to be renamed as report_12_1.rp remark_mm_3.rmr (3 Replies)
Discussion started by: anushree.a
3 Replies

7. UNIX for Dummies Questions & Answers

renaming files

Hello all- I need to rename files by adding an embedded 0 e.g. aaa_bbb_1234 needs to become aaa_bbb_01234 The aaa and 1234 will change but the bbb_ can be my anchor. TIA (9 Replies)
Discussion started by: ohagar
9 Replies

8. UNIX for Dummies Questions & Answers

Help in renaming file !!!

Hi All, I want to rename a file inside a script which has a date portion appended at the start of the file name. The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too. #!/usr/bin/ksh cd /space/file/source dt=$(date "date "+%m%d%Y")... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

9. Shell Programming and Scripting

Renaming files

hi, I have the following files as below. reg_test_123232 reg_test_125654 reg_test_473843 How do I rename reg_test_123232 to abc_123232 and the rest of file by just keeping the numeric field ? Please advise :) (3 Replies)
Discussion started by: cedrichiu
3 Replies

10. Shell Programming and Scripting

Renaming the files

Hello, i wanna rename my files which names are written in movies.txt films.txt = amovie bmovie cmovie dmovie emovie and i wanna find this files and rename the files to 1_amovie ... (12 Replies)
Discussion started by: redbeard_06
12 Replies
Login or Register to Ask a Question