Linux script question: Write a script to output all of the file names that have
ID: 3605561 • Letter: L
Question
Linux script question: Write a script to output all of the file names that have at least 2 hard links. Use the stat command similar to the script in chapter 10.F.
note: Chapter 10.F is the picture below:
f. Using the tool of your choice (egrep, awk, a shell script), provide an instruction using stat to obtain all files in /etc that have at least 7 hard links, stat -c "%n %h" gives you the name and hard links. Which files were found? Include in your answers the instruction or script that you wrote.Explanation / Answer
Script:
ls -l etc | awk '(index($1,"d") == 0) {print "etc/" $9}' | xargs stat -c "%n %h" | awk '$2 >= 2 {print $1}'
Explanation:
ls:
-l etc -> list out all the files and directories in etc.
awk:
index($1,”d”==0) -> filters out directories from the ls result, leaving only files.
{print “etc/”$9} -> outputs relative path of the file. Eg: etc/file1.txt.
‘$2 >= 2 {print $1}’ -> prints the filename having link greater than 2.
xargs:
Takes the output of awk and inputs it to stat command.
stat:
-c -> to provide output format.
%n -> File name.
%h -> Hard links.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.