Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 3Naam in het boek: Essential File Management Tools

Manage files and folders

This chapter is about the daily work with files: know where things are in Linux, list, copy, move and delete files, make links, and pack files into compressed archives. You need all of it in the exam and every day at work.

  • 16 Lessen
  • 23 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Bestanden archiveren, comprimeren en uitpakken met tar, gzip en bzip2
  • Bestanden en mappen maken, verwijderen, kopiëren en verplaatsen
  • Harde en symbolische links maken

3.1

Know the main folders of Linux

Linux has one tree that starts at /. Every folder has a job; the File System Hierarchy Standard (FHS) says which.

FolderWhat is in it
/The root of the tree. Everything starts here.
/bootThe kernel and the files to start the system.
/devDevice files: disks (/dev/sda), terminals, /dev/null.
/etcConfiguration files of the system and services.
/homeThe home folders of the users.
/rootThe home folder of root.
/usrPrograms (/usr/bin), libraries, documentation. Mostly read-only.
/varData that changes: logs (/var/log), web sites, mail, caches.
/tmpTemporary files for everybody. Cleaned regularly.
/runRuntime data (process IDs, sockets). Empty after a reboot.
/optExtra software from other vendors.
/srvData that this server offers (FTP, web).
/mnt, /mediaPlaces to mount file systems by hand / removable media.
/proc, /sysVirtual folders: live information from the kernel.

Onthoud

settings in /etc, logs in /var/log, programs in /usr/bin, users in /home.

Op het werk

When a disk is full, look first in /var (logs, caches, container images) and /tmp.

3.2

See what is mounted where

A disk or partition becomes part of the tree when it is mounted on a folder. One tree can be made of many file systems.

df -hT
Filesystem            Type   Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs     17G  4.1G   13G  25% /
/dev/vda1             xfs    960M  310M  650M  33% /boot
tmpfs                 tmpfs  1.9G     0  1.9G   0% /dev/shm
findmnt
lsblk
mount | grep xfs
  • df -hT shows each mounted file system, its type and how full it is.
  • findmnt shows the mounts as a tree.
  • lsblk shows the disks and partitions and where they are mounted.

Separate file systems for /boot, /var or /home protect the system: a full /var/log cannot fill /.

Onthoud

df -h = how full, findmnt = the mount tree, lsblk = the disks.

3.3

Use wildcards to work on many files

The shell expands wildcards to matching file names before the command runs.

WildcardMatchesExample
*Any text (also nothing)ls *.conf
?Exactly one characterls file?.txt
[abc]One of these charactersls [abc]*
[a-z]One character in this rangels host[0-9]
[!abc]One character not in the listls [!a]*
{a,b}Each word in the list (brace expansion)mkdir {dev,test,prod}
ls /etc/*.conf
ls -d /etc/[a-c]*
cp /var/log/messages{,.bak}

The last line copies messages to messages.bak: {,.bak} makes two words.

Let op

Test a wildcard with ls or echo before you use it with rm.

3.4

Make, enter and remove folders

mkdir makes a folder, cd goes into it, pwd shows where you are, rmdir removes an empty folder.

  1. Make a folder, also with the parent folders that do not exist yet.
    mkdir /tmp/project
    mkdir -p /tmp/project/app/logs
  2. Move around.
    cd /tmp/project/app
    pwd
    /tmp/project/app
    cd ..
    cd -
    cd
  3. Remove folders: rmdir only when empty, rm -r with all content.
    rmdir /tmp/project/app/logs
    rm -r /tmp/project

cd .. = one folder up, cd - = back to the last folder, cd alone = your home folder.

3.5

Use absolute and relative paths

An absolute path starts at / and works from everywhere. A relative path starts in the current folder.

PathMeaning
/etc/ssh/sshd_configAbsolute: the same file from any folder
ssh/sshd_configRelative: only works when you are in /etc
.The current folder
..The folder above
~Your home folder (~student = the home of student)
cd /etc
ls ssh/sshd_config
cp ../etc/hosts ~/hosts.copy

Examentip

Use absolute paths in scripts, cron jobs and config files. They always work.

3.6

List files with ls

ls shows the content of a folder. With options it shows owners, sizes, times and hidden files.

ls -l
ls -la
ls -lh /var/log
ls -lrt /var/log
ls -ld /etc
ls -R /etc/ssh
OptionWhat it shows
-lLong list: type, permissions, links, owner, group, size, date
-aAlso hidden files (names that start with .)
-hSizes like 4.0K, 12M
-t / -rSort by time / reverse the order (-lrt = newest at the bottom)
-dThe folder itself, not its content
-RAlso all sub-folders
-iThe inode number
-ZThe SELinux context

Onthoud

ls -lrt = "what changed last?" (look at the bottom).

Commando's in deze les

3.7

Copy files and folders

cp SOURCE TARGET copies. Folders need -r. Use -a to keep owners, permissions and times.

cp /etc/hosts /tmp/
cp /etc/hosts /tmp/hosts.bak
cp -r /etc/ssh /tmp/ssh-backup
cp -a /var/www/html /backup/html
cp -a /etc/skel/. ~/test/
  • When the target is a folder, the file keeps its name inside it.
  • -a (archive) = recursive + keep permissions, owner, times, links and SELinux context. Best for backups.
  • /etc/skel/. copies the content of the folder, hidden files included.

Let op

cp overwrites the target without asking (unless you use -i; root has an alias cp -i on RHEL).

Commando's in deze les

3.8

Move and rename files

mv moves a file to another place, or gives it a new name. It is the same action.

mv report.txt /tmp/
mv report.txt report-2026.txt
mv /tmp/old-logs /srv/archive/

Moving inside the same file system is instant (only the name changes). Between file systems, mv copies and then deletes.

Commando's in deze les

3.9

Delete files and folders

rm deletes files; with -r it deletes folders and all their content. There is no trash can.

rm notes.txt
rm -i *.log
rm -r /tmp/build
rm -rf /tmp/build
OptionMeaning
-rRecursive: folders and everything inside
-fForce: no questions, no error for missing files
-iAsk before each file

Let op

rm -rf with a wrong path or a space in the wrong place (rm -rf / tmp/x) can destroy the system. Read the line twice before you press Enter.

Commando's in deze les

3.11

Make and read tar archives

tar packs many files and folders into one archive file, and unpacks it again. With -z, -j or -J it also compresses.

OptionMeaning
-cCreate an archive
-xExtract an archive
-tList the content
-vShow the file names while working
-f FILEThe archive file (always the last letter before the name)
-z / -j / -JCompress with gzip / bzip2 / xz
-C DIRExtract into this folder
  1. Create a compressed archive of a folder.
    tar -czvf /root/etc-backup.tar.gz /etc
  2. List what is inside without extracting.
    tar -tvf /root/etc-backup.tar.gz
  3. Extract it into another folder.
    mkdir /tmp/restore
    tar -xzvf /root/etc-backup.tar.gz -C /tmp/restore
  4. Extract only one file.
    tar -xzvf /root/etc-backup.tar.gz -C /tmp/restore etc/hosts

tar removes the leading / from the names, so extracting never overwrites the real /etc by accident (unless you extract in /).

Onthoud

create, extract, list + verbose + file. Compression: z = gz, j = bz2, J = xz.

Examentip

The exam objective names tar, gzip and bzip2. Know tar -czf, tar -cjf and tar -xf (extract detects the compression by itself).

3.12

Compress single files

gzip, bzip2 and xz compress one file and replace it with a smaller file. The un versions bring it back.

ToolCompressDecompressEnding
gzipgzip filegunzip file.gz.gz (fast)
bzip2bzip2 filebunzip2 file.bz2.bz2 (smaller)
xzxz fileunxz file.xz.xz (smallest, slow)
zipzip arch.zip filesunzip arch.zip.zip (also for Windows)
gzip big.log
ls big.log*
big.log.gz
gunzip big.log.gz
gzip -k big.log
zcat /var/log/messages-*.gz | grep -i error

-k keeps the original file. zcat, bzcat and xzcat read a compressed file without unpacking it.

Op het werk

Old logs in /var/log are compressed by logrotate. Search them with zcat or zgrep instead of unpacking.

3.13

Find files with find

find walks through a folder tree and lists every file that matches your tests (name, owner, size, time …). It can also run a command on each file.

find /etc -name "*.conf"
find / -user student
find / -size +100M
find /var/log -mtime -1
find /home -type d -name ".ssh"

Copy all files of a user to another folder:

mkdir /root/student-files
find / -user student -type f -exec cp --parents {} /root/student-files/ \; 2>/dev/null
TestMeaning
-name "x*"Name (with wildcards; quote them)
-inameName, ignore upper/lower case
-type f / -type d / -type lFile / folder / link
-user NAME / -group NAMEOwner / group
-size +10MBigger than 10 MiB (- = smaller)
-mtime -7Changed in the last 7 days
-perm -4000Has the SUID bit
-exec CMD {} \;Run CMD on each file ({} = the file)

Examentip

Tasks like "find all files of user X and copy them to /root/found" are common. Use -type f, -user, and -exec cp.

Op het werk

find / -perm -4000 -type f 2>/dev/null lists SUID programs: a classic check in a security audit.

3.14

Remember the main points

The most important facts of this chapter, in short.

  • The FHS places files by purpose: /etc (settings), /var (changing data, logs), /home (users), /usr (programs), /tmp (temporary files).
  • An absolute path starts at /; a relative path starts at the current folder.
  • Wildcards: * (any text), ? (one character), [abc] (one of these characters).
  • cp -a keeps owner, permissions and time stamps; mv moves or renames; rm -r deletes folders.
  • A hard link is a second name for the same inode (same file system only); a symbolic link points to a path and can cross file systems.
  • tar -czf makes a gzip archive, tar -xf unpacks, tar -tf lists; -C picks the target folder.
  • find searches by name, size, owner, time and type, and can run a command on every file with -exec.

3.15

Learn the key terms

The words of this chapter in simple English.

TermMeaning
FHSFilesystem Hierarchy Standard: the rules for which folder holds what.
MountConnecting a file system (disk, partition, network share) to a folder.
Absolute pathA path from the root folder, like /etc/hosts.
Relative pathA path from the current folder, like ../docs.
Wildcard (globbing)Special characters (*, ?, [ ]) the shell turns into matching file names.
InodeThe record that holds the data and properties of a file; a name points to it.
Hard linkOne more name for the same inode.
Symbolic linkA small file that points to another path.
ArchiveOne file that holds many files and folders (tar).
CompressionMaking files smaller with gzip, bzip2 or xz.

Commando's in deze les

3.16

Check what you learned

Think about the answer first, then open it.

1Which folder holds most configuration files?

/etc.

2How do you copy a folder with all its files and keep the owners and permissions?
3What happens to a symbolic link when you delete the original file?

It breaks: it points to a path that does not exist any more.

4Why can a hard link not point to a file on another disk?

A hard link is a name for an inode, and inode numbers only mean something inside one file system.

5How do you make a compressed archive of /etc in /root?

tar -czf /root/etc.tar.gz /etc (or -cJf … .tar.xz for xz).

6How do you unpack an archive into /tmp/restore?
7How do you find all files bigger than 100 MiB under /var?
8How do you show the files in a folder with hidden files and details?

Oefenen

Alle commando's in dit hoofdstuk