How to look for all filesystem which start with /filesystem - linux

When I do df -h I get following output
/dev/sda 3.7T 34M 3.7T 1% /filesystem1
/dev/sdb 3.7T 34M 3.7T 1% /filesystem2
/dev/sdc 3.7T 34M 3.7T 1% /filesystem3
I am using following command to get this list but it is giving blank
df -h | grep ^filesystem
Please help to find correct command

Your command looks for filesystem at the beginning of the line. There are two problems with that:
The string you're looking for begins with /filesystem, not filesystem; and
it's not at the beginning of the line, it's the last field in the line.
You can use awk and get the last field with $NF.
df -h | awk '$NF ~ /^\/filesystem/'

Related

converting the output of df -h into an array and then modified it's output

I want to converting the output of df -h into an array and then modified it's output with bash script.
For example the output of command is:
Filesystem Size Used Avail Use% Mounted on
/dev/sda4 28G 480M 26G 2% /var
/dev/sda2 28G 45M 26G 1% /tmp
/dev/sda5 275G 4.6G 256G 2% /home
tmpfs 790M 84K 789M 1% /run/user/1000
The output should be:
Filesystem Size Used Avail Use%
device4 28G 480M 26G 2%
device2 28G 45M 26G 1%
device5 275G 4.6G 256G 2%
Tmp 790M 84K 789M 1%
I know I should set IFS=$'\n' to have this output in an array but I have no idea how can I recognize device name and replace with the proper name.
Thank you for helping me to solve the problem.
This works for me:
#!/bin/bash
df -h | sed 's#\(.*\) \(.*\)$#\1#' | sed 's#Mounted##' | sed 's#/dev/sda\(.\)#device\1 #'
the first sed removes the last column
the second sed removes the word "Mounted". The last column header is "Mounted on", so the first sed only removes "on".
the last sed replaces /dev/sda by device.

display drives with lsblk -o name -lpn , extract string containing "sd"

So I've looked all over the place for what to do here and just find "from file". I am looking to extract from a command output.
Task: display the absolute path names of disks beginning with the sd.
Current progress: displaying absolute path name of disks
[host /]$ lsblk -o name -lpn
/dev/sda
/dev/sda1
/dev/mapper/centos-root
/dev/sda2
/dev/md127
....
Desired output
/dev/sda
/dev/sda1
/dev/sda2
....
I've played around with cut, print, awk and sed.
Got syntax errors or no output.
grep
lsblk -o name -lpn | grep "/dev/sd"
awk
lsblk -o name -lpn | awk '/dev\/sd/{print}'
sed
lsblk -o name -lpn | sed -n '/\/dev\/sd/p'
Output:
/dev/sda
/dev/sda1
/dev/sda2
lsblk accepts arguments, so you can in many cases just as easily say
lsblk -no name -lp /dev/sd?
The ? is just wildcard match of a single character, so sda, sdb, ...
There is a potential pitfall. If you have more than 26 disks or you are working on a system that uses those unique disk identifiers. In which case you need to change the wildcard to an asterisk and filter out only the unique results,
lsblk -no name -lp /dev/sd* | sort -u
Output (for either command):
/dev/sda
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sdb
/dev/sdb1
/dev/sdb2
Try with sed:
lsblk -o name -lpn |sed -n '/\/sd/p'

Reading only two characters before a specific pattern in a linux file

I have a file like
Filesystem Size Used Avail Use% Mounted on
/abc/xyz/mnop
82G 7.7G 70G 10% /
hello 32G 922M 31G 3% /abc/asd
/abc/xyz 477M 118M 334M 27% /asd
/abc/xyz 50G 9.4G 38G 21% /ad
/abc/xyz 79G 27G 49G 36% /asd
/abc/xyz 30G 7.9G 21G 29% /sd
/abc/xyz 197G 2.4G 185G 2% /asd
xyz:/backups/abc
500G 18G 483G 4% /asdas
abc
1.9T 1.5T 405G 79% /media/Scratch
I want only the two characters before % i.e 10,3,27,21 and so on which i will compare one by one with a value 85 whether greater or less. But the first line use% should be skipped. Please suggest me how to get only those values in a variable one by one or in a file which i can compare with 85 using conditional statements.
i used grep -E -o ".{0,2}%" test.txt , but it is giving % with the values e.g 10% and also se% which i don't want.
Thanks
You can use awk:
df -h | awk 'NR>1{print $5+0}'
Or with a file:
awk 'NR>1{print $5+0}' test.txt
Using gnu grep:
grep -oP '\d+(?=%)' test.txt
Use below command it helps to get your desired results
cat text.txt | cut -d'%' -f1 | awk '{print $NF }'
Use perl: perl -ne 'print $1 . "," if /(\d{1,2})\%/' test.txt

bash script that computes used diskspace percentage for given partition

I have a bash script that computes used diskspace percentage for given partition:
df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }
it doesn't work on a partition with long name because the name pushes the other columns to the next line, how do I fix this?
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup60-ROOT
2865493440 2222006740 497927968 82% /
tmpfs 30913808 0 30913808 0% /dev/shm
/dev/sda1 495844 103347 366897 22% /boot
Instead of parsing the entire "table" you could output the usage in percentage directly by using the --output parameter (see man 1 df for details):
$ df -k --output=pcent /dev/sda1
Use%
13%
That should be a lot easier to filter.
E.g. by creating an array with readarray in Bash 4:
$ readarray -t -s 1 percentage < <(df -k --output=pcent /dev/sda1)
$ echo "${percentage[0]// /}"
13%
Assigning the output of df to an array line by line:
$ percentage=($(df -k --output=pcent /dev/sda1))
$ echo "${percentage[1]}"
13%
The -P (portability) option for df use a output format compliant with posix and keeps everything in one line. You can also simplify the awk part using sel.

Bash: Selected Part in BOLD font in bash shell

Is there any way to grep some text from large file and mark that in BOLD letters in Linux BASH shell ?
Like
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 15G 11G 3.3G 76% /
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/sda3 51G 45G 3.8G 93% /home
/dev/sdc1 917G 359G 512G 42% /data
I have above output and I want whenever system mails me about this df output the /data line should be in bold letters.
Using ANSI escape sequence you can do this (though this is terminal dependent):
echo -e "\033[1m$(grep '/data' file)\033[0m"
Will produce:
/dev/sdc1 917G 359G 512G 42% /data
Bash doesn't really have bold (it does have bright which is effectively equivilent). You could use sed to insert the bash control codes, but if you are less concerned with the exact colour choice, you can use
grep --color -E "text match pattern|" mylargefile
To get grep to do the highlighting. (see Colorized grep -- viewing the entire file with highlighted matches for alternatives and discussion)

Resources