1

I have lots of old log files in a directory, with apps logs generated every month to this directory. I want to use a script to remove only the last month old logs.

This is my file logsDir , these files are 0kb, i have created for question understanding

[root@dasari9 logDir]# ls -ltr
total 136
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram9.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram8.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram7.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram6.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram69.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram68.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram67.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram66.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram5.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram59.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram58.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram57.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram56.logs
 -rw-r--r-- 1 root root 0 Mar 20 12:22 ram4.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram49.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram48.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram47.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram46.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram3.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram39.logs
 -rw-r--r-- 1 root root 0 Mar 20 12:22 ram38.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram37.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram36.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram2.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram29.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram28.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram27.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram26.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram1.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram19.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram18.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram17.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram16.logs

This is my logs.sh:

#!/bin/bash

Jan=Dec Feb=Jan Mar=Feb Apr=Mar May=Apr Jun=May Jul=Jun Aug=Jul Sep=Aug Oct=Sep Nov=Oct Dev=Nov

DIR=/logDir
cd $DIR
if [ $DIR -eq $PWD ]; then 
PWD=$(pwd)
cmd=$(date |awk '{print $2}') 
dat=$(ls -ltr |grep $cmd |awk '{print $9}')
rm -rf $dat
else
echo " enterend in the wrong dir"
fi

but it's failing to get that variable value Apr=Mar

This is the output, I'm trying to delete Apr files

[root@dasari9 /]# bash -x log.sh 
+ Jan=Dec
+ Feb=Jan
+ Mar=Feb
+ Apr=Mar
+ May=Apr
+ Jun=May
+ Jul=Jun
+ Aug=Jul
+ Sep=Aug
+ Oct=Sep
+ Nov=Oct
+ Dev=Nov
+ DIR=/logDir
+ cd /logDir
++ pwd
+ PWD=/logDir
+ '[' /logDir == /logDir ']'
++ date
++ awk '{print $2}'
+ cmd=Apr
++ ls -ltr
++ grep '$Apr'
++ awk '{print $9}'
+ dat=
+ rm -rf
9
  • Without knowing the content of the directory, it's hard to understand what's happening here. What happens when you run ls -ltr | grep Mar in the directory? Does the output show some of the files, and does the output have 9 columns? Commented Jun 16, 2015 at 11:27
  • Better post the code in shellcheck.net for an initial safety check. Also, Jan=Dec Feb=Jan Mar=Feb Apr=Mar May=Apr Jun=May Jul=Jun Aug=Jul Sep=Aug Oct=Sep Nov=Oct Dev=Nov looks quite weird. Commented Jun 16, 2015 at 11:32
  • running with ls -ltr | grep Mar |awk '{print $9}' , it gives all march file names only , with out all fields like type of file , time,date..etc..I'm adding files also now Commented Jun 16, 2015 at 11:33
  • 1
    Why don't you use find for this? Something like find $DIR -mtime +30 will find you files that have not been modified for more than 30 days. Commented Jun 16, 2015 at 11:34
  • 1
    Or better yet; this mostly reduces your script to one line: find . -mtime +30 -exec echo 'rm {}' \; | bash -x Commented Jun 16, 2015 at 11:41

1 Answer 1

1

It seems like you're looking for an indirect variable reference. Your $cmd variable points to the variable $Apr and you want to use the value of $Apr.

Here's how you can do this (just the relevant lines):

cmd=$(date | awk '{print $2}')
eval month=\$$cmd
dat=$(ls -ltr | grep $month | awk '{print $9}')

The eval command resolves the indirect reference created by \$$cmd.

More details here: http://www.tldp.org/LDP/abs/html/ivr.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.