What does character mean in grep ^ d?

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

What would I like to know what the symbol ^ in ^ d means?

+4
source share
3 answers

The carriage ^ and the dollar sign $ are metacharacters that respectively correspond to the empty line at the beginning and end of the line. Grep matches only lines starting with "d".

+11
source

To complement The New Idiot's good answer , I want to point out that this is:

 ls -l | grep ^d 

Shows all directories in the current directory . This is because ls -l appends d to the beginning of directory information.

The ls -l format is as follows:

 -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 this clearer, you can ls -lF :

 -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 -l | grep /$ ls -l | grep /$ will do the same as ls -l | grep ^d ls -l | grep ^d .

+4
source

It has two meanings. One of them, like the "New idiot" above, indicated. Another, equally useful, is inside a character class expression, where it means negation: grep -E '[^[:digit:]]' accepts any character except a digit. The grep -E '[^[:digit:]]' accepts any character except a digit. The ^ `must be the first character inside [].

+1
source

Source: https://habr.com/ru/post/1485746/


All Articles