Basic file management
Copying, moving, deleting and archiving files, plus the glob patterns the shell expands before any of those commands ever see an argument.
Lesson 3 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.
The shell expands globs, not the command
When you type rm *.txt, rm never sees an asterisk: the shell expands it and passes the resulting file names. * matches any run of characters including none, ? matches exactly one, [abc] matches one of a set and [!abc] matches anything outside it. A pattern that matches nothing is passed through unchanged by default, which is why a mistyped pattern produces a "no such file" naming the pattern itself.
Because expansion happens first, quoting a pattern hands it to the command literally — which is what find requires: find . -name "*.txt" must be quoted or the shell expands it in the current directory before find runs.
Moving things about safely
cp copies, with -r or -a needed for directories; -a additionally preserves permissions, timestamps and links, which is what you want for a backup. mv both moves and renames, since renaming is moving within a directory. rm deletes, -r recursively and -f without asking; rmdir removes only an empty directory, which is occasionally the safer tool. -i on any of these prompts before overwriting or deleting.
mkdir -p creates a whole path at once and does not complain if it already exists. touch creates an empty file or, on an existing one, updates its timestamps. file guesses a file's type from its contents rather than its name — the correct answer whenever a question notes that extensions mean nothing on Linux.
Archives and compression are two separate things
tar collects many files into one archive and does not compress by itself; gzip, bzip2 and xz compress a single file and do not archive. Combining them is why tar has compression flags: tar -czf a.tar.gz dir creates with gzip, -cjf uses bzip2, -cJf uses xz, and swapping c for x extracts while t lists. The f flag names the archive file and must come last in a bundled group, because the file name follows it.
Each compressor has three spellings and they are all one operation: gzip compresses and gunzip or gzip -d reverses it, bzip2 compresses and bunzip2 or bzip2 -d reverses it, xz compresses and unxz or xz -d reverses it. Every one of them REPLACES the file it was handed unless -k is given to keep it, so bunzip2 dump.sql.bz2 leaves dump.sql and no .bz2 behind. The -c flag writes to standard output instead, leaving the original alone. cpio is the older archiver that reads its file list from standard input, which is why it appears alongside find.
dd copies blocks rather than files and is the odd one out in this whole objective, because it takes no conventional options at all — every part of it is a key=value operand. dd if=source of=target names the input and output, and each falls back to standard input or standard output when it is left off. bs= sets the block size; count= stops the copy after that many INPUT blocks, so bs=1M count=100 copies exactly a hundred megabytes and no more; skip= skips blocks at the start of the input and seek= skips them at the start of the output, which is the pair most often confused; conv= asks for a transformation such as conv=notrunc. Nothing asks for confirmation and a block device accepts raw writes, which is why of= is the operand to read twice and why swapping if and of destroys the wrong disk.
Worth carrying in
- cp -a
- Recursive copy preserving permissions, timestamps and links.
- mkdir -p a/b/c
- Create the whole path; no error if it exists.
- rmdir
- Remove an empty directory only.
- file NAME
- Identify a file by its contents, not its extension.
- tar -czf a.tgz dir
- Create a gzip-compressed archive. -x extracts, -t lists.
- gzip -c
- Compress to standard output, leaving the original in place.
- dd if= of= bs=
- Block-level copy. Used for images and whole devices.
- dd count= skip= seek=
- Stop after this many input blocks; skip blocks of the input; skip blocks of the output.
- bunzip2 file.bz2
- Decompress in place, replacing the file.
bzip2 -dis the same; -k keeps the archive. - [!abc]
- Glob matching one character outside the set.
What the exam does with this
tardoes not compress andgzipdoes not archive. Questions are built on candidates assuming otherwise.- The shell expands globs before the command runs — which is why find patterns are quoted.
- rmdir fails on a non-empty directory;
rm -rdoes not. ddhas no ordinary options: everything is key=value. skip= is the input side and seek= the output side, and a question will offer them the wrong way round.
- Objective
- 103. GNU and Unix commands
- Share of the exam
- 43.33% (the whole objective)
- Questions in this lesson
- 20
- Signed for by a person
- 0
Partly checked. None of the 20 questions here has been read against the cited source by a person. 20 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.
Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
Drill this lesson
A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.
Practise Basic file management
Questions in this lesson
- Which command finds files under /data whose names end in .txt, .TXT or .Txt in a single search? machine-checked
- The directory /srv/old still contains files and subdirectories. Which command removes it together with everything inside it? machine-checked
- You have been handed backup.tar and want to see what it contains before unpacking anything. Which command lists the archive's contents without extracting it? machine-checked
- A downloaded file is called `data` with no extension. Which command inspects its content and reports what kind of file it is? machine-checked
- A directory contains the single file log2024.txt. Select the THREE shell glob patterns that match that file name. machine-checked
- Neither project nor project/src exists yet. Type the complete command that creates the directory project/src/main in a single invocation, creating the missing parent directories as needed. machine-checked
- You must duplicate /srv/www to /srv/www.bak so that the copy keeps every file's ownership, permissions and timestamps, recurses into subdirectories, and stores symbolic links as links rather than as copies of their targets. Which command does all of that? machine-checked
- You are renaming report.txt to report.old and are not certain whether a report.old already exists. Which command performs the rename but asks for confirmation before overwriting an existing destination? machine-checked
- The path archive/2023/logs is empty, and so are its parents archive/2023 and archive once it is gone. You want all three removed in one command, and the command must stop with an error rather than delete any directory that still has something in it. Which command do you run? machine-checked
- A script refreshes the timestamps of log files but must never bring a missing file into existence. Type the short option for touch, including its leading dash, that suppresses the creation of files that do not already exist. machine-checked
- Select the THREE true statements about GNU find's time-based tests. machine-checked
- An audit asks for every regular file under /srv larger than 100 MiB. Which command produces that list? machine-checked
- A tree under /var/www contains roughly 200,000 files whose mode must be set to 644. You want find to do it while starting as few chmod processes as possible. Which command meets that requirement? machine-checked
- You have site.tar.gz in your home directory and need its contents unpacked under /opt/site, without changing your working directory first. Which command does that? machine-checked
- Type the complete tar command that creates the bzip2-compressed archive /backup/etc.tar.bz2 from the directory /etc, using single-letter options. machine-checked
- Select the TWO true statements about dd. machine-checked
- You want to archive every file under /etc with cpio, feeding it the list of names produced by find. Which command builds the archive? machine-checked
- The file dump.sql.bz2 is in the current directory. Type the complete command, using no options at all, that decompresses it back to dump.sql. machine-checked
- Select the THREE true statements about ls options. machine-checked
- A directory holds file1.txt, file2.txt and file10.txt. Which glob pattern matches the first two but not file10.txt? machine-checked
Practise Basic file management