LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-27-2011, 10:55 AM   #1
Passmossis
LQ Newbie
 
Registered: Jul 2011
Location: USA
Distribution: RHEL 6.5
Posts: 28

Rep: Reputation: 2
First script, results from find command


This is my first go at scripting in Linux. I'm using Red Hat Enterprise Client 5.7.

This test script is to find all files that match the criteria from a find command. Files that are found are passed through the script to have the correct permissions set.

Code:
#!/bin/bash
CorrectPermission=-r--------
ID=$(find /test -name abc -print)
echo $ID
Permisson=$(ls -l $ID | awk '{print $1}')
If there is only one file that meets the criteria of the find command, this works. If there are multiple results, the script fails. The reason it fails is the results from the find command are not parsed to multiple lines, I believe.

I am testing this script against:

Code:
/test/abc
/test/dif/abc
Where the two abc files have incorrect permissions.

Results of executing my script are:

Code:
/test/dif/abc /test/abc
Permissions: -rw-r--r-- -rwxrwxrwx
./test3.scr: line 7: [: too many arguments
Permissions: Incorrect
Updating permissions to: 400

This is not for any school assignment, but for personal understanding. Thank you for any help you can provide.
 
Old 10-27-2011, 11:52 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
You could use a loop, something like

Code:
#!/bin/bash
for i in `find /test -type f -name abc ! -perm 600`
do
 chmod 600 $i
done
Keep in mind, this would change permissions on all files in all subdirectories, even if they are executable. "man find" has many examples to help if this is not exactly what you're looking for.

In this situation, there isn't much benefit to checking the permissions since you want all files to be 600 (you could skip checking and just set all files to 600).
 
1 members found this post helpful.
Old 10-27-2011, 12:33 PM   #3
Passmossis
LQ Newbie
 
Registered: Jul 2011
Location: USA
Distribution: RHEL 6.5
Posts: 28

Original Poster
Rep: Reputation: 2
Thanks to your exert I was able to accomplish what I wanted to do. Thank you!

Code:
#!/bin/bash
CPerm=-rw-------
echo Setting correct permissions for abc
for i in `find /test -type f -name abc`
do
    echo Found $i
    Perm=$(ls -l $i | awk '{print $1}')
    echo Current permissions: $Perm
    if [ $Perm == $CPerm ]; then
        echo Permissions: Good
    else
        echo Permissions: Incorrect
        echo Updating permissions to: 600
        chmod 600 $i
        Perm=$(ls -l $i | awk '{print $1}')
        echo New permissions: $Perm
    fi
done
While I do like the '! -perm 600' in find, this script is a bit longer to where I also groom the owner and group owner fields using awk.
 
Old 10-27-2011, 12:51 PM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
You can also check out the "stat" command, it has the ability to output owner, permissions, etc based on the attributes you specify (unless you want to use awk).
 
  


Reply



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
find command uses different format for results in 10.04 lts and 8.04 lts koloth Ubuntu 2 05-05-2011 08:51 AM
reusing find command results in mplayer arguments mukami Linux - Newbie 1 01-22-2010 04:11 PM
Grab results of GRUB's find command into a shell script variable kushalkoolwal Programming 6 02-03-2009 05:04 PM
Shell Script format question - output command results in quotes Barefootpanda Linux - General 5 10-13-2008 11:40 PM
find command displays unexpected results helptonewbie Linux - Newbie 5 08-12-2008 02:25 AM

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

All times are GMT -5. The time now is 12:42 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
Open Source Consulting | Domain Registration