Script for renaming multiple scripts


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script for renaming multiple scripts
# 1  
Old 08-31-2013
Script for renaming multiple scripts

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
# 2  
Old 08-31-2013
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.
# 3  
Old 08-31-2013
Posix shell, ksh, bash:
Code:
for filename in test.sh test
do
  basename=${filename%%.*}
  extension=${filename#$basename}
  newname=${basename}${extension:-.txt}
  echo "$filename -> $newname"
done


Last edited by MadeInGermany; 08-31-2013 at 03:14 PM..
# 4  
Old 08-31-2013
This is one way to copy the files from one directory to another adding '.txt' as extension if file has no extension:
Code:
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


Last edited by Scott; 08-31-2013 at 03:51 PM.. Reason: Removed FONT tags from code again
# 5  
Old 09-01-2013
Many thanks for your replies

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

Quote:
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:
Code:
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?
# 6  
Old 09-02-2013
Change the first 4 lines to this:
Code:
SOURCE_DIR="/home/irlp/webstuff"
TARGET_DIR="/home/irlp/webstuff/trial"
cd $SOURCE_DIR

for SF in *

# 7  
Old 09-02-2013
Many thanks for the help. All sorted now Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script calling multiple scripts (using lock file)

Hi all, i have compiled a script as below. Basically this script is intended to call 3 other scripts, which i intend to run simultaneously. #!/usr/bin/bash HOMEDIR=/path/of/home LOCKDIR=$HOMEDIR/lock #check for LOCK LOCKFILE=$LOCKDIR/$(basename $0 .sh).lock if ; then echo... (2 Replies)
Discussion started by: nms
2 Replies

2. Shell Programming and Scripting

Multiple scripts in one single script - crontab

Hello all, Hope all's well. i'm not sure if this is possible but i have some scripts running in a crontab with different intervals, 1min, 5 min, etc. As for a "cleaner" and better control of these scripts (as by time we will have hundred's of scripts used for the same purpose, i.e for Nagios... (4 Replies)
Discussion started by: nms
4 Replies

3. UNIX for Beginners Questions & Answers

A single script to run multiple scripts

Hi , Can someone help! I need a shell script to run multiple scripts by using single shell script, incase any one of the scripts fails, it should get exit and after trouble shooting if we re-execute it, it should start from the failed script. I have a written a scripting till the... (1 Reply)
Discussion started by: anniesurolyn
1 Replies

4. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (6 Replies)
Discussion started by: Madhu Siddula
6 Replies

5. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (1 Reply)
Discussion started by: Madhu Siddula
1 Replies

6. Shell Programming and Scripting

Local script to trigger multiple remote scripts

Hi All, I am facing problem running a script which triggers another script in multiple remote servers. my script in my local server looks like below ssh server1 "sudo -u uname /usr/local/script/start.sh &2>&1 >/dev/null " ssh server2 "sudo -u uname /usr/local/script/start.sh &2>&1 >/dev/null "... (7 Replies)
Discussion started by: sain
7 Replies

7. Shell Programming and Scripting

Script to execute multiple scripts

Hi All, I have 4 scripts to execute. Each one take anywhere from 3 -9 hours to run depending on what it's doing. I'm running one at a time then check back periodically to see if it's still going and if not, run the other. How can I put these scripts into another script so that they are... (5 Replies)
Discussion started by: bbbngowc
5 Replies

8. Shell Programming and Scripting

Renaming multiple files with a shell script

Hey guys, I'm really new to UNIX and shell scripting in general. For my internship I need to rename a bunch of files. Specifically, I need to change the first letter of each of the files to lowercase and I have to change the endings so they all basically look like "file_cone.jpg". I know I... (4 Replies)
Discussion started by: jjzieve
4 Replies

9. UNIX for Dummies Questions & Answers

Script Problem-Processing multiple scripts

Hi All, I have four scripts to run. My 1st script will make script2 and script3 to run. I am setting a cron job for this script1 to run continuously. This script1 will check for 2 text files and based on the existance of those text files it will initiate the script2 and script3. Now my doubt... (2 Replies)
Discussion started by: RSC1985
2 Replies

10. UNIX for Dummies Questions & Answers

Sh script to run multiple php scripts

I wrote a .sh script to run 5 php scripts. The problem is that it's running 1 then 2 then 3 in that order .... I want it to execute all 5 at ONCE.... nohup php /home/script1/script1.php && nohup php /home/script2/script2.php && nohup php /home/script3/script3.php && nohup php... (1 Reply)
Discussion started by: holyearth
1 Replies
Login or Register to Ask a Question