LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Script problem (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-problem-4175535366/)

Sjel85 02-28-2015 09:05 AM

Shell Script problem
 
Hi,

I'm wondering if anyone can help me make a script that searches through a specific folder (in this case /tmp ) for files with a given permissions (755) and then delete all the other files with different permissions?

The correct permission should be, as mentioned 755, and those are the files that should be kept (not deleted).
All other files in this folder with different permissions should be deleted.

Thanks! :)

TB0ne 02-28-2015 09:09 AM

Quote:

Originally Posted by Sjel85 (Post 5324710)
Hi,
I'm wondering if anyone can help me make a script that searches through a specific folder (in this case /tmp ) for files with a given permissions (755) and then delete all the other files with different permissions?

The correct permission should be, as mentioned 755, and those are the files that should be kept (not deleted).
All other files in this folder with different permissions should be deleted.

We certainly can HELP you...so post what YOU have written/tried of your own so far, and tell us where you're stuck. But we are NOT going to write your scripts FOR YOU...there are many thousands of easily-found bash scripting tutorials, one of which is in my posting signature, so if you're having trouble getting started, that should give you help.

Since this seems VERY much like a homework question, your teacher can also help you, as well as the man pages on the find and ls commands.

Sjel85 02-28-2015 10:34 AM

I'm sorry, I didn't intended for you to make it for me, but help me along the way. Preferable with guidelines so that I can learn from it. I might be stupid, but I've been reading a lot about it, but it's so new for me that I sometimes feels like a huge question mark while reading it. A simple explanation of why this is wrong would've been greatly appreciated, and hopefully with a nudge towards what could be different.

Okay, so what I've written is this:

Code:

#!/bin/bash

if [ ./tmp -type f ! -perm 755 ]

        then
          echo "Files with different permissions than 755 are deleted"
          -rm -f {}
        else
          echo "No files without permissions 755 were found. No files deleted."
fi

What I (most likely) obviously get when I try to run this, is that there's too many arguments. I thought it was similar to when using the find command. Because if I use "find ./tmp -type f ! -perm 755", I get the result I need. Now, of course I also needs it to be able to remove those files that it finds.

When I run the bash script, I just use the filename ( ./snd ) command for it, if that matters.

joe_2000 02-28-2015 11:55 AM

What you have looks like you have taken some syntax elements from the find command and tried to use them in isolation... which obviously does not work.

Try playing around with find:

Code:

find /tmp/ -type f ! -perm 755
Once it finds what you want, append the -exec part to actually remove the found files. I'll leave that out for now so you can figure it out yourself...

EDIT: Oops. You already did that part. So you are almost there. Look at man find to see how the -exec flag works.

unSpawn 02-28-2015 11:57 AM

Quote:

Originally Posted by Sjel85 (Post 5324734)
(..) if I use "find ./tmp -type f ! -perm 755", I get the result I need. Now, of course I also needs it to be able to remove those files that it finds.

Check 'man find' to see if your version of 'find' honors the "-delete" switch?

metaschima 02-28-2015 12:57 PM

Don't use the test '[]' brackets for commands. Just use 'if' and the command.

Besides the '-delete' you could also pipe to xargs.

Code:

if find ./tmp -type f ! -perm 755
        then
          echo "Files with different permissions than 755 are deleted"
          find ./tmp -type f -print0 ! -perm 755 | xargs -0 rm -f
        else
          echo "No files without permissions 755 were found. No files deleted."
fi


Sjel85 02-28-2015 02:31 PM

Thanks for the replies!

So it seemed to work perfectly when I removed the brackets, thanks a lot for that heads-up metaschima!

Also tried with the xargs command, but somehow it overlooked the "rules" and just deleted all the files in the folder... So I went back to test with the -delete function (now that the brackets was removed), and it worked flawlessly. So thanks a lot for the help, everyone.


All times are GMT -5. The time now is 03:19 AM.