|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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.
Last edited by zaxxon; 06-12-2012 at 08:10 AM.. Reason: code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
at command line:
mv test1.txt test1_fails.txt
|
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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"
fiCheers, Ranga
|
| The Following User Says Thank You to rangarasan For This Useful Post: | ||
millsy5 (06-12-2012) | ||
|
#4
|
|||
|
|||
|
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"
fiI 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
|
||||
|
||||
|
Quote:
Use the below code Code:
#! /bin/csh
base=$1
if [ -f ${base} ]
then
cp ${base} ${base/./_fails.}
else
echo "${base} file not found"
fiCheers, Ranga
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
bash
The above script will work with bash. But you have used csh.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|