8 Replies Latest reply: Feb 4, 2013 6:22 PM by Adam Mercado
Adam Mercado Level 1 Level 1 (10 points)

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.

 

cheers

Adam


Mac Pro, Mac OS X (10.6.8), ATI Radeon 3870, 4GB RAM
  • 1. Re: Different Terminal operation methods
    etresoft Level 7 Level 7 (24,270 points)

    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.

  • 2. Re: Different Terminal operation methods
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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)

  • 3. Re: Different Terminal operation methods
    BobHarris Level 6 Level 6 (13,120 points)

    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

  • 4. Re: Different Terminal operation methods
    Adam Mercado Level 1 Level 1 (10 points)

    First of all, thank you all for taking the time to explain these things to me. It's a great help. I have a number of points to adress so I'll tackle them in order so as not to create a rambling mess.

     

    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?

     

    2. For clarification, I'm trying to DO something here, I've already done it, successfully (however I managed it) and I'm just trying to understand why it worked. I'm a designer with a tiny amount of Terminal experiance, and I'm hoping to pass on some useful information that I discovered to other designers who may find it also useful.

     

    3. Why would I want to do something like that? Because the drive ACL got corrupted and locked by mistake when DiskWarrior locked up on me. The drive contains no System files, only my audio and video files. So I know they do not need to be locked, and no the flags are not there for a reason. This is why I had to use sudo to simply gain access to the drive.

     

    4. Thanks for explaining difference between / and . that makes all the sense in thre world now. And also for pointing out the time-stamping process. When doing my research for the fix to my problem I read a lot of people having lengthy TimeMachine backups after time-stamping updated all of their files. I can see how proceeduraly this could be a big big issue.

     

    Thanks again guys for straightening out these points. Without the Apple Discussions I would have been dead in the water. Some of my searches took me back as far as 2006 so these posts live on to be useful long after the OP has been addressed.

  • 5. Re: Different Terminal operation methods
    Adam Mercado Level 1 Level 1 (10 points)

    Hi Frank

     

    I tried your test

    sudo cd /toSomePlace

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

     

    but that did nothing for me. Not sure what results I should heve been expecting but after pwd all I got was a new prompt, nothing more. Could you further illustrate the point you were trying to make as I still don't fully grok the concept of the starting directory.

     

    Many thanks

    Adam

     

    EDIT:

     

    Okay, I get it. I got the results of my current directory. I played around with pwd some more and I think I understand the relationship of sudo to non-sudo commands. Once sudo has been invoked and the command executed, any non-sudo commands take place on whatever directory they last left off on.

     

    So I entered

    DrOctoPro:~ influxx$ pwd

    /Users/influxx

    DrOctoPro:~ influxx$ cd /

    DrOctoPro:/ influxx$ pwd

    /

    DrOctoPro:/ influxx$ sudo cd /Volumes/Narnia

     

    DrOctoPro:/ influxx$ pwd

    /

    So even though sudo changed directory to Narnia, when I asked for a pwd I am still in /

     

    I can see how that would be very very dangerous indeed.

     

    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?

     

    Thanks

    Adam

     

    Message was edited by: Adam Mercado

  • 6. Re: Different Terminal operation methods
    BobHarris Level 6 Level 6 (13,120 points)

    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.

  • 7. Re: Different Terminal operation methods
    BobHarris Level 6 Level 6 (13,120 points)

    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

     

    -OR-

     

    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

     

    -OR-

    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.

  • 8. Re: Different Terminal operation methods
    Adam Mercado Level 1 Level 1 (10 points)

    Wow Bob, fascinating couple of answers. I had no idea about the parent/child processees. That really makes sense now. Thank you so much for explaining so consisely.

     

    The Terminal is so powerful, it's an amazing tool. I love learning ways I can put it to use. Thanks for helping