16

When I do ls -l | grep ^d it lists only directories in the current directory.

What I'd like to know is what does the caret ^ in ^d mean?

2
  • For related information, look up regular expressions or check out this page on using regular expressions in grep: robelle.com/smugbook/regexpr.html Commented Oct 14, 2010 at 3:51
  • This works, but is suboptimal. Consider ls */ instead, which will work with or without the long listing (ls -l) and without needing grep (and thus is very slightly faster) and is less likely (than ls -l *) to run into an argument list too long issue (since it puts just directories into the command line). Of course, find . -maxdepth 1 -type d is even better, as it doesn't clutter your command line at all. Commented Mar 6, 2015 at 1:45

2 Answers 2

27

Andy's answer is correct, as seen in the man page:

Anchoring

The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the beginning and end of a line.

The reason it works is the -l flag to ls makes it use the long-listing format. The first thing shown in each line is the human-readable permissions for the file, and the first character of that is either d for a directory or - for a file

18

That's a caret, not a carrot. It means "beginning of the line." The grep is matching only lines that start with "d".

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.