TT Lab
Get started
Learn Learning paths Courses

LFCS — Linux Foundation System Administrator

Handling Files and find Expressions

Continue in TT Lab

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

  1. Create four directories, src, archive, report, and patch, under /root/lfcs-ess/.
  2. 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.
  3. 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).
  4. 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.
  5. 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.
  6. 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.
  7. 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).
  8. 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.

Notes

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.