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 7Naam in de cursus: Managing Files from the Command Line

Manage files from the command line

Daily admin work is full of small file jobs: make a folder, copy a config file before you change it, move logs, delete old data, make links. This chapter shows the commands for each job and the traps that delete data by mistake.

  • 7 Lessen
  • 13 Commando's
  • 1 Labs

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

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

Vaardigheden in dit hoofdstuk

  • Bestanden en mappen kopiëren, verplaatsen, linken en verwijderen

7.1

Create files and folders

mkdir makes folders, touch makes empty files (or updates the time of an existing file).

mkdir reports
mkdir -p projects/web/{html,logs,backup}
mkdir -m 750 private
touch notes.txt
touch -d "2026-01-01 08:00" old.log
ls -l old.log
-rw-r--r--. 1 student student 0 Jan  1  2026 old.log
  • mkdir -p makes all missing parent folders and gives no error if the folder exists — good in scripts.
  • mkdir -m 750 sets the rights at once.

7.2

Copy files and folders

cp SOURCE TARGET copies. For folders you need -r. Use -a to keep owner, rights and dates.

cp /etc/hosts ~/hosts.bak
cp file1 file2 file3 /tmp/
cp -a /etc/ssh /root/ssh-backup-$(date +%F)
cp -r projects /tmp/
cp -i notes.txt /tmp/
cp: overwrite '/tmp/notes.txt'? n
cp -v *.conf /backup/
cp -u *.html /var/www/html/
OptionWhat it does
-rCopy folders with everything inside
-aArchive: like -r, and keep rights, owner, dates, links, SELinux context
-iAsk before you overwrite
-vShow each file
-uCopy only when the source is newer
-pKeep rights, owner and dates (for files)

Op het werk

Before you edit a config file, copy it: cp -a /etc/ssh/sshd_config{,.orig}. The braces make /etc/ssh/sshd_config /etc/ssh/sshd_config.orig.

7.3

Move and rename

mv moves a file to another folder or gives it a new name. There is no separate rename command.

mv notes.txt notes-2026.txt
mv notes-2026.txt ~/Documents/
mv *.log /var/tmp/oldlogs/
mv -i report.pdf /srv/share/
mv -n data.csv archive/
mv projects/web projects/website
  • -i asks before it overwrites, -n never overwrites.
  • Moving inside the same file system is fast (only the name changes). Moving to another disk copies the data and then deletes the source.

Commando's in deze les

7.4

Delete files and folders

rm deletes files, rm -r deletes folders with all content, rmdir deletes only empty folders. There is no recycle bin on the command line.

rm old.log
rm -i *.tmp
rmdir emptydir
rm -r /tmp/test
rm -rf /tmp/build-*
rm -- -strange-name.txt
OptionWhat it does
-iAsk for every file
-IAsk once when you delete more than 3 files or go into folders
-rDelete folders and everything inside
-fForce: no questions, no error when a file is missing
--End of options: the next word is a file name, even if it starts with -

Let op

rm -rf with a variable is dangerous: if $DIR is empty, rm -rf $DIR/* becomes rm -rf /*. In scripts write rm -rf "${DIR:?}"/* — Bash stops when the variable is empty.

Onthoud

Deleted = gone. Copy important data to a backup before big clean-ups.

7.6

Find files

find searches the folder tree by name, size, owner, time or type, and can run a command on every hit.

find /etc -name "*.repo"
find / -user anna 2> /dev/null
find /var/log -type f -size +100M
find ~ -mtime -1
find /tmp -type f -mtime +30 -delete
find /srv -type f -name "*.sh" -exec chmod 750 {} \;
locate sshd_config
TestFinds
-name "*.conf" / -inameBy name (with -iname big and small letters are the same)
-type f / -type d / -type lFiles / folders / links
-size +100MBigger than 100 MiB (- = smaller)
-user anna / -group salesBy owner / group
-mtime -1 / -mtime +30Changed in the last day / more than 30 days ago
-perm -4000With the SUID right
-exec CMD {} \;Run CMD on every hit ({} = the file)

More options: the RHCSA lesson about find.

Commando's in deze les

7.7

Look at file details

stat shows everything about a file: size, rights, owner, inode and the three times. file says what kind of data is inside.

stat /etc/hosts
  File: /etc/hosts
  Size: 158             Blocks: 8          IO Block: 4096   regular file
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Modify: 2026-10-20 10:12:04.000000000 +0300
file /usr/bin/passwd backup.tar.gz
/usr/bin/passwd: setuid ELF 64-bit LSB pie executable, x86-64
backup.tar.gz:   gzip compressed data
du -sh ~/Downloads
1.2G    /home/student/Downloads

Oefenen

Alle commando's in dit hoofdstuk