Help - Search - Members - Calendar
Full Version: Copy Or Rename Script
Linuxhelp > Support > Technical Support
ishba
Hello I am rather new to Linux and I need a script or utility to copy or rename a file that changes frequently to succesive file names.. that maybe i can call from cron to be done at certain times
As an example I have file list.xxx and want it copied or renamed to list-1.xxx
and next time it is copied to check to see what last name is and name it next higher number in succession.. ie list-2.xxx and list-3.xxx

Am I asking for too much? sounds pretty simple just not familiar enough with linux script files.

Maybe it doesnt have to be a script can i add options to end of a command like cp that will handle this for me?
hughesjr
A script is the way to go ... it can be done several ways. First, would a date-time e better than a number? If you are going to run the script once per day or less, you could use the date like so list-11-21-2003.xxx list-11-22-2003.xxx (or maybe list-11-22-2003-1143.xxx) Either is doable ... what's best for you?

Here is a simple script i named test.sh

CODE
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# This does the math for the filename and stores the result
# for future use in a file called test.num.  This file is
# designed to be in the directory the other files reside in
#
if [ -f test.num ]; then
 a=`cat test.num`
else
 a=0
fi

let "a = $a + 1"
echo $a > test.num

# This copies the file list.xxx to list-#.xxx when you run
# the script.  If you also want to remove the old list.xxx
# and start with a new file, remove the # from the last line

cp list.xxx list-"$a".xxx
#echo > list.xxx


If you save this script to a text file and make it executable (chmod 755 test.sh), it will copy a file named list.xxx to list-1.xxx (the next time it is run it will copy list.xxx to list-2.xxx, etc.) and if you remove the # from the last line it will blankout list.xxx to start it over from a blank file.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.