Rename a log file to old, then create a new log file.

Hi all,

I have about 15 directories all with exactly the same structure. I have a file in each of them called log.txt. This file sits in /home/ftp/*usernamehere*/ftptransfer/log/

Username here is the only change in each of the 15 directories.

I want to create a SIMPLE shell script that go's into each directory and renames the log.txt files to log.txt.old, and then recreates log.txt (would need to remove the log.txt.old when it runs for the second time.

I kind of had an attempt but ran into a few issues... Heres my code :frowning:

#!/bin/sh
 
for i in /home/ftp/*/ftptransfer/log/; do    
if  [ "$i" != "log.txt" ]; 
 then  
 rm log.txt.old
 mv log.txt log.txt.old
 touch log.txt    
 fi
 done 
 
for i in /home/ftp/*/ftptransfer/log/
do
  if [ -f "$i/log.txt.old" ]
  then
    mv -f log.txt log.txt.old
  fi
  touch log.txt
done

Thanks alot for your reply cfajohnson but it doesnt seem to work?

What does "doesnt seem to work" mean?

What does happen?

Do you get any error messages?

Sorry that was a bit vague, it simply creates a log.txt file in the directory i run the script from (which is in my home directory away from the accounts we are attempting to rename the log files).

The script should have read:

for i in /home/ftp/*/ftptransfer/log/
do
  if [ -f "$i/log.txt.old" ]
  then
    mv -f "$i/log.txt" "$i/log.txt.old"
  fi
  touch "$i/log.txt"
done

Or:

for i in /home/ftp/*/ftptransfer/log/
do
   (
     cd "$i"
     if [ -f log.txt.old ]
     then
         mv -f log.txt log.txt.old
     fi
     touch log.txt
    )
done