LFCS — Linux Foundation System Administrator
Handling Files and find Expressions
Goal
You check by hand when the shell expands what, and why find is an expression language. You go once around the LFCS essential commands domain, through links, archives, and patches.
Why it matters
This domain is the stretch that eats up most of the exam time. The problems themselves are not hard, but there is a lot of syntax where a one-character difference changes the result, like -perm 644 and -perm -644, so you are silently wrong. Once you understand links and inodes, operational incidents like "I deleted it but the space did not shrink" start to look like natural consequences. Choosing a compression format is also a trade-off, not a matter of taste — xz makes smaller files but uses far more CPU and time. So the backup interval and the restore time requirement decide the format.
Steps
- Create four directories,
src,archive,report, andpatch, under/root/lfcs-ess/. - Create six files,
data-01.txtthroughdata-06.txt, in/root/lfcs-ess/src/. Each ofdata-01throughdata-03must be at least 200000 bytes, and each ofdata-04throughdata-06must be at least 1 byte and under 1000 bytes. - Create a hard link
src/data-01.hardand a symbolic linksrc/data-01.symtodata-01.txt. Then write two lines inreport/inode.txt:inode=<data-01.txt 의 inode 번호>andlinks=<링크 수>(inode= followed by the inode number of data-01.txt, and links= followed by the link count). - Write only the names of the regular files directly under
src/whose names end in.txtand whose size is over 100k intoreport/big.txt, one per line. - Change the permissions of
data-05.txtanddata-06.txtto 600, and write the names of the regular files directly undersrc/whose permissions are exactly 600 intoreport/perm600.txt. - Change the modification time of
data-06.txtto more than 30 days ago, and write the names of the files directly undersrc/that match-mtime +30intoreport/old.txt. - Make the
src/directory into two archives,archive/src.tar.gzandarchive/src.tar.xz, and write three lines inreport/compress.txt:gzip=<바이트 크기>,xz=<바이트 크기>, andsmaller=<gzip 또는 xz>(the byte size of each, and which of gzip or xz is smaller). - Create four lines,
listen=8080,workers=2,loglevel=info, andtimeout=30, inpatch/conf-old.txt, and four lines,listen=9090,workers=4,loglevel=info, andtimeout=30, inpatch/conf-new.txt. Save the difference between the two files in unified format topatch/conf.patch, and then apply that patch topatch/conf-target.txt, which is a copy ofconf-old.txt, so that it becomes identical toconf-new.txt.
Notes
- For a file of a fixed size, it is quick to make it by cutting only the front part, as in
head -c 200000 /dev/urandom. - If you use
%fin the-printfoffind, only the file name comes out, not the path. -size +100kand-size +100have different units. The latter is a count of 512-byte blocks.- A common mistake: if you apply the patch to the original in step 8, the
conf-old.txtverification breaks. Be sure to apply it to the copy.
Create four working directories
Create four directories, src, archive, report, and patch, under /root/lfcs-ess/.
There is an option that creates the parent path in one go even if it does not exist. You may also list all four directories on one line.
Prepare six files of different sizes
Create six files, data-01.txt through data-06.txt, in /root/lfcs-ess/src/. Each of data-01 through data-03 must be at least 200000 bytes, and each of data-04 through data-06 must be at least 1 byte and under 1000 bytes.
To extract a set number of bytes, just cut the front part from an input device. The three large files must exceed 200000 bytes, and the three small files must be under 1000 bytes.
Hard links, symbolic links, and inodes
Create a hard link src/data-01.hard and a symbolic link src/data-01.sym to data-01.txt. Then write two lines in report/inode.txt: inode=<data-01.txt 의 inode 번호> and links=<링크 수> (inode= followed by the inode number of data-01.txt, and links= followed by the link count).
A hard link attaches one more name to the same inode, so the link count goes up. You can read the inode number and the link count at once with the format output of stat.
Finding by size condition
Write only the names of the regular files directly under src/ whose names end in .txt and whose size is over 100k into report/big.txt, one per line.
The unit of find's size condition differs by suffix. To extract the result as file names only, specify the output format. Also limit the depth so it does not descend into subdirectories.
Finding by permission condition
Change the permissions of data-05.txt and data-06.txt to 600, and write the names of the regular files directly under src/ whose permissions are exactly 600 into report/perm600.txt.
In permission conditions, 'exact match' and 'includes these bits' are different syntax. This time you must use the exact-match one for the result to be right.
Finding by modification time condition
Change the modification time of data-06.txt to more than 30 days ago, and write the names of the files directly under src/ that match -mtime +30 into report/old.txt.
You can change a file's time by giving touch a time directly. The plus sign in -mtime means 'older than that'.
Comparing gzip and xz compression ratios
Make the src/ directory into two archives, archive/src.tar.gz and archive/src.tar.xz, and write three lines in report/compress.txt: gzip=<바이트 크기>, xz=<바이트 크기>, and smaller=<gzip 또는 xz> (the byte size of each, and which of gzip or xz is smaller).
tar selects the compressor with an option letter. After making them, read the byte size of each archive and compare them yourself to write down which is smaller.
Make a patch and apply it to a copy
Create four lines, listen=8080, workers=2, loglevel=info, and timeout=30, in patch/conf-old.txt, and four lines, listen=9090, workers=4, loglevel=info, and timeout=30, in patch/conf-new.txt. Save the difference between the two files in unified format to patch/conf.patch, and then apply that patch to patch/conf-target.txt, which is a copy of conf-old.txt, so that it becomes identical to conf-new.txt.
You get a unified-format difference by adding one option to diff. You must apply the patch to the copy, not the original — if the original changes, the earlier verification breaks.