RHCSA EX200 - Input/Output Redirection LAB
Use Input/Output Redirection
Create a Server Health Log File
-
Create a server health log file that contains a sequential number of outputs with the hostname, date and time, and a simple header:
cloud_user@server1: ~ $ { echo " " ; echo "==== `date` on `hostname` ====" ; df -hT ; } ==== Wed May 1 20:53:07 UTC 2024 on server1 ==== Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 1.9G 17M 1.9G 1% /run tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/xvda2 xfs 20G 14G 6.7G 67% / tmpfs tmpfs 373M 4.0K 373M 1% /run/user/1001 -
Rerun the previous command, and output this to a text file:
{ echo " " ; echo "==== `date` on `hostname` ====" ; df -hT ; } > `hostname`-health.txt -
Review the output:
cloud_user@server1: ~ $ cat server1-health.txt ==== Wed May 1 20:53:51 UTC 2024 on server1 ==== Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 1.9G 17M 1.9G 1% /run tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/xvda2 xfs 20G 14G 6.7G 67% / tmpfs tmpfs 373M 4.0K 373M 1% /run/user/1001Observe what was added to the file.
-
Rerun the first
echocommand to create the server health log file twice more:{ echo " " ; echo "==== `date` on `hostname` ====" ; df -hT ; } > `hostname`-health.txt -
Review the output using
cat server1-health.txtagain to see how many times the command was run. -
Change the server health log file to contain a double redirect to ensure that the initial and subsequent outputs are not overwritten:
{ echo " " ; echo "==== `date` on `hostname` ====" ; df -hT ; } >> `hostname`-health.txt -
Run the above command 2 more times.
-
Inspect the output again using
cat server1-health.txt.
cat server1-health.txt
cloud_user@server1: ~ $ cat server1-health.txt
==== Wed May 1 20:58:43 UTC 2024 on server1 ====
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 17M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/xvda2 xfs 20G 14G 6.7G 67% /
tmpfs tmpfs 373M 4.0K 373M 1% /run/user/1001
==== Wed May 1 21:00:24 UTC 2024 on server1 ====
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 17M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/xvda2 xfs 20G 14G 6.7G 67% /
tmpfs tmpfs 373M 4.0K 373M 1% /run/user/1001
==== Wed May 1 21:00:25 UTC 2024 on server1 ====
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 17M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/xvda2 xfs 20G 14G 6.7G 67% /
tmpfs tmpfs 373M 4.0K 373M 1% /run/user/1001
Search for Files in the /home Directory
-
Find all the files in the
/homedirectory owned by thecloud_user:find /home -user cloud_userObserve a long list of filenames, along with 2
Permission deniederrors at the end. -
Generate clean lines of output without the 2 errors:
find /home -user cloud_user 2> /dev/null -
Find out how many clean lines of output there are:
find /home -user cloud_user 2> /dev/null | wc -l -
Save the output to a text file:
find /home -user cloud_user 2> /dev/null > cloud_user-files.txt -
Ensure the filenames are in the text file:
cat cloud_user-files.txt -
Number the list of filenames, and save them in another text file:
nl cloud_user-files.txt > numberedfiles.txt -
Ensure the numbered filenames are in the text file:
cat numberedfiles.txt -
Sort the numbered lines:
sort numberedfiles.txtObserve that because there were no leading zeroes in front of the lower numbers, it doesn't sort properly.
-
To fix this, run a numeric sort:
sort -n numberedfiles.txt -
Generate a list showing the first-level directories inside
/etc:find /etc -maxdepth 1Observe the output includes files that go 1 level further than then
/etcdirectory. -
Add on to the previous command to generate a list showing the space used for the first-level directories inside
/etc:find /etc -maxdepth 1 -iname "*.*" -exec du -sh {} \;Observe the previous list now includes total space usage for each item.
-
Rerun the following command, and sort it by space usage from least to most used:
find /etc -maxdepth 1 -iname "*.*" -exec du -sh {} \; | sort -h -
Rerun the previous command, and this time, output it to
etc-space-usage.txt:find /etc -maxdepth 1 -iname "*.*" -exec du -sh {} \; | sort -h > etc-space-usage.txtYou shouldn't see any output aside from 2 directory errors.
-
Review the file:
less etc-space-usage.txtObserve the files listed and sorted by space usage.
Use grep and Regular Expressions to Analyze Text
-
Find all the files owned by the
cloud_userin the/homedirectory:find /home -user cloud_user -
Find all the files owned by the
cloud_userin the/homedirectory that contain the word "file":cloud_user@server1: ~ $ find /home -user cloud_user | grep -i file /home/cloud_user/.bash_profile find: ‘/home/ssm-user’/home/cloud_user/numberedfiles.txt /home/cloud_user/cloud_user-files.txt : Permission denied find: ‘/home/devops’: Permission deniedNotice in the output that it only searches for the word "file" in the actual filenames rather than within the contents of the file.
-
Use the
-execfeature offindto find the word "file" in the files themselves:find /home -user cloud_user -exec grep -i file {} \; -
Find out how many lines of output were found:
find /home -user cloud_user -exec grep -i file {} \; | wc -lYou should see
140lines at the bottom of the output. -
Count how many lines were generated without errors:
find /home -user cloud_user -exec grep -i file {} \; 2> /dev/null | wc -lYou should again see
140lines. Note that this counts only the standard out rather than the standard error. -
Utilize the
grepcommand directly to find the word "file" inside all files owned bycloud_userin the/homedirectory:grep -ir file /usr/share/doc/zip -
Add a total count of the output to the bottom of the list:
grep -ir file /usr/share/doc/zip ; !! | wc -lYou should see
965at the bottom. -
Run a case-sensitive search for only the word "file" as lowercase:
grep -ir file /usr/share/doc/zip ; grep -r file /usr/share/doc/zip | wc -lYou should see
925at the bottom. -
Create a text file containing all the search results for the word "file":
grep -ir file /usr/share/doc/zip ; grep -r file /usr/share/doc/zip > grepoutput.txt -
Check the text file output:
cat grepoutput.txt
No Comments