zaiRuh
zaiRuh

Reputation: 1

Bash: List file size using ls , awk and grep

I entered the following commands on the terminal:

ls -l someFile | awk '{print $5}' | grep [0-9]

Based on my understanding, the ls -l prints the lists of directories and files with long format (including the permissions) within the current directory, so piping the awk (to print desired field from the list, which is the size of file) and grep [0-9] gave me the correct output that I want, which is the file size highlighted in red.

For example: drwxr-xr-x 2 zdgx6 students 103 Feb 23 2017 delosreyes_hw1

Output is 103 (since that's the size and the font is red)

However, when I tried this on my Bash script like this: echo " $file size: $( ls -l "$file" | awk '{print $5}' | grep [0-9] ) $regfile "

it outputs the file size correctly but it's not highlighted in red. So I assume that my syntax may have been wrong but i didn't get any errors.

Any idea why that might be?

Upvotes: 0

Views: 3949

Answers (3)

zaiRuh
zaiRuh

Reputation: 1

Thanks all! I find that using the grep --color=always was the simplest solution for my question. The other suggestions worked as well but we haven't discussed those commands in class. Here's my code:

 echo " $file $( ls -l "$file" | awk '{print $5}' | grep --color=always [0-9] ) $regfile " 

Upvotes: 0

rici
rici

Reputation: 241771

Gnu grep will highlight the matched part of each line if:

  • you supply the command-line argument --color=always [Note 1], or

  • stdout is a terminal and you either supply the command-line argument --color=auto or do not specify --color (because auto is the default setting).

Running grep inside $(...) in order to capture the output as part of a bash expansion means that stdout will be redirected to a pipe, which is not a terminal. So unless you specify --color=always, match coloring will be disabled. That's usually want you want when you are processing the output of grep.

So you could "fix" this by using the --color=always option, but really the simpler solution is to send colour control codes directly, since that is the only reason you are using grep.

Colour codes can be sent in a reasonably portable way using the tput utility, which is part of the ncurses package and will generally be installed on any Linux/BSD system. You'll want the following codes:

tput bold    # Sets boldface (Otherwise, the colour will be washed out)
tput setaf 1 # 1 is red. 2 is green, 3 yellow, 4 blue, 5 magenta, 6 cyan and 7 white

After you output the highlighted text, you'll need to reset the console to normal:

tput sgr0    # Normal colour and style

So you could do, for example:

echo "$file size: $(tput bold)$(tput setaf 1)$(ls -l "$file" | awk '{print $5}')$(tput sgr0) $regfile"

If you were doing that a lot, you might want to save the tput outputs in bash variables:

bold_red=$(tput bold)$(tput setaf 1)
reset_col=$(tput sgr0)

echo "$file size: $(tput bold)$(tput setaf 1)$(ls -l "$file" | awk '{print $5}')$(tput sgr0) $regfile"

You could also hard-code typical console codes if you know what they are:

printf "%s size: \033[1;31m%s\033m %s\n" "$file" "$(ls -l "$file" | awk '{print $5}')" "$regfile"

Notes

  1. For the benefit of those of us with a different notion of English orthography, grep allows both --color and --colour. To avoid confusion, I used the first one in the text here, although I typically use the second out of habit.

Upvotes: 1

P....
P....

Reputation: 18381

echo "$(tput setaf 1)$(stat -c '%B' file.log)$(tput sgr0)"
512  

You should not use ls to parse the details of any file, you can use stat instead. You can issue stat --help to check the details of various flags provided by stat command. Also by doing this, you do not have to use any additional pipe to feed into awk.

Upvotes: 1

Related Questions