Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-12-2012
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
Script to change file name

Hi, I need to write a script that changes the name of the filename. Let's say my script is named change_filename and I want to use it on a file named test1.txt. After running the script I want the filename renamed to test1_fails.txt

e.g.


Code:
$   ls

test1.txt   test2.txt   test3.txt

$   change_filename test1.txt
$   ls

test1_fails.txt   test2.txt   test3.txt


I'm thinking maybe the sed command and to substitute the filename but I don't know how to do it.

Moderator's Comments:
Please use code tags, thanks!

Last edited by zaxxon; 06-12-2012 at 08:10 AM.. Reason: code tags
Sponsored Links
    #2  
Old 06-12-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,574
Thanks: 15
Thanked 441 Times in 426 Posts
at command line: mv test1.txt test1_fails.txt
Sponsored Links
    #3  
Old 06-12-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
bash

Hi,

Try this one,

Code:
#! /usr/bin/bash
base=$1
if [ -f ${base} ];
then
    mv ${base} ${base/./_fails.}
else
    echo "${base} file not found"
fi

Cheers,
Ranga
The Following User Says Thank You to rangarasan For This Useful Post:
millsy5 (06-12-2012)
    #4  
Old 06-12-2012
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
Hi Rangarasan, thanks for your reply. However it is not working for me. I'm new to Unix programming so maybe it is something simple I have not spotted. I ran the following code:


Code:
#! /bin/csh

base=$1

if [ -f ${base} ]
then
   cp ${base} ${base././_fails.}
else
   echo "${base} file not found"
fi

I get the following output


Code:
$ change_filename test1.txt
base=test1.txt: Command not found
base: Undefined variable

The file test1.txt is definitely in the current directory
Sponsored Links
    #5  
Old 06-12-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
Quote:
Originally Posted by millsy5 View Post
Hi Rangarasan, thanks for your reply. However it is not working for me. I'm new to Unix programming so maybe it is something simple I have not spotted. I ran the following code:


Code:
 
#! /bin/csh
 
base=$1
 
if [ -f ${base} ]
then
   cp ${base} ${base././_fails.}
else
   echo "${base} file not found"
fi

I get the following output


Code:
 
$ change_filename test1.txt
base=test1.txt: Command not found
base: Undefined variable

The file test1.txt is definitely in the current directory
There is an extra dot in the substitution part.
Use the below code

Code:
#! /bin/csh

base=$1

if [ -f ${base} ]
then
cp ${base} ${base/./_fails.}
else
echo "${base} file not found"
fi

Cheers,
Ranga
Sponsored Links
    #6  
Old 06-12-2012
Registered User
 
Join Date: Oct 2011
Posts: 41
Thanks: 16
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rangarasan View Post
There is an extra dot in the substitution part.
Use the below code

Code:
#! /bin/csh

base=$1

if [ -f ${base} ]
then
cp ${base} ${base/./_fails.}
else
echo "${base} file not found"
fi

Cheers,
Ranga
That extra dot was actually a typo. I did not have that in my actual script.

Do I need to declare variables somewhere? I don't know why it thinks base is a command and not a variable. I've tried renaming base but I still get the same error.
Sponsored Links
    #7  
Old 06-12-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
bash

The above script will work with bash. But you have used csh.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Change XML file structure script cgkmal Shell Programming and Scripting 10 10-11-2011 09:18 PM
change file contents using script Sanal Shell Programming and Scripting 1 09-01-2011 04:17 PM
Need help script to change the log file? justbow Shell Programming and Scripting 5 08-11-2009 01:01 PM
script to ftp file (ip change) happyv Shell Programming and Scripting 2 09-19-2006 03:50 AM
script to change value in file gopskrish UNIX for Dummies Questions & Answers 1 06-22-2005 07:46 AM



All times are GMT -4. The time now is 07:49 AM.