I have a file that contains paths - looks like this:


What would be the easiest way to remove all lines that contain the current directory ($PWD)?

grep -v "^$PWD$" FILE-LIST
  • -v inverses the search, so only non-matching lines are printed
  • ^...$ ensures that the pattern only matches the whole line (otherwise all subdirectories of $PWD would got filtered as well)
  • 1
    Awesome, that's perfect - simple and works! – Emanuil Rusev Dec 25 '18 at 12:38
  • 3
    See also the -x option which does the ^ and $ for you – Jeff Schaller Dec 25 '18 at 13:20
  • 1
    grep -xvF -- "$PWD" should be safe, lest the present working directory name comprise grep regex metachars and/or options. – Rakesh Sharma Dec 26 '18 at 4:23

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.