# rename files using shell scripting

**URL:** <https://community.unix.com/t/rename-files-using-shell-scripting/163022>\
**Category:** Shell Programming and Scripting\
**Created:** [July 4, 2006, 2:02pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022 "2006-07-04T14:02:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gfhgfnhhn](https://community.unix.com/letter_avatar/gfhgfnhhn/32/6_22734026db12803ebb3e059622ac3534.png) [@gfhgfnhhn](https://community.unix.com/u/gfhgfnhhn)\
**Post date:** [July 4, 2006, 2:02pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022/1 "2006-07-04T14:02:46Z")

</div>

Hi all,

i want to rename some files in my directory  
using korn shell scripting.

1) i want to rename files who have no extension so that they  
will have the format: filename.extension

and

2) i want the files who has extension initially, to stay the same  
(they will not be changed)

do you have any idea about how to do this using ksh scripting?  
(without using sed or awk)

with regards

---

<div class="post-metadata">

**Author:** ![Hitori](https://community.unix.com/user_avatar/community.unix.com/hitori/32/394_2.png) [@Hitori](https://community.unix.com/u/Hitori)\
**Post date:** [July 4, 2006, 3:37pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022/2 "2006-07-04T15:37:30Z")

</div>

$ cd where you want  
$ find -maxdepth 1 -type f -regex "^.\*/[^\.]\*$" -exec mv {} {}.extension \;

---

<div class="post-metadata">

**Author:** ![gfhgfnhhn](https://community.unix.com/letter_avatar/gfhgfnhhn/32/6_22734026db12803ebb3e059622ac3534.png) [@gfhgfnhhn](https://community.unix.com/u/gfhgfnhhn)\
**Post date:** [July 4, 2006, 3:58pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022/3 "2006-07-04T15:58:53Z")

</div>

thank you very much  
but i still need the ksh solution,  
because i also want to do some operation on the files  
other than renaming them.  
i want to pass the "filenames with no extension" to a ksh variable  
do some operation (including rename)

for example:  
i have code to process files which have format like: filename.ext

for file in \*.ext; do  
#some commands  
done

but i want to modify the code lines above as to  
process files which have no extension (format like: filename ), while the files which have name format like filename.ext will not be affected

---

<div class="post-metadata">

**Author:** ![Hitori](https://community.unix.com/user_avatar/community.unix.com/hitori/32/394_2.png) [@Hitori](https://community.unix.com/u/Hitori)\
**Post date:** [July 4, 2006, 4:15pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022/4 "2006-07-04T16:15:07Z")

</div>

You can use  
for file in `find ...` (without -exec)

But you'll have problems with files with spaces, see [http://www.unix.com/showthread.php?t=28876](http://www.unix.com/showthread.php?t=28876)

---

<div class="post-metadata">

**Author:** ![Hitori](https://community.unix.com/user_avatar/community.unix.com/hitori/32/394_2.png) [@Hitori](https://community.unix.com/u/Hitori)\
**Post date:** [July 4, 2006, 4:37pm UTC](https://community.unix.com/t/rename-files-using-shell-scripting/163022/5 "2006-07-04T16:37:55Z")

</div>

```nohighlight
#!/bin/sh

find -maxdepth 1 -type f -regex "^.*/[^\.]*$" | while IFS= read vo
do
mv "$vo" "$vo".ext
other commands
done

```

Try #!/bin/ksh
