LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 08-19-2009, 03:36 AM   #1
ElectroLinux
Member
 
Registered: Feb 2004
Posts: 74

Rep: Reputation: 15
delete a file based on content and date


I'm trying to delete /tmp/.yz files that are older than 5 days that DON'T contain the number of a pid that is still running. Can I do this on the command line? How?

Can someone help with a quickie perl script?

I know this will help find the files and delete, but it doesn't look at the content and compare it to a live pid. I don't want to delete that file if the pid is alive.

find /path/to/files* -mtime +5 -exec rm {} \;


Thanks,
ElectroLinux
 
Old 08-19-2009, 03:51 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Your requirement is a bit complex for a simple command line so scripting is preferable.

I don't PERL so here's an untested bash script
Code:
#!/bin/bash

for file in $(find /path/to/files -type f -mtime +5)
do
    if [[ ! -d /proc/"$(cat $file)" ]]; then
        rm "$file"
    fi
done
Some caveats:
  • This assumes the files contain only the PID.
  • This assumes $PATH is set and the commands used are not aliased.
  • The file names do not include whitespace characters.
In other words it's proof-of-concept rather than robust.
 
Old 08-19-2009, 03:52 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
I'm not an expert in Perl, anyway I can suggest a way using shell. You have to do a loop over the files given from the find command, extract the PID and use ps to find out if the process is still running (or eventually simply check for the existence of the directory /proc/$PID. Something like:
Code:
while read file
do
  pid=$(cat "$file")
  if ps $pid > /dev/null
  then
    echo process is still running, do nothing
  else
    echo process is terminated
    rm -i "$file"
  fi
done < <(find /path/to/files* -mtime +5)
or
Code:
while read file
do
  pid=$(cat "$file")
  if [ -d /proc/$pid ]
  then
    echo process is still running, do nothing
  else
    echo process is terminated
    rm -i "$file"
  fi
done < <(find /path/to/files* -mtime +5)
Edit: beaten by catkin!
 
Old 08-19-2009, 03:58 AM   #4
tapke
LQ Newbie
 
Registered: Jun 2008
Posts: 13

Rep: Reputation: 0
FILES=`grep -H "yourstring" * | sed "s/:yourstring//g"`; for f in $FILES; do rm -rfv $f; done

do this in desired directory
 
Old 08-19-2009, 04:08 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Whoa! Please don't run any variant of rm -fr without testing very carefully first!

You can test tapke's suggestion by changing do rm -rfv $f to do echo rm -rfv $f
 
Old 08-19-2009, 06:34 AM   #6
ElectroLinux
Member
 
Registered: Feb 2004
Posts: 74

Original Poster
Rep: Reputation: 15
Thanks for all your replies. One problem I have is that the output of the file has the pid like this:
<whitespaces> 19993 <whitespaces or CR>
<whitespaces> 14559 <whitespaces or CR>
<whitespaces> 1182 <whitespaces or CR>

So i'm not sure if that effects the matching and i'm not sure if the preceding space are white spaces/tabs and after if there are white spaces,tabs, or CR. Does that matter?

As you can see here...I replaced remove with echo...is this correct? It looks like it would have deleted /tmp.X1-lock, but it should have not since it is in /proc/
I'm doing testing so I took out the date condition as well.

#!/bin/bash
for file in $(find /tmp/.X*-lock -type f)
do
if [[ ! -d /proc/"$(cat $file)" ]]; then
echo "$file"
fi
done



# ./try
/tmp/.X1-lock
/tmp/.X349-lock
/tmp/.X350-lock
/tmp/.X353-lock
/tmp/.X355-lock
/tmp/.X356-lock
/tmp/.X357-lock
/tmp/.X358-lock
/tmp/.X359-lock
/tmp/.X360-lock
/tmp/.X361-lock
/tmp/.X362-lock
/tmp/.X363-lock
/tmp/.X364-lock
/tmp/.X365-lock
/tmp/.X366-lock
/tmp/.X367-lock
/tmp/.X5-lock
# cat /tmp/.X1-lock
12567
# cat /proc/12567
cat: /proc/12567: Is a directory

Last edited by ElectroLinux; 08-19-2009 at 06:35 AM. Reason: whitespaces were missing
 
Old 08-19-2009, 06:49 AM   #7
ElectroLinux
Member
 
Registered: Feb 2004
Posts: 74

Original Poster
Rep: Reputation: 15
Actually, i did a little modifying and I think this will work...Notice the .X1-lock file is not echoed which was a live PID.

THANKS EVERYONE!

#!/bin/bash
for file in $(find /tmp/.X*-lock)
do
if [[ ! -d /proc/"$(cat $file | awk -F" " '{print$1}' )" ]]; then
echo "$file"
fi
done


#./try
/tmp/.X349-lock
/tmp/.X350-lock
/tmp/.X353-lock
/tmp/.X355-lock
/tmp/.X356-lock
/tmp/.X357-lock
/tmp/.X358-lock
/tmp/.X359-lock
/tmp/.X360-lock
/tmp/.X361-lock
/tmp/.X362-lock
/tmp/.X363-lock
/tmp/.X364-lock
/tmp/.X365-lock
/tmp/.X366-lock
/tmp/.X367-lock
/tmp/.X5-lock
 
Old 08-19-2009, 07:01 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Quote:
Originally Posted by ElectroLinux View Post
Code:
#!/bin/bash
for file in $(find /tmp/.X*-lock)
do
    if [[ ! -d /proc/"$(cat $file | awk -F" " '{print$1}' )" ]]; then
           echo "$file"
    fi
done
The find command in that form is equivalent to
Code:
for file in /tmp/.X*-lock
because bash expands /tmp/.X*-lock before it gets to find. And is it OK that it looks for all such files, not just ones more than 5 days old? Maybe you want something like
Code:
for file in $(find /tmp -name 'X*-lock' -mtime +5)
Here the single quotes in 'X*-lock' prevent the shell expanding the pattern so it is passed verbatim to find.

Edit:

Sorry -- you did explain "I'm doing testing so I took out the date condition as well."

The fact that your awk -F" " worked suggests that there were only spaces (no tabs) before and after the PID in the files. awk is working line-by-line so silently strips any trailing CRs.

Last edited by catkin; 08-19-2009 at 07:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
bash script to delete files / folders based on date and freespace nekawa Linux - Newbie 5 06-08-2009 09:00 PM
File content delete ajkannan83 Linux - Software 4 07-01-2008 04:16 AM
Delete files based on current date -1 Winanjaya Linux - Newbie 3 06-23-2008 04:38 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM


All times are GMT -5. The time now is 11:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration