Could someone explain what crud is and how exactly grep works in this case?

file $(grep –irl crud /usr/src/linux-2.4)
  • It looks like "crud" is the search string. Look at man grep for what -irl does. – DK Bose Apr 17 '17 at 12:39
  • 3
    You have used a where you want to use a -. Small but important difference. – Jos Apr 17 '17 at 12:39
  • @Jos Yes, it's an en dash (U+2013) instead of hyphen/minus (U+2D) – wjandrea Apr 19 '17 at 21:27

Let's break this down. First, the $(command) format is called "command substitution". It is a way of returning a command's output, essentially creating a variable whose value is the output of that command. For example:

$ echo foo
$ var=$(echo foo) ## set the output of 'echo foo' to the variable $var
$ echo "$var"

Now, in this case, the command being run is grep -irl crud /usr/src/linux-2.4. To understand what that means, run man grep and see what each of those three (i, r and l) options does:

   -i, --ignore-case
          Ignore case distinctions in  both  the  PATTERN  and  the  input

   -r, --recursive
          Read all files  under  each  directory,  recursively,  following
          symbolic  links only if they are on the command line.  Note that
          if  no  file  operand  is  given,  grep  searches  the   working
          directory.  This is equivalent to the -d recurse option.

   -l, --files-with-matches
          Suppress normal output; instead print the  name  of  each  input
          file  from  which  output would normally have been printed.  The
          scanning will stop on the first match.

Taken all together, that means "recurse through all the files in /usr/src/linux-2.4 and search through each of them case-insensitively for the word crud, then print the names of any files that match".

So, that command will print a list of file names containing the word crud. Because we are running file $(grep . . . ), this means that the command file (which prints out the file type and some information about the file in question) will be run on each of the files returned by the grep.

Putting it all together, that command will print out the basic file information about all files in /usr/src/linux-2.4 containing the string crud. On my 14.04 machine, that returns:

$ file $(grep -irl crud /usr/src/)
/usr/src/linux-headers-3.16.0-77/include/linux/pktcdvd.h:                   C source, ASCII text
/usr/src/linux-headers-3.16.0-77/arch/arm/mach-sa1100/include/mach/cerf.h:  ASCII text
/usr/src/linux-headers-3.13.0-108/include/linux/pktcdvd.h:                  C source, ASCII text
/usr/src/linux-headers-3.13.0-108/arch/arm/mach-sa1100/include/mach/cerf.h: ASCII text
/usr/src/linux-headers-3.13.0-46/include/linux/pktcdvd.h:                   C source, ASCII text
/usr/src/linux-headers-3.13.0-46/arch/arm/mach-sa1100/include/mach/cerf.h:  ASCII text
/usr/src/linux-headers-3.16.0-30/include/linux/pktcdvd.h:                   C source, ASCII text
/usr/src/linux-headers-3.16.0-30/arch/arm/mach-sa1100/include/mach/cerf.h:  ASCII text

file according to the help, determines the Type of the processed file. The command $(grep -irl crud /usr/src/linux-2.4) is:

  • $(<command>) Captures the value of the inside command.
  • grep -irl crud /usr/src/linux-2.4, is the inside command, which is searching for the string crud, with ignore case, recursively and print only names of files containing matches.

So, your complete command, is looking for the type of the files that match the string crud, inside the directory /usr/src/linux-2.4

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.