Skip navigation

Different Terminal operation methods

685 Views 8 Replies Latest reply: Feb 4, 2013 6:22 PM by Adam Mercado RSS
Adam Mercado Level 1 Level 1 (10 points)
Currently Being Moderated
Feb 2, 2013 12:42 PM

I'm writing a report on my blog on some Terminal operations I performed recently to rescue a drive that was locked and with corrupt ACL. I want to get some things straight in my mind before I spread errouneous info.


When using a command like:


sudo cd /Volumes/Media

sudo find . -flags uchg -exec chflags nouchg {} \;


Which as I understand it will find all locked files on /Volumes/Media and unlock them


or using a command like:


sudo chflags -R nouchg /Volumes/Media


Which as I understand it will unlock ALL files recursively from the given directory, which effectively does the same thing as above.


What is the functional or operational difference between those two methods? Is there a safer or more preferable method?


Also, what is the difference between find . and find /


The Apple Discussions have been greatly helpful the past few weeks as I've learned more and more about how to get things done in Terminal. Thanks to all who share their knowledge here. I want to make sure I understand things fully before I make incorrect assumptions.




Mac Pro, Mac OS X (10.6.8), ATI Radeon 3870, 4GB RAM
  • etresoft Level 7 Level 7 (23,905 points)
    Currently Being Moderated
    Feb 2, 2013 1:57 PM (in response to Adam Mercado)

    Why would you want to do something like that at all? Those flags are there for a reason.


    You don't need sudo to change a directory. You could just do:

    find /Volumes/Media -flags uchg -exec chflags nouchg {} \;


    This appears to be a pretty harmless command in the right context. Using it as part of "find /" would not be the right context and would likely be quite harmful. This is one of those times where one accidental space could trash your hard drive. I strongly advise against the use of sudo at all unless you know precisely what you are doing and why.


    There doesn't appear to be much difference between the two. If every file had that flag, then the find version would be doing twice the work, having to both check and change whereas the recursive version is just blindly changing. However, if only 3 files need to be changed, then the recursive version is needlessly modifying all files when only 3 needed changing.

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Feb 2, 2013 2:51 PM (in response to Adam Mercado)

    Well there is one huge difference the first commands


    sudo cd /Volumes/Media

    sudo find . -flags uchg -exec chflags nouchg {} \;



    Will not run the find in /Volumes/Media but in what ever directory you started in.


    The cd only changes the working directory for the shell that is spawned by the sudo command. When that command finshes you'll be back at the working directory that you started in.


    You can test this by doing


    sudo cd /toSomePlace

    sudo pwd (or just pwd, won't make a difference)

  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Feb 2, 2013 5:06 PM (in response to Adam Mercado)

    As as been explained above the


    sudo cd ...


    command is useless.  Frank Caggiano has explained why the sudo cd does not work as you think, but if you do not understand what he said, you need to come back and ask, as the concept of what happens iin a subprocess stays in a subprocess.  Things like your current working directory and shell variable and environment variable changes in a subprocess do not affect the originating shell (namely the one issuing the command prompt).  If you do not understant this, you will get tripped up again and again in the shell.


    I'm assuming you have access to /Volume/Media, you cd without the sudo.  If for some reason you cannot cd to /Volume/Media, then you will need to take a totally different approach.


    etresoft has mentioned that the 'find' approach will only modify the files that are locked, where as the chflags -R approach will blindly modify every file.  On the surface this may not appear to affect the end result, but the chflags -R approach will also change the ctime (Change Time) of every file, where as the 'find' approach will only change the ctime for the files that need changing.


    The change to ctime may cause an incremental backup utility to backup every file in /Volume/Media instead of just the few files that have had their flags changed.


    Also, what is the difference between find . and find /

    . in Unix file systems is the current directory.  .. is the parent directory ../../ is the grandparent directory, etc...


    / is the top level of the boot file system.  From here every other file accessible via the operating system is somewhere under /


    As has been explained, both . and / can be dangerous.  . is dangerous if you are not where you think you are, such as the case of using sudo cd no actually placing you where you think are going.  / is dangerous as depending on what you are doing with the 'find' command you can make some non-reversable file system modifications.


    When playing with 'find' you might want to NOT include -exec until you absolutely know that the 'find' command you are creating spits out exactly what files you need to modify.  Or if you want to test with -exec, then use echo first


    find . -flags uchg -exec echo chflags nouchg {} \;


    where the echo command will display the chflags command that you plan to exeucte, but will not actually change anything.  You can then verify that the commands look good and the files being changed are what you expect.  You can later either remove the 'echo' or just pipe the command into sh


    cd /Volumes/Media

    find . -flags uchg -exec echo chflags nouchg {} \; | sudo sh

    MacBook Pro, Mac OS X (10.7.5), 27" iMac, MacBook, MacMini, etc...
  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Feb 4, 2013 2:07 PM (in response to Adam Mercado)

    1. I did indeed completely misunderstand how cd works. I figured changing the directory would indeed turn all future focus to that directory. So:

    •    what DOES cd do and why?
    •    how do I move focus into the directory I want to work in?


    The 'cd' command is interpreted by the shell and changes the current working directory for the shell.


    Each process maintains its own current working directory.  The shell is code running in a process.


    A child process inherits the parent current working directory.


    The 'sudo' command is run in a child process, and starts a shell process running as the 'root' (very privileged user), which then interprets the 'cd' command.


    Child processes CANNOT change the parent process environment.  That is to say, the child process cannot change the parent process' current working directory (amoung many other things in the parent process).


    Saying this differently.


    cd /path/to/some/directory


    will change the current running shell's current working directory.  Any child process created by the same shell AFTER the 'cd' command will inherit the new current working directory.


    sudo cd /path/to/some/directory


    will start a child process running a new shell as 'root' which will change the current working directory of the child shell process.  Since the only command the child shell process is running is 'cd', the child shell process exits, returning to the 'sudo' command, which having completed its task returns to the parent shell.  Since the 'cd' command happened in a child process, the 'cd' does NOT affect the parent shell, so the parent shell did not change its working directory.


    You really need to understand parent/child process relationships and that, except for some select command interpreted by the shell (see: man bash), all other commands are executed in a child process.

  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Feb 4, 2013 2:14 PM (in response to Adam Mercado)

    In which case, how does one stay in sudo mode (if that makes any sense at all)? Or would that be completely unessasary and a bass ackwards way of doing things?

    Put all the commands in a script, then:


    sudo name_of_script




    you could separate multiple commands with semi-colons all enclused in quotes


    sudo 'cd /Volumes/Media; find . -flags uchg -exec chflags nouchg {} \;'


    -OR- pipe the commands to excuete into a sudo'ed shell


    ( echo 'cd /Volume/Media'

      echo 'find . -flags uchg -exec chflags nouchg {} \;'

    ) | sudo bash



    since you are using the 'find' command


    sudo find /Volume/Media -flags uchg -exec chflags nouchg {} \;


    There are most likely several other ways to do this.


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.