How to rename all the files in a folder having specific
extension?
Example: I have some files with extension (.txt) in a folder
name 'Test'. I have to rename all the .txt files in a test
and its subdirectories to .my extension.

Answers were Sorted based on User's Feedback



How to rename all the files in a folder having specific extension? Example: I have some files with..

Answer / ramit

Hi Nagesh,

Thanks for the reply but I dont think this will work
as $file= myFile.txt
and mv $file $file.my will rename to myFile.txt.my

I found the solution by extracting basedirectory and
filename then renaming it to req. extension.

thanks

Is This Answer Correct ?    2 Yes 4 No

How to rename all the files in a folder having specific extension? Example: I have some files with..

Answer / vipin

The script given below may work properly.
find command is used to search in all the subdirectories.
basename will extract only filename with .txt extension from
absolute path of file.
cut command will print opnly filename without extension.
mv command is uset to rename.

#!/bin/bash
for i in $(find -name \*.txt)
do
mv $i $(echo `basename $i` | cut -d . -f 1).my
done

Is This Answer Correct ?    0 Yes 2 No

How to rename all the files in a folder having specific extension? Example: I have some files with..

Answer / bindu

its very simple
try this out
rename .txt .my *.txt
all the files with .txt extension will be converted into .my

Is This Answer Correct ?    0 Yes 2 No

How to rename all the files in a folder having specific extension? Example: I have some files with..

Answer / nagesh

for file in *.txt
do
mv $file $file.my
done

Is This Answer Correct ?    5 Yes 10 No

How to rename all the files in a folder having specific extension? Example: I have some files with..

Answer / jks

It can be done using the xargs command of Unix ...
cd to the dir containing the *.txt files

$ls *.txt | xargs -i mv \{\} \{\}.my

Is This Answer Correct ?    5 Yes 15 No

Post New Answer

More Shell Script Interview Questions

What is web script?

0 Answers  


Is shell scripting a language?

0 Answers  


c program to display the information of given file similar to givan by the unix or linux command ls -l

0 Answers   IBM,


Explain about return code?

0 Answers  


Why do we use shell scripting?

0 Answers  






How will you schedule a job that will run every month last day?(some months have 30 days,some 31 days,28,29 days)

1 Answers   NTT Data, TCS,


Explain about the exit command?

0 Answers  


what is the difference between writing code in shell and editor?

1 Answers  


How does shell scripting work?

0 Answers  


How to declare functions in unix shell script?

4 Answers  


What is the difference between a variable and value?

7 Answers   Sun Microsystems,


How many fields are present in a crontab file and what does each field specify?

0 Answers  


Categories