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.

tar collects; the z is what compresses. They are separate jobs. tar -czf backup.tgz /home — part 1, -c: create (x extracts, t lists); part 2, z: through gzip (j bzip2, J xz); part 3, f: the archive FILE is the next argument; part 4, backup.tgz: so it has to come first, right here; part 5, /home: what goes in. tar 1 -c 2 z 3 f 4 backup.tgz 5 /home 1 create (x extracts, t lists) 2 through gzip (j bzip2, J xz) 3 the archive FILE is the next argument 4 so it has to come first, right here 5 what goes in
tar collects; the z is what compresses. They are separate jobs.

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.

Quoting decides whether the command ever sees the pattern. A ladder of two tests, each asked only on one branch of the test above it, and exactly one way out of each. The first test is: Is the pattern quoted? (the shell reaches it before the command does). Quoted leads to The command is handed the pattern itself (which is why a find -name pattern must be quoted); Unquoted carries on to the next test. On that branch the next test is: Does it match any name in the current directory? It matches leads to The names replace it, and only those arrive (rm never sees an asterisk); It matches nothing leads to The pattern is passed through unchanged (by default, hence a no such file naming the pattern itself). Is the pattern quoted? the shell reaches it before the command does Quoted Unquoted The command is handed the pattern itself which is why a find -name pattern must be quoted Does it match any name in the current directory? It matches It matches nothing The names replace it, and only those arrive rm never sees an asterisk The pattern is passed through unchanged by default, hence a no such file naming the pattern itself
Quoting decides whether the command ever sees the pattern.

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 -d is the same; -k keeps the archive.
[!abc]
Glob matching one character outside the set.

What the exam does with this

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

Practise Basic file management

The rest of objective 103