0

ls -l | grep -v '^d' | sort -g -r -k 5 | head -2: I used this command to write into a text file.

I've researched a bit online and I think these commands mean this:

  1. grep: a search that searches for a specific pattern in a string

  2. -v: an option for grep that tell it to find and display all lines that do not match

  3. '': not sure what the single quotes are for

  4. ^d: the caret signifies the beginning of a line and the d is the pattern that grep is searching for. This works when using ls -l.

  5. sort sorts the contents of a text file numerically

  6. -g compares according to numerical value not sure what this means exactly

  7. -rreverses the results of the comparison. This would make more sense if I knew that the point of the comparing was for.

  8. -k 5 starts a key at POS1 - not sure what this means, does it mean that something will happen at the 5th character in the first line of my text file?

  9. head -2 displays the first 2 lines of my text file.

Could someone help clarify the parts I am unsure about?

  • FWIW, on a GNU system that can be simplified to ls -lS | grep -v '^[dt]' -m2. However, as Bruce Ediger mentions there are potential problems parsing the output of ls (it's generally better to use find). It's tolerable in the command line, but probably best avoided in a script, especially if you want the script to be portable. – PM 2Ring Feb 11 '16 at 7:42
3

You've got most of the technical details, but I think you're missing the semantics of the whole thing.

The single quotes in '^d' keep whatever shell runs that pipeline from treating characters in the regular expression (which is '^d') as "special". For example, $ is the regular expression meaning "end of line". Shells also use $ to mark the next token as a shell variable, whose value is to be interpolated into the string. The ^ is an ancient synonym for |, the pipe stdout of left-hand-side into stdin of right-hand-side. The single quotes keep ^ from being treated specially.

The semantics are to not pass any lines of ls -l output that are marked as directories. ls -l puts a 'd' as the first character of any line pertaining to a directory.

The -g option to sort causes the command to look for representations of numbers in the key field, and sort according to numeric value, not as strings. The default sort order is smallest to biggest, so the -r option causes it to sort biggest to smallest. The -k 5 says to use field #5 as the key field. sort uses whitespace characters (blanks, tabs, etc.) to divide a line of text into "fields" by default. On my Arch Linux box, the 5th whitespace-separated field of ls -l is the size of the file in bytes.

Semantics here are sorting files by size in bytes, biggest first.

You are sorting files (not directories) by size in bytes, and putting the information about the largest two files into the text file.

Editorially, it's generally considered bad form to parse the output of ls; see Why you shouldn’t parse the output of ls(1). Historically ls had different formats on different machines, and a script that assumed, say file size is field 5, would give hard-to-understand problems on another machine. So watch out for that.

  • "The ^is an ancient synonym for |". Wow! Thanks for that info. – PM 2Ring Feb 11 '16 at 7:39
  • Wait, I still don't understand the single quotes. I found another example where someone used grep ^processor to find the word processor, but he didn't use single quotes. – whatwhatwhat Feb 11 '16 at 13:57
  • @whatwhatwhat - some people are just in the habit of single-quoting every regular expression. Given that for some shells, $, *, parentheses, square brackets, | and ^ all have special meaning, it's probably just safer to single quote all regexps. I know that I do, even when the regexp doesn't contain any characters that have special meaning to shells. – Bruce Ediger Feb 11 '16 at 14:17
  • Ahhh ok I see. Sounds good. Thanks so much for your help!! – whatwhatwhat Feb 12 '16 at 3:00
0

Single quotes '' are used to indicate the start and end of the characters.

-g flag in sort compare according to general numerical value.

-r option make the output sorted from highest to lowest, since the default output of sort is lowest to highest.

-k option is used if we want to sort on the column or word position in lines of text file.

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.