LinuxQuestions.org
Help answer threads with 0 replies.
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
  Search this Thread
Old 02-28-2015, 10:05 AM   #1
Sjel85
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Rep: Reputation: Disabled
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!
 
Old 02-28-2015, 10:09 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,250

Rep: Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432
Quote:
Originally Posted by Sjel85 View Post
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.
 
Old 02-28-2015, 11:34 AM   #3
Sjel85
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-28-2015, 12:55 PM   #4
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Arch, Debian
Posts: 843

Rep: Reputation: 242Reputation: 242Reputation: 242
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.

Last edited by joe_2000; 02-28-2015 at 12:57 PM.
 
Old 02-28-2015, 12:57 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,360
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by Sjel85 View Post
(..) 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?
 
Old 02-28-2015, 01:57 PM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
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
 
Old 02-28-2015, 03:31 PM   #7
Sjel85
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
shell script problem bondoq Linux - Newbie 4 07-02-2006 07:31 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration