Igor Aguiar Igor Aguiar - 1 year ago 87
Bash Question

keep zero on left using BC calc

i'm with a problem using shell script
i trying to do an activite that I need to move every files older tthan the date I pass by paramter, i'n doing like this

cd $1
for i in *
var=$(echo $(ls -l --time-style=+%d%m%y $i) | awk '{print $6}' | bc)
temp=$(echo $2 | bc)
if [ $var -lt $temp ]
cp $i /home/igor/olders

but the problem is when the date is before day 10 i dont know how to make this number bigger than the orders, like 01/07/16 will be smaller than 13/06/16, someone now how to help me?

Answer Source

You may want to use the alternative suggested in the comment:

find -newerXY reference (quoting the manpage for find(1)http://linux.die.net/man/1/find):

Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the comparison. 
Some combinations are invalid; for example, it is invalid for X to be t. Some combinations are not implemented on all systems; for example B is not supported on all systems. If an invalid or unsupported combination of XY is specified, a fatal error results. Time specifications are interpreted as for the argument to the -d option of GNU date. If you try to use the birth time of a reference file, and the birth time cannot be determined, a fatal error message results. If you specify a test which refers to the birth time of files being examined, this test will fail for any files where the birth time is unknown.

If you want to stick to your script simply change the format for the ls command. You currently use --time-style=+%d%m%y when you likely want --time-style=+%y%m%d (needs the same style as a parameter or a switch within your script).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download