# Script for renaming multiple scripts

**URL:** <https://community.unix.com/t/script-for-renaming-multiple-scripts/333778>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 31, 2013, 7:34am UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778 "2013-08-31T07:34:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![orangepeel](https://community.unix.com/letter_avatar/orangepeel/32/6_449c533f586939b6e5f246632a46421f.png) [@orangepeel](https://community.unix.com/u/orangepeel)\
**Post date:** [August 31, 2013, 7:34am UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/1 "2013-08-31T07:34:01Z")

</div>

Hi There

I am looking at writing a script that I can run from a cron job (say every month) that will copy my existing scripts to another folder, then in THAT new folder, append them all with the extension of .txt

I have the cp command down pat (cp /existing\_folder /new\_folder)

however I am stuck on the rename part.

All of the searching that I have found has examples of renaming files with an extension on them (rename file1.bak file2.txt)

what command can I use to rename all files in the new\_folder that DO NOT have an extension on them to a file with the same file name but ending with .txt. I have files simply called news (no extension) and I am hoping to rename them (via a script) from news to news.txt

The reason for the renaming is to make it easier to share them via the web (for reading and editing off-site)

Some files have existing extensions (file.conf or file.sh) however the majority of my scripts do not have an extension on the end of the file....

I hope this makes sense - I have searched this pretty hard and tried many ideas, however nothing seems to work

Many thanks for taking the time to read my question

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [August 31, 2013, 12:46pm UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/2 "2013-08-31T12:46:24Z")

</div>

You can either use `test` or `[` with an appropriate pattern to find out which files don't have a .txt extension, or you might use recent bash's extglob option to exploit the extended pattern matching.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [August 31, 2013, 2:08pm UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/3 "2013-08-31T14:08:56Z")

</div>

Posix shell, ksh, bash:

```nohighlight
for filename in test.sh test
do
  basename=${filename%%.*}
  extension=${filename#$basename}
  newname=${basename}${extension:-.txt}
  echo "$filename -> $newname"
done

```

---

<div class="post-metadata">

**Author:** ![spacebar](https://community.unix.com/user_avatar/community.unix.com/spacebar/32/1679_2.png) [@spacebar](https://community.unix.com/u/spacebar)\
**Post date:** [August 31, 2013, 2:16pm UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/4 "2013-08-31T14:16:25Z")

</div>

This is one way to copy the files from one directory to another adding '.txt' as extension if file has no extension:

```nohighlight
SOURCE_DIR="$HOME/tmp/dir1"
TARGET_DIR="$HOME/tmp/dir2"

for SF in $SOURCE_DIR/*
do
  case `basename "$SF"` in
    *.* )  
      # Has an extension
      TF=$SF
      ;;
  * )
      # Does not have an extension
      TF="$SF.txt"
      ;;
  esac

  cp $SOURCE_DIR/$SF $TARGET_DIR/$TF
  if [[$? -eq 0]]; then
    echo "Successfully copied $SOURCE_DIR/$SF to $TARGET_DIR/$TF"
  else
    echo "$? : Error occured coping $SOURCE_DIR/$SF to $TARGET_DIR/$TF"
  fi
done

```

---

<div class="post-metadata">

**Author:** ![orangepeel](https://community.unix.com/letter_avatar/orangepeel/32/6_449c533f586939b6e5f246632a46421f.png) [@orangepeel](https://community.unix.com/u/orangepeel)\
**Post date:** [September 1, 2013, 5:55am UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/5 "2013-09-01T05:55:26Z")

</div>

Many thanks for your replies

I have tried "spacebars" script however it is giving out the following error:

> [@](#):
>
> cp: cannot stat `/home/irlp/webstuff//home/irlp/webstuff/setMonDate': No such file or directory  
> 1 : Error occured coping /home/irlp/webstuff//home/irlp/webstuff/setMonDate to /home/irlp/webstuff/trial//home/irlp/webstuff/setMonDate.txt

I have set the following parameters:

```plaintext
SOURCE_DIR="/home/irlp/webstuff"
TARGET_DIR="/home/irlp/webstuff/trial"

```

It appears that the script is not copying the files over and appending the .txt extension on the newly copied files into the target directory

Am I on the right path with this or something astray?

---

<div class="post-metadata">

**Author:** ![spacebar](https://community.unix.com/user_avatar/community.unix.com/spacebar/32/1679_2.png) [@spacebar](https://community.unix.com/u/spacebar)\
**Post date:** [September 1, 2013, 11:02pm UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/6 "2013-09-01T23:02:21Z")

</div>

Change the first 4 lines to this:

```nohighlight
SOURCE_DIR="/home/irlp/webstuff"
TARGET_DIR="/home/irlp/webstuff/trial"
cd $SOURCE_DIR

for SF in *

```

---

<div class="post-metadata">

**Author:** ![orangepeel](https://community.unix.com/letter_avatar/orangepeel/32/6_449c533f586939b6e5f246632a46421f.png) [@orangepeel](https://community.unix.com/u/orangepeel)\
**Post date:** [September 2, 2013, 5:40am UTC](https://community.unix.com/t/script-for-renaming-multiple-scripts/333778/7 "2013-09-02T05:40:05Z")

</div>

Many thanks for the help. All sorted now 🙂
