I run grep some-string -r . &. While it is running in bkg, I cd to another directory. It seems that grep interprets the hard link . differently then. What happens before and after I change the current directory? Will both the original and the new directories not be searched completely?

I wonder if . as a command line argument to a command is only dereferenced at the start of running the command, or is dereferenced whenever it is used by the program during its running?

  • What differences in behaviour do you see? – Stephen Kitt Mar 20 '15 at 19:20
  • it found some files in the original working dir before I changed the working dir, and some files in the new working dir after I changed the working dir. – Tim Mar 20 '15 at 19:26
  • Was your new working directory a sub-directory of the initial working directory? – Stephen Kitt Mar 20 '15 at 19:28
  • If it doesn't reveal anything sensitive, would you mind copying and pasting grep's output? – Stephen Kitt Mar 20 '15 at 19:33
  • Can't reproduce it. Not sure why I got the strange result. – Tim Mar 20 '15 at 22:12

Each process has its own "current working directory", which can't be changed from outside the process.

So when you do

grep some-string -r . &

your shell starts grep in the background, and grep's current working directory is initialised to the same value as the shell's at that moment. grep's definition of . here is its own current directory, not anything else's; the shell has no part in the argument's interpretation.

Subsequently changing the shell's directory using cd has no impact on grep...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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