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
Answer / manuswami
for file in `ls` ; do NEW=`echo $file | sed 's/.txt/.my/g'`
; mv $file $NEW ; done
Is This Answer Correct ? | 10 Yes | 0 No |
Answer / akshay telang
for i in `find . -name "*.txt"`
> do
> mv $i ${i%.txt}.my
> done
Is This Answer Correct ? | 11 Yes | 4 No |
Answer / prav gir
#!/bin/sh
for filename in *$1*
do
echo "$filename"
mv -f "$filename" `echo $filename|sed "s/$1/$2/"`
done
run script like
$>scrpt1 .txt .my
Is This Answer Correct ? | 3 Yes | 3 No |
Answer / geichel
#!/bin/bash
for x in $(find $1 -name '*.txt' -type f )
do
OUT=$(echo $x | sed -e "s/\.txt$/.my/")
mv $x $OUT
done
exit 0;
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / venugopal adep
for file in `ls *.txt`; do new_file=`echo $file | sed
's/txt/my/g'`; mv $file $new_file; done
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / indu sharma
find . -name "*.txt" -exec ls {} \; | xargs -n1 -I{} sh -c 'mv "{}" `echo "{}"|basename "{}" .txt`.my'
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / sushanta
for i in *
do
p=`basename $i c`
q=$p "txt"
mv $i $q
done
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / veera
rename replacing-text replaced-test list-of-files
ex: rename .txt .my *.txt
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / aravind s
basename is the appropriate command to cut the unwanted
string from a filename
for example
$basename unix.txt txt #txt stripped off
unix.
-----------------------
for your problem the solution is
for i in `find . -name '*.txt'`|xargs ls; do
leftname=`basename $i txt`
mv $i ${leftname}doc
done
-----------------------
basename isn't a shell bulletin, but an external command.
The reason why we deal with it here is that it's most
effective when used with the for loop.
Ref: Unix Concepts & Applications - Sumitabha Das
Is This Answer Correct ? | 2 Yes | 3 No |
why did you apply to shell
What does $1 mean in bash?
What is the command to find out today's date?
write a shell script to identify the given string is palindrome or not?
17 Answers CTS, HP, IBM, InfoEst, Wipro,
is this growing field and what is average package in this?
Why is shell scripting important?
How to know that your remote server is ruing smoothly or not in unix?
c program which behaves like a shell(command interpreter). it has its own prompt say "NewShell$".any normal shell command is executed from your shell by starting a child process to execute a system program corrosponding to the command
How Connect to a Database in Shell Programming?
What does echo $0 do?
What does $$ mean in shell script?
what is the meaning of First line of shell script ,what is meaning of #! pleas explain brifly