Questions regarding script moving of dated fles.

Latest response

Good Day,

I have previous written a script that allowed me to do clean up of my /var/log/messages file. The Script provides the ip address of each machine and catted the message file before it was cleaned and rotated. The file contents of the log file for each of the 69 clients as backup and lists the file as ip address.txt. This is working as I wanted.

What i want to do now is append the date that the script was run to the front of the file keeping the ip address also. This is the code I found for that.

! /bin/bash

find . -maxdepth 1 -type f | \
cut -c3- | \
grep -v '^.' | \
grep -v '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | \
xargs -n 1 -I {} echo mv "{}" "date +"%Y-%m-%d"
mkdir date +%Y_%m_%d

I would like to create a directory for that date and move all the files to that directory. Also I would like this to happen each time the script is run.

Is there a quick way to do this? I am a newbie so shell scripting is starting to be something I will b doing a lot of so any pointers you have will be much appreciated.




Hi John,

First create and test the variable, below is an example. The block below is created with three "~" characters and then the code, with three more "~" characters

mydate=`date '+%Y%m%d_%H%M%S'`
echo $mydate

You can adjust the variable with the specifics you want, use man date to see all of them, I susepct you're acquainted with that.

Below are some possible methods to set the variables you want...

mydate=`date '+%Y%m%d_%H%M%S'`
## Get the ip address, now if you have more than one primary IP address, you might have to be creative
myip="$(hostname -I awk '{print $1}')"
# important note, you might have to do some additional work if you have more than one IP address, or have docker running
# make a variable for the parent directory of the destination you wish to send it to.  
# We'll assume an nfs-mounted /data/servermessagelog/ which already exists before the script is ran, with proper permissions.
/bin/mkdir $target/$mydate

# note this is not a complete script but just has some excerpts based on the original post

I have a perl script I copy off audit logs to a directory where things go to tape. I could share that method in principle at a later time.

Hope this helps.


Thanks for the input. Much appreciated...