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
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.
| Folder | What is in it |
|---|---|
/ | The root of the tree. Everything starts here. |
/boot | The kernel and the files to start the system. |
/dev | Device files: disks (/dev/sda), terminals, /dev/null. |
/etc | Configuration files of the system and services. |
/home | The home folders of the users. |
/root | The home folder of root. |
/usr | Programs (/usr/bin), libraries, documentation. Mostly read-only. |
/var | Data that changes: logs (/var/log), web sites, mail, caches. |
/tmp | Temporary files for everybody. Cleaned regularly. |
/run | Runtime data (process IDs, sockets). Empty after a reboot. |
/opt | Extra software from other vendors. |
/srv | Data that this server offers (FTP, web). |
/mnt, /media | Places to mount file systems by hand / removable media. |
/proc, /sys | Virtual 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.
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 xfsdf -hTshows each mounted file system, its type and how full it is.findmntshows the mounts as a tree.lsblkshows 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 /.
Use wildcards to work on many files
The shell expands wildcards to matching file names before the command runs.
| Wildcard | Matches | Example |
|---|---|---|
* | Any text (also nothing) | ls *.conf |
? | Exactly one character | ls file?.txt |
[abc] | One of these characters | ls [abc]* |
[a-z] | One character in this range | ls host[0-9] |
[!abc] | One character not in the list | ls [!a]* |
{a,b} | Each word in the list (brace expansion) | mkdir {dev,test,prod} |
The last line copies messages to messages.bak: {,.bak} makes two words.
Make, enter and remove folders
mkdir makes a folder, cd goes into it, pwd shows where you are, rmdir removes an empty folder.
Use absolute and relative paths
An absolute path starts at / and works from everywhere. A relative path starts in the current folder.
| Path | Meaning |
|---|---|
/etc/ssh/sshd_config | Absolute: the same file from any folder |
ssh/sshd_config | Relative: only works when you are in /etc |
. | The current folder |
.. | The folder above |
~ | Your home folder (~student = the home of student) |
Examentip
Use absolute paths in scripts, cron jobs and config files. They always work.
List files with ls
ls shows the content of a folder. With options it shows owners, sizes, times and hidden files.
| Option | What it shows |
|---|---|
-l | Long list: type, permissions, links, owner, group, size, date |
-a | Also hidden files (names that start with .) |
-h | Sizes like 4.0K, 12M |
-t / -r | Sort by time / reverse the order (-lrt = newest at the bottom) |
-d | The folder itself, not its content |
-R | Also all sub-folders |
-i | The inode number |
-Z | The SELinux context |
Onthoud
ls -lrt = "what changed last?" (look at the bottom).
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.
Move and rename files
mv moves a file to another place, or gives it a new name. It is the same action.
Delete files and folders
rm deletes files; with -r it deletes folders and all their content. There is no trash can.
| Option | Meaning |
|---|---|
-r | Recursive: folders and everything inside |
-f | Force: no questions, no error for missing files |
-i | Ask 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.
Make hard links and soft links
A hard link is a second name for the same data (same inode). A soft link (symbolic link) is a small file that points to a path.
Every file has an inode with the data and the permissions. A file name is just a pointer to an inode. ls -i shows the inode number, and the second column of ls -l shows how many names point to it.
- Make a hard link and a soft link.
- Look at them.
ls -li data.txt data-hard.txt ~/hosts-soft - Remove the original and see what happens.
| Hard link | Soft link | |
|---|---|---|
| Points to | The inode (the data) | A path (a name) |
| Other file system | Not possible | Possible |
| Link to a folder | Not possible | Possible |
| Original deleted | Data still there | Link is broken |
A hard link only works inside one file system. Also, a normal user may not make a hard link to a file of another user (a safety rule of the kernel).
Onthoud
soft link = shortcut to a name. Hard link = another name for the same data.
Examentip
ln -s TARGET LINKNAME: the target comes first, like cp.
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.
| Option | Meaning |
|---|---|
-c | Create an archive |
-x | Extract an archive |
-t | List the content |
-v | Show the file names while working |
-f FILE | The archive file (always the last letter before the name) |
-z / -j / -J | Compress with gzip / bzip2 / xz |
-C DIR | Extract into this folder |
- Create a compressed archive of a folder.
tar -czvf /root/etc-backup.tar.gz /etc - List what is inside without extracting.
tar -tvf /root/etc-backup.tar.gz - Extract it into another folder.
- 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.
Compress single files
gzip, bzip2 and xz compress one file and replace it with a smaller file. The un versions bring it back.
| Tool | Compress | Decompress | Ending |
|---|---|---|---|
| gzip | gzip file | gunzip file.gz | .gz (fast) |
| bzip2 | bzip2 file | bunzip2 file.bz2 | .bz2 (smaller) |
| xz | xz file | unxz file.xz | .xz (smallest, slow) |
| zip | zip arch.zip files | unzip 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.
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| Test | Meaning |
|---|---|
-name "x*" | Name (with wildcards; quote them) |
-iname | Name, ignore upper/lower case |
-type f / -type d / -type l | File / folder / link |
-user NAME / -group NAME | Owner / group |
-size +10M | Bigger than 10 MiB (- = smaller) |
-mtime -7 | Changed in the last 7 days |
-perm -4000 | Has 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.
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 -akeeps owner, permissions and time stamps;mvmoves or renames;rm -rdeletes 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 -czfmakes a gzip archive,tar -xfunpacks,tar -tflists;-Cpicks the target folder.findsearches by name, size, owner, time and type, and can run a command on every file with-exec.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| FHS | Filesystem Hierarchy Standard: the rules for which folder holds what. |
| Mount | Connecting a file system (disk, partition, network share) to a folder. |
| Absolute path | A path from the root folder, like /etc/hosts. |
| Relative path | A path from the current folder, like ../docs. |
| Wildcard (globbing) | Special characters (*, ?, [ ]) the shell turns into matching file names. |
| Inode | The record that holds the data and properties of a file; a name points to it. |
| Hard link | One more name for the same inode. |
| Symbolic link | A small file that points to another path. |
| Archive | One file that holds many files and folders (tar). |
| Compression | Making files smaller with gzip, bzip2 or xz. |
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).