Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ^ character mean in grep ^d?

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?

like image 729
Aniket Thakur Avatar asked Jun 12 '13 07:06

Aniket Thakur


2 Answers

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".

like image 196
AllTooSir Avatar answered Feb 17 '23 22:02

AllTooSir


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.

like image 27
fedorqui 'SO stop harming' Avatar answered Feb 17 '23 23:02

fedorqui 'SO stop harming'