ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Without going into the obvious ramifications of running rm -Rf on a directory via an automated script, I seem to be having a bugger of a time doing so.
Is bash set up to not allow that particular command from a script? I can run it against individual files and it works fine but not against one particular finding which happens to be a directory.
The idea is to look for unowned files (both user and group) and delete anything found. The script is below:
Code:
#!/bin/bash -x
WORKDIR=/var/local/orph_files
mv $WORKDIR/filelist.txt $WORKDIR/oldfilelist.txt
find / -nouser -o -nogroup > $WORKDIR/filelist.txt
#find / -nouser > $WORKDIR/filelist.txt
diff $WORKDIR/filelist.txt $WORKDIR/oldfilelist.txt > $WORKDIR/diffilelist.txt
sed -i '/^/ s/^<\s\|^>\s//' $WORKDIR/diffilelist.txt
if [ -s $WORKDIR/diffilelist.txt ]; then
echo -e "The following files on $HOSTNAME are not owned by anyone and will be removed:\n " > $WORKDIR/mailfile.txt
for i in `cat $WORKDIR/diffilelist.txt`;
do
if [[ $i == *proc* ]]
then
continue
else
rm -Rf $i
echo "$i was removed" >> $WORKDIR/mailfile.txt
fi
done
echo -e "\n\nThank You,\nYour Happily Running System" >> $WORKDIR/mailfile.txt
cat $WORKDIR/mailfile.txt | mail -s "$HOSTNAME Orphaned Files" admin@company.com
else
echo "There are no unowned files on $HOSTNAME." > $WORKDIR/mailfile.txt
echo -e "\n\nThank You,\nYour Happily Running System" >> $WORKDIR/mailfile.txt
cat $WORKDIR/mailfile.txt | mail -s "$HOSTNAME Orphaned Files" admin@company.com
fi
Even if I run
Code:
find / -nouser -o -nogroup -exec rm -Rf {} \;
doesn't work.
If I run the above find without -exec it spits out the files and directory as expected.
It helps if you post error messages: hard to tell what's happening otherwise. For instance, I found that rm -rf works fine on directories, but I needed to add -a -prune, otherwise find would search inside the deleted directory and complain that it no longer existed!
Code:
~/tmp/scratch$ mkdir dir
~/tmp/scratch$ ls -a
. .. dir
~/tmp/scratch$ find . -name dir -exec rm -rf {} \;
find: `./dir': No such file or directory
~/tmp/scratch$ ls -a
. ..
~/tmp/scratch$ mkdir dir
~/tmp/scratch$ find . -name dir -exec rm -rf {} \; -a -prune
~/tmp/scratch$ ls -a
. ..
It helps if you post error messages: hard to tell what's happening otherwise.
This is the output of running the script with debugging on a server that has several unowned files that aren't the directory I'm having problems with (it does have that directory, though):
Code:
[root@server ~]# /usr/local/sbin/orph_files.sh
+ WORKDIR=/var/local/orph_files
+ mv /var/local/orph_files/filelist.txt /var/local/orph_files/oldfilelist.txt
+ find / -nouser -o -nogroup
find: /proc/11436/task/11436/fd/4: No such file or directory
find: /proc/11436/task/11436/fd/4: No such file or directory
find: /proc/11436/task/11436/fdinfo/4: No such file or directory
find: /proc/11436/task/11436/fdinfo/4: No such file or directory
find: /proc/11436/fd/4: No such file or directory
find: /proc/11436/fd/4: No such file or directory
find: /proc/11436/fdinfo/4: No such file or directory
find: /proc/11436/fdinfo/4: No such file or directory
+ diff /var/local/orph_files/filelist.txt /var/local/orph_files/oldfilelist.txt
+ sed -i '/^/ s/^<\s\|^>\s//' /var/local/orph_files/diffilelist.txt
+ '[' -s /var/local/orph_files/diffilelist.txt ']'
+ echo -e 'The following files on server are not owned by anyone and will be removed:\n '
++ cat /var/local/orph_files/diffilelist.txt
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ 2901,3104d2900 == *proc* ]]
+ rm -rf 2901,3104d2900
+ echo '2901,3104d2900 was removed'
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ /home/user/app/thing/sgchroot/tmp/sess_lel29584oll6on5fufc865sie7 == *proc* ]]
+ rm -rf /home/user/app/thing/sgchroot/tmp/sess_lel29584oll6on5fufc865sie7
+ echo '/home/user/app/thing/sgchroot/tmp/sess_lel29584oll6on5fufc865sie7 was removed'
<snip>
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ /proc/28305/attr == *proc* ]]
+ continue
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ 3108,3110d2903 == *proc* ]]
+ rm -rf 3108,3110d2903
+ echo '3108,3110d2903 was removed'
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ /var/run/sudo/user0 == *proc* ]]
+ rm -rf /var/run/sudo/user0
+ echo '/var/run/sudo/user0 was removed'
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ /var/run/sudo/user1 == *proc* ]]
+ rm -rf /var/run/sudo/user1
+ echo '/var/run/sudo/user1 was removed'
+ for i in '`cat $WORKDIR/diffilelist.txt`'
+ [[ /var/run/sudo/testuser == *proc* ]]
+ rm -rf /var/run/sudo/testuser
+ echo '/var/run/sudo/testuser was removed'
+ echo -e '\n\nThank You,\nYour Happily Running System'
+ cat /var/local/orph_files/mailfile.txt
+ mail -s 'server Orphaned Files' admin@company.com
It doesn't remove the directory in question. It does remove all other unowned files. I've used both 'rm -rf' and 'rm -Rf' neither works.
Which file is not removed that you think should have been removed?
The only undeleted file that I see is
Code:
/proc/28305/attr
which is not an error since it contains 'proc'. According to your script such files should not be deleted.
The directory that should be removed (and all files under it) doesn't show up because the script doesn't remove them. If I didn't <snip> the output it would have been several more session files as well as the skipping of several /proc files. The latter of which will go away since I've been told about the the -prune option to find.
Okay, the problem is the bit of code where you produce diffilelist.txt:
Code:
diff $WORKDIR/filelist.txt $WORKDIR/oldfilelist.txt > $WORKDIR/diffilelist.txt
sed -i '/^/ s/^<\s\|^>\s//' $WORKDIR/diffilelist.txt
I'm not sure what exactly your intention is, but it should be pointed out that the sed command leaves some stuff from the diff that is not a filename, thus the "fishy" "2901,3104d2900".
Ignoring that, diffilelist.txt will contain files from the new file list, and files from the old list, but not files that are in both. So the answer as to why you are not removing a file now is: you didn't remove it last time (so it's in both lists). Also, you will attempt to remove non-existant files that were successfully removed last time (because they will only be in the old list).
I don't really understand why you are interested in oldfilelist.txt at all.
Quote:
How can I get the file name being removed into the email like I do in the for loop?:
Code:
echo "$i was removed" >> $WORKDIR/mailfile.txt
Code:
while read FILE; do
echo "$FILE was removed" >> $WORKDIR/mailfile.txt
done < inputfile
You can use "i" instead of "FILE" (in either style of loop) if you prefer shorter inscrutable variable names.
Last edited by ntubski; 10-07-2011 at 04:13 PM.
Reason: you're -> your
I've eliminated the whole difffile thing. Looking back, I'm not sure why I was interested in the oldfiles.txt file either. I've removed it and simply set the script to run a find and remove the files.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.