Rename all files (filename with spaces) to different extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename all files (filename with spaces) to different extension
# 1  
Old 06-13-2012
Rename all files (filename with spaces) to different extension

Hi,

I have files with filenames as below.

Code:
SGM Daily Sales Email-en-us-05312012.xlwa

I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders.

I have the following script.

Code:
#!/bin/ksh
for oldfile in $(find /usr/local/data/vstar/report_repository -name "*.xlwa")
do
newfile="$(echo $oldfile | sed 's/.xlwa/.xls/g')"
mv "$oldfile" "${newfile}"
done

this is the log of it.

Code:
find /usr/local/data/vstar/report_repository -name "*.xlwa"
++ find /usr/local/data/vstar/report_repository -name '*.xlwa'
+ for oldfile in '$(find /usr/local/data/vstar/report_repository -name "*.xlwa")'
echo $oldfile | sed 's/.xlwa/.xls/g'
++ echo /usr/local/data/vstar/report_repository/SGM/SGM
++ sed s/.xlwa/.xls/g
+ newfile=/usr/local/data/vstar/report_repository/SGM/SGM
+ mv /usr/local/data/vstar/report_repository/SGM/SGM /usr/local/data/vstar/report_repository/SGM/SGM
mv: cannot stat `/usr/local/data/vstar/report_repository/SGM/SGM': No such file or directory
+ for oldfile in '$(find /usr/local/data/vstar/report_repository -name "*.xlwa")'
echo $oldfile | sed 's/.xlwa/.xls/g'
++ echo Daily
++ sed s/.xlwa/.xls/g
+ newfile=Daily
+ mv Daily Daily
mv: cannot stat `Daily': No such file or directory
+ for oldfile in '$(find /usr/local/data/vstar/report_repository -name "*.xlwa")'
echo $oldfile | sed 's/.xlwa/.xls/g'
++ echo Sales
++ sed s/.xlwa/.xls/g
+ newfile=Sales
+ mv Sales Sales
mv: cannot stat `Sales': No such file or directory
+ for oldfile in '$(find /usr/local/data/vstar/report_repository -name "*.xlwa")'
echo $oldfile | sed 's/.xlwa/.xls/g'
++ echo Email-en-us-05312012.xlwa
++ sed s/.xlwa/.xls/g
+ newfile=Email-en-us-05312012.xls
+ mv Email-en-us-05312012.xlwa Email-en-us-05312012.xls
mv: cannot stat `Email-en-us-05312012.xlwa': No such file or directory

So whenever there is a space in the filename, it is taking as a different file..

Could somebody help me out here..

Cheers!!!!!
# 2  
Old 06-13-2012
Code:
find /usr/local/data/vstar/report_repository -name "*.xlwa"|while read a
do
 mv "$a" "${a%xlwa}"xls
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename files repeats previous filename in directory

In the below bash processes substitution, if there are 3 files in a directory /home/cmccabe/medex.logs/analysis.log, the filename variable is set to where these files are located. The code does execute, the problem is that if there is a renamed file in the output directory below, it gets... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Creating Multiple data files with spaces in the filename

Hi, I have a list of filenames in the format with Spaces in the filename. As an example : Sample File 1.txt Sample File 2.txt Sample File 3.txt.....I have about 100 files like this. I am trying to create a block of code or use an available command to a) Create a file b) Put in some... (2 Replies)
Discussion started by: ban3rj33
2 Replies

3. Red Hat

How to rename files to files with mv extension?

currently in my directories $ ls -lrth total 32K -rw-r--r-- 1 oracle oinstall 864 Feb 25 16:01 cor_bin_gateway_cnt.sql -rw-r--r-- 1 oracle oinstall 782 Feb 25 16:01 mer_bin_gateway_cnt.sql I want to rename files with *.sql to *.mv extension, but when I execute the following $ mv... (1 Reply)
Discussion started by: jediwannabe
1 Replies

4. UNIX for Dummies Questions & Answers

Rename multiple files, changing prefix, extension and dropping characters

I'm currently only able to perform some very basic functions, so hope this makes sense... I have a set of about 27 files that need to be renamed from something like this: 000012ABCDEFGHIJ.XXX.YYY.ZZZ 000078KLMNO.XXX.YYY.ZZZ 000099PQ.XXX.YYY.ZZZ to something like this: newa012.abc... (11 Replies)
Discussion started by: bbmcg
11 Replies

5. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

6. Shell Programming and Scripting

ksh command to rename all files with no extension

hi! i want to rename all files with no extension with the extension DAT. with this command ls |grep -v "\\." i can list files but i dont know how i am going to rename them.. so i tried FILE_LIST=ls |grep -v "\\." for TEST_FILE in ${FILE_LIST} do mv $TEST_FILE... (2 Replies)
Discussion started by: kouppoua
2 Replies

7. Shell Programming and Scripting

List files with spaces in filename

I have a text file containing files in a directory structure i.e. /project/hr/raw/jcpdatav/datav_aug03 /project/hr/raw/jcpdatav/comb8121sep02n /project/hr/raw/jcpdatav/datav_feb04_ons /project/hr/raw/jcpdatav/corpsick_jun06 /project/hr/raw/jcpdatav/jcpjoiners200507... (3 Replies)
Discussion started by: mr_crosby
3 Replies

8. Shell Programming and Scripting

copying files with spaces in the filename

Hi I am having difficulty copying files from one dir to another due to a space in the names of the file with an extension .rtf There are a group of files and the command am using is cp `ls -rt /wlblive/home/whiops/ops/RTFs/*.rtf|head -20` /wlblive/home/jamshed Since the files are... (3 Replies)
Discussion started by: jamshedj
3 Replies

9. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

10. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies
Login or Register to Ask a Question