Replace file name extension in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace file name extension in loop
# 1  
Old 03-31-2011
Replace file name extension in loop

Hi!

I have this shell script that I need to finish. Currently I need to fix one line to make it work. I need to change a file extension. See this code, is pretty simple.

Code:
#!/bin/sh

# Extensions
OLD_EXT=.flv
NEW_EXT=.mp4

COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l)

if [ $COUNT_FILES -gt 0 ]; then

for f in *$OLD_EXT; do

$NEW_FILE=$(sed 's/$OLD_EXT/$NEW_EXT/g' $f)

echo "Old File: " $f
echo "New File: " $NEW_FILE

done

fi

I need to fix the line:
Code:
$NEW_FILE=$(sed 's/$OLD_EXT/$NEW_EXT/g' $f)

What I need in that line is that the variable $NEW_FILE is the value of $f, but replaced the file extension.

Example:
20110318175645.test_file_name.flv
will become:
20110318175645.test_file_name.mp4

I don't want to change the filename of the physical file, just the variable name for screen output in the loop.

But that is not working with that code. How is the correct code?

Thanks!
# 2  
Old 03-31-2011
Change...
Code:
$NEW_FILE=$(sed 's/$OLD_EXT/$NEW_EXT/g' $f)

...to...
Code:
NEW_FILE=$(echo $f | sed "s/$OLD_EXT/$NEW_EXT/g")

---------- Post updated at 09:56 AM ---------- Previous update was at 09:54 AM ----------

Or...
Code:
NEW_FILE=${f%$OLD_EXT}$NEW_EXT

This User Gave Thanks to Ygor For This Post:
# 3  
Old 03-31-2011
The code
Code:
NEW_FILE=${f%$OLD_EXT}$NEW_EXT

Did the trick... thank you very much! Smilie
# 4  
Old 03-31-2011
your code
Code:
COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l)

if [ $COUNT_FILES -gt 0 ]; then

if redundant. If your for loop does not find your files, then it won't process. So no need to count at all.
# 5  
Old 03-31-2011
Quote:
Originally Posted by kurumi
your code
Code:
COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l)

if [ $COUNT_FILES -gt 0 ]; then

if redundant. If your for loop does not find your files, then it won't process. So no need to count at all.
Hi,

I'm not sure I understand.

What changes do you suggest to the script?

Thanks
# 6  
Old 03-31-2011
Just
Code:
for f in *$OLD_EXT; do
  ....
  echo "Old File: " $f
  echo "New File: " $NEW_FILE
  .....
done

This User Gave Thanks to kurumi For This Post:
# 7  
Old 03-31-2011
If no files match $file will be "*.flv"

This will be better (also weeds out any directories or special files with a .flv suffix):

Code:
for f in *$OLD_EXT; do
  [ -f "$file" ] || continue
  ....
  echo "Old File: " $f
  echo "New File: " $NEW_FILE
  .....
done

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find command + replace the extension (.xxx) by *

Hello, I'm on HP Unix and in a Job, I tried to extract all files from a folder, and replace the extension (.xxxx) by '*' , remove duplicates and move the result in a file.. Example : Folder has : ABC, CCC.txt, CCC.sf, CCC.sfd, DDD I need to generate and output file with : ... (6 Replies)
Discussion started by: royinfo.alain
6 Replies

2. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Remove extension in loop

Dear all sorry for bothering you wityh dumb question but I am stucked with an issue. Well, I am trying to loop over files in folder, make an operation and rewrite the output. Example: INPUT file1.txt file2.txt file3.txtMy command (doesn't work!!) for file in /path/to/*.txt do command... (13 Replies)
Discussion started by: giuliangiuseppe
13 Replies

4. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

5. Shell Programming and Scripting

Loop through file and replace with sed

Hello all, I need some help please. I got file1 with names. foo bar foo bar foo bar foo bar foo bar and I got file2 with some text some text some text #KEYWORD some text some text some text (3 Replies)
Discussion started by: stinkefisch
3 Replies

6. Shell Programming and Scripting

[Solved] Replace extension of filename stored in a variable

Hi there, I have a small question (most like a true beginners question :) ). In a script I have a filename stored in variable (vFile). Through the an input parameter this variable gets its value (for instance cookie.txt). Two new variables are created with the value of vFile, but with a... (2 Replies)
Discussion started by: rberkers
2 Replies

7. Shell Programming and Scripting

Passinng specific file extension to while loop in ksh

hello, i have the below while loop wherein i am passig list of filenames to be scped. this is in unix ksh - filenamelist.txt has list of files names, including .dat and .txt files but i want to pass only the .txt filenames to the while loop so that only .txt files gets scped. how can... (4 Replies)
Discussion started by: billpeter3010
4 Replies

8. Shell Programming and Scripting

for loop other file with same name and different extension

Hi Friends, I am using the following command for i in `ls $PWD`; do cat $i > test && mv test $i; done But, I want to execute another command and write the output to another file with different extension but same name, like this I tried using this for i in `ls $PWD`; do... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

9. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

10. Shell Programming and Scripting

replace *.sqc files with *.sqC extension

Hi everyone, I want to know how to replace file extensions. For example how do i go about replacing all *.c files in a directory to *.cpp I think we can do it either with "find" commad or with special registers. I tried to use the following command mv \(.*\).c \1.cpp but it is giving... (7 Replies)
Discussion started by: rameshonline
7 Replies
Login or Register to Ask a Question