I am currently working on script with the following goals:
- Script will check every 15-minute if there is
AppA.log4
in/logs
directory. There are numerous apps in/logs
directory, for exampleAppA.log4
,AppB.log4
,AppC.log4
- If there is
AppA.log4
, it will be renamed toAppA.log.<timestamp>
zip
the renamed file and move to/backupDIR
So far, I have created working script however it is hardcoded.
Can you help improving this to be more dynamic? For example, AppA.log4
and AppB.log4
will be moved and zipped, leaving AppC.log4
in the /logs
directory.
What if there's a separate file that lists all the App (AppA
and AppB
) that will be read by this script. How can I apply it here. Thank you in advance.
TIMESTAMP=`date "+%Y.%m.%d-%H.%M"`
LOGS="AppA.log.4"
APPLOGS_DIR="/logs/"
BACKUP_DIR="/backupDIR/"
cd $APPLOGS_DIR;
if [ -f "$LOGS" ];
then
mv $LOGS $BACKUP_DIR;
cd $BACKUP_DIR;
mv $LOGS AppA_$TIMESTAMP;
gzip AppA_$TIMESTAMP;
else
echo "No log file to be backed up"
fi
ls | find *.log4 | sed s/log4/log
to dynamically use the names (also it will rename log4 to log) – theSwapnilSaste Jul 17 '20 at 07:00What I mean is, my goal now is to move and gzip AppA.log4 and AppB.log4. However there is still AppC.log4 in the /logs/ directory. I just want to specify AppA and AppB. I am thinking of another file where I can input AppA and AppB. The script will read this, but not yet sure on how can I implement it.
– UnixDummy001 Jul 17 '20 at 07:41/logs
directory before and the desired content after running your script, so that we can understand which files should be treated in what way. – AdminBee Jul 17 '20 at 08:16ls
is highly disrecommended as it will stumble on spaces or other special characters in the filenames (even though it would appear that in this case, the names are "well-behaved" in that respect). Also, why do you pipe the output ofls
intofind
? – AdminBee Jul 17 '20 at 08:18ls
intofind
, becausefind
doesn't care about its stdin. What you've written is equivalent tofind *.log4 | sed s/log4/log
, which will also fail because thesed
is missing a trailing/
– Chris Davies Jul 17 '20 at 08:50gzip
. On UNIX/Linux platformsgzip
is definitely the preferred solution, but it doesn't zip as inZIP.EXE
on Windows, it compresses. – Chris Davies Jul 17 '20 at 08:54sh
or forbash
? – Chris Davies Jul 17 '20 at 08:55