Shell Script to Add File Extension

Write a small shell script that adds an extension ".new" to all the files in a directory.

Questions by ayongying

Showing Answers 1 - 35 of 35 Answers

ak.alok86

  • May 31st, 2010
 

Please use the following script to change the files in a directory to .new extension

The directory name should be provided as an argument

dir=$1
for file in `ls $1/*`
do
mv $file $file.new
done

abdas

  • Jun 26th, 2011
 

for file in /dxusers/testing/test_delete/*
do
 if [ -f $file ]
 then
  mv $file $file.new
 fi
done

  Was this answer useful?  Yes

Mo

  • Apr 20th, 2012
 

path=${1?pls provide the path to script}
cd $path
for i in *
do
mv $i ${i}.new
if [ $? -eq 0 ]
then
echo "file rename successful"
ls -l ${i}*
fi
done

  Was this answer useful?  Yes

Jessmon

  • Jun 1st, 2012
 

Code
  1. for i in *;

  2. do mv $i $i.New;

  3. done

  Was this answer useful?  Yes

gururaj

  • Jun 5th, 2012
 

ls -lrt | awk {priint "mv "$9" "%9".New"} | sh

  Was this answer useful?  Yes

Nirmal Acharya

  • May 11th, 2014
 

while [ ! -z "$1" ]
do
mv "$1" "$1.new"
done

  Was this answer useful?  Yes

Krishan

  • Apr 4th, 2016
 

find . -name *.* -exec mv {} {}.xml ;

  Was this answer useful?  Yes

Keerthiga

  • Sep 9th, 2016
 

find . -name "filename" -exec sh -c mv $1 $1.new _ {} ;

  Was this answer useful?  Yes

rajat

  • Sep 30th, 2016
 

for i in ` ls -1 | grep -v txt.sh`
do
echo $i
mv $i $i".txt"
done

  Was this answer useful?  Yes

Nawaz

  • Oct 25th, 2016
 

For item in `ls`; do mv $item ${item}.new; done

Code
  1. for item in `ls`; do mv $item ${item}.new; done

  Was this answer useful?  Yes

Nawaz

  • Jun 9th, 2017
 

for f in *; do mv $f ${f}.new; done

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions