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 character ^ in ^d mean?
The caret ^
and the dollar sign $
are meta-characters that respectively match the empty string at the beginning and end of a line.The grep is matching only lines that start with "d".
To complement the good answer by The New Idiot, I want to point out that this:
ls -l | grep ^d
Shows all directories in the current directory. That's because the ls -l
adds a d
in the beginning of the directories info.
The format of ls -l
is like:
-rwxr-xr-x 1 user group 0 Jun 12 12:25 exec_file
-rw-rw-r-- 1 user group 0 Jun 12 12:25 normal_file
drwxr-xr-x 16 user group 4096 May 24 12:46 dir
^
|___ see the "d"
To make it more clear, you can ls -lF
to include a /
to the end of the directories info:
-rwxr-xr-x 1 user group 0 Jun 12 12:25 exec_file*
-rw-rw-r-- 1 user group 0 Jun 12 12:25 normal_file
drwxr-xr-x 16 user group 4096 May 24 12:46 dir/
So ls -lF | grep /$
will do the same as ls -l | grep ^d
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With