bash - Deleting Inactive 5 days Old Files -
my aim delete more 5 days old files no longer used process. starter have written following script, not work, says line 10 command not found.
home=~/var cd $home f in `find . -type f`; if [`lsof -n $f`]; echo $f fi done
hmm ye not being done try this:
#!/bin/bash dir=$home/var ########################################################## ## files older 5 days , recursive value set 1 # f in $(find $dir -mtime +5 -maxdepth 1 -type f); ########################################################## f in $(find $dir -type f); # run lsof , pattern a-z send dev null lsof -n $f |grep [a-z] > /dev/null # if found exit status 0 or success if [ $? = 0 ]; echo "$f in use -->" else echo "file $f not in use" fi done
in script had defined home ~/var - using squigly line in scripting try , stay away from. secondly changing environment variable's value within script.. try command line
env|grep home
this new method lot cleaner
now here pointer may mean need make further changes...
will script running in cron job ? running cron entry existing user ? if set root run above fail show how:
echo $home /home/myuser sudo -i echo $home /root
notice how ~ or $home value home has changed.. if decide run cron entry user try
scriptuser="your_user" getent passwd $scriptuser|awk -f":" '{print $6}'
if current user sudo su - or sudo -i executing script try :
getent passwd $(logname)|awk -f":" '{print $6}'
Comments
Post a Comment