Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can anyone help me adapt this script to Mac OS X?

USR=$(logname)
f=`find /home/$USR/documents -name "*.doc" -o -name "*.xsl" -o -name "*.pdf"`
for file in $f
do
   chattr +i $f
done

My goal is to find files with this extension and can not be deleted afterwards, which is achieved by chattr +i in linux

share|improve this question
2  
It would help if you explain what you are trying to do (I can see what your script is doing, I want to know what you expect it to do). –  terdon Feb 10 '13 at 17:26
1  
Why do you store the output of find in $f and then iterate over that when you could simply do everything from find itself with the -exec option? Your script will fail for any file that even has a space in its name. You should never use command substitution to iterate over something that outputs filenames. See: mywiki.wooledge.org/UsingFind#Actions –  slhck Feb 10 '13 at 18:25
1  
If you want to achieve the same effect as "locking" a file in its Get Info dialog, that's chflags uchg $f. –  Daniel Beck Feb 10 '13 at 18:56

1 Answer 1

I'd be happy to comment on your script if you explain what you want it to do. As far as I know, the chattr command is specific to linux. From its man page:

NAME
chattr - change file attributes on a Linux file system

If all you want is to make files returned by that find command read-only, try this:

find $HOME/documents -name "*.doc" -o -name "*.xsl" -o -name "*.pdf" -exec chmod 444 {} \;

Update: If Daniel Beck's suggestion is what you need, you can incorporate it into the find command:

find $HOME/documents -name "*.doc" -o -name "*.xsl" -o -name "*.pdf" -exec chflags uchg {} \;

I still don't understand why you need a script. The USR=$(logname) is useless since all you want is the current user's home directory and that is already in $HOME.

share|improve this answer
    
My goal is to find files with this extension and can not be deleted afterwards, which is achieved by chattr +i in linux= –  user197163 Feb 10 '13 at 19:02
    
In that case, my suggestion should also work, it makes the files read-only. –  terdon Feb 10 '13 at 19:03
    
To execut this script yor need sudo(chattr is needet) , then $home is root home! –  user197163 Feb 11 '13 at 6:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.