-2

These two commands can save the size of a file into a variable, as I am new to linux I cant understand there working. Kindly explain how they works.

    ls -l | grep testing.txt | cut -f6 -d' '

    filesize=$(stat -c '%s' testing.txt)

These commands are taken from: linux shell file size

Community
  • 1
  • 1
Napster
  • 161
  • 9
  • The first command does *not* save the size of a file to a variable. – lurker Mar 23 '15 at 19:54
  • That's a really awful way to collect file size on Linux -- it's extremely open to bugs. Don't do it that way. – Charles Duffy Mar 23 '15 at 19:54
  • ...in fact, why don't you ask for the *right* way to do it, instead of asking how a wrong way works? – Charles Duffy Mar 23 '15 at 19:54
  • Thanks @lurker for your reply, I know that first command didn't save it to the variable, but I want to know that what is grep or cut -f6 -d ' ' is doing. – Napster Mar 23 '15 at 19:55
  • @CharlesDuffy sure, I can ask it, will you answer that question? – Napster Mar 23 '15 at 19:56
  • 1
    `man cut` and `man grep` are good ways to find out. – Ulrich Schwarz Mar 23 '15 at 19:57
  • Have you checked the man page for `grep` and for `cut`? `grep` only allows text lines in its in put pass through to the output that match the given expression (in this case, that match `testing.txt`). And `cut` selects certain fields from the text. In this case, field number 6 with a delimiter of space. – lurker Mar 23 '15 at 19:57
  • 1
    Pay attention to Charles' comment. listing a directory and limiting file selection with `grep` and piping to `cut` is a train wreck. If there are multiple files, e.g. (`limit-testing.txt, time-testing.txt, ...`) you can end up with unwanted results. – David C. Rankin Mar 23 '15 at 19:58
  • It doesn't work at all because of variable length fields. The size field varies in size , and when it does you get junk. – Robert Jacobs Mar 23 '15 at 19:59
  • You'd probably have better luck trimming the output of `ls` using `sed`, but the stat command is a much better way to go. – Eric Hughes Mar 23 '15 at 20:01
  • @EricHughes, ...I'd actually suggest `awk`, given its default behavior of treating runs of separators as a single column separation, if for some reason one had no choice but to parse `ls` (being, for instance, on a system with no GNU `stat` and no `perl`, and a version of `wc -c` that doesn't optimize by using `stat` data). – Charles Duffy Mar 23 '15 at 20:04
  • @CharlesDuffy good point! I too often just reach for `sed` and mash regexes together until something works. – Eric Hughes Mar 23 '15 at 20:10

3 Answers3

2

Don't do that. Really, don't.

stat --format=%s testing.txt

...relies on having GNU stat, but is much more efficient and reliable (on any system where GNU stat is available).

filesize=$(stat --format=%s testing.txt)

...does the same thing, but saves the result into a variable named filesize (just as you'd need to do with the ls result to save its result for use anywhere else).


On a system without GNU stat, you might also consider:

filesize=$(wc -c <testing.txt)

...which will, if written by a sane person, seek to the end of the file and check its position, rather than reading all contents (a much more expensive test).


ls -l | grep testing.txt | cut -f6 -d' '
  1. lists all files (rather than looking only at the one file you care about), thus being much slower on large directories (with directories with tens of thousands of files or more, this could take minutes).
  2. filters only for names that contain testing.txt (where the . can be any character -- it would find testingatxt as well, or sometesting8txtbye.jpg).
  3. dividing the results into columns by spaces, takes the 6th column from each line found.

Note that step 3 is particularly error-prone, because the specification for ls doesn't make any promises about how many spaces will exist between each column in ls -l, so there's no guarantee that this will work the way you expect.

See also Why You Shouldn't Parse the Output of ls(1).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • The %s is for size is it possible to save the permissions of a file in a variable like string – Napster Mar 23 '15 at 20:13
  • `man stat` will give you a list of available format strings. `%a` and `%A` will, as that documentation notes, give you access writes in either a numeric (octal) value or a human-readable one, as preferred. – Charles Duffy Mar 23 '15 at 20:16
  • One more thing, you said that filesize=$(stat --format=%s testing.txt) this command is a better option what if I want to put a variable instead of testing.txt? – Napster Mar 23 '15 at 20:22
  • @AliSajid, then use `"$varname"` in place of `testing.txt`. – Charles Duffy Mar 23 '15 at 20:28
0

Only the second command saves anything into anything.

The first command takes the output of ls -l, pipes it to grep which looks for lines matching testing.txt, then pipes those lines to cut, which removes portions of the lines. In this case, it selects only the 6th field -f6, and uses spaces as field delimiters -d' '

The second command performs the stat operation on the file in question, then saves the result to a variable.

Eric Hughes
  • 831
  • 6
  • 19
0
stat --format=%s testing.txt

or

stat -c '%s' testing.txt

moreover you can man the stat page to understand what %s or %A do.

Ali Sajid
  • 3,964
  • 5
  • 18
  • 33
  • What does this add when compared to pre-existing answers? It offers an alternative (already given in other answers), but doesn't answer the literal question (explaining how the original command worked) at all. – Charles Duffy Apr 01 '15 at 11:30