Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 1 · Basics · Lesson 01 · 40 minutes

Find your way in the file system

After this lesson you can
  • Move between folders and say where you are
  • Create, copy, move and delete files and folders
  • Read the long listing of a file, and sort it by name, size and time
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

It is your first week as a junior analyst. A server has shown a strange login, and your team lead gives you access to it. "Collect the log files for this case in one folder, keep a copy of each, and tell me where they are." The server has no desktop. You have a terminal, a keyboard and a path to find.

The idea

Linux keeps every file in one tree. The top of the tree is /, the root. (root is also the name of the administrator account. The two meanings are not related.) A folder is called a directory. A path is the route to a file through the tree: each / in a path means "go into".

The path /home/you/krlab/linux-basics
TopUnder /Under /homeYour lab/ (root)homeetcvartmpyoukrlablinux-basics1234TopUnder /Under /homeYour lab/ (root)homeetcvartmpyoukrlablinux-basics1234
  1. / (root) → home: go into home
  2. home → you: go into you
  3. you → krlab: go into krlab
  4. krlab → linux-basics: go into linux-basics

Some directories have a fixed job on every Linux system:

PathWhat it is
/home/youYour home directory. The short name is ~.
/etcSettings of the system and its services.
/var/logLogs.
/tmpTemporary files. Every user can write here.
/usr/binPrograms.
/procNot real files: a live view of the kernel (the core of the system) and the processes.

An absolute path starts at the root: /var/log/syslog. A relative path starts where you are: notes/todo.txt. Two names are special: . is the directory you are in, and .. is the directory above it.

The shell (the program that reads your commands) always has one working directory. A relative path starts there.

Worked example

1. Make your lab folder

Every course on KaliRange works in its own folder under ~/krlab. You need three commands:

  • mkdir (make directory) creates a directory. The option -p (parents) also creates the directories above it, and gives no error if they exist.
  • cd (change directory) moves you into a directory.
  • pwd (print working directory) shows where you are.
mkdir -p ~/krlab/linux-basics
cd ~/krlab/linux-basics
pwd
/home/you/krlab/linux-basics

2. Make folders and a file

echo prints the text that you give it. The sign > sends that text into a file and replaces what the file held before. find . lists everything under the directory you are in, and sort puts the lines in order. The sign | gives the output of one command to the next command.

mkdir -p cases/2026/alpha notes
echo "first note" > notes/todo.txt
find . | sort
.
./cases
./cases/2026
./cases/2026/alpha
./labcheck.sh
./notes
./notes/todo.txt

labcheck.sh is the script of the set-up lesson.

3. Copy, then move

cp (copy) makes a second file with the same contents. mv (move) moves a file to a new place. In Linux, to rename a file is to move it to a new name. ls (list) shows the contents of a directory.

cp notes/todo.txt cases/2026/alpha/
mv cases/2026/alpha/todo.txt cases/2026/alpha/plan.txt
ls cases/2026/alpha
plan.txt

4. Read the long listing

ls -l (long) shows one line of facts for each file. -a (all) adds the hidden entries, whose names start with a dot.

ls -la cases/2026/alpha
total 4
drwxrwxr-x 2 you you 60 Nov 21 08:31 .
drwxrwxr-x 3 you you 60 Nov 21 08:31 ..
-rw-rw-r-- 1 you you 11 Nov 21 08:31 plan.txt

The numbers 60 and total 4 depend on the file system: on yours they can be 4096 and total 8. Read the last line from left to right:

The long listing of plan.txt
-rw-rw-r--type and permissions1names that point to the fileyouthe owneryouthe group11size in bytesNov 21 08:31time of the last changeplan.txtthe name-rw-rw-r--type and permissions1names that point to the fileyouthe owneryouthe group11size in bytesNov 21 08:31time of the last changeplan.txtthe name

The first character of the type is - for a file and d for a directory. Lesson 10 explains the permissions. The size is 11, because "first note" has 10 characters and echo adds a line end.

5. Change the size format and the order

seq 1 1000 prints the numbers from 1 to 1000, one on each line, so it makes a file of a known size. Make three files of different sizes. Make them in this order, because the sort by time below depends on it. sleep 1 waits one second, so that each file gets its own time.

mkdir sizes
seq 1 1000 > sizes/numbers.txt
sleep 1
echo "tiny" > sizes/small.txt
sleep 1
echo "a short line of text, a little longer" > sizes/medium.txt
ls -l sizes
total 12
-rw-rw-r-- 1 you you   38 Nov 21 10:46 medium.txt
-rw-rw-r-- 1 you you 3893 Nov 21 10:46 numbers.txt
-rw-rw-r-- 1 you you    5 Nov 21 10:46 small.txt

Without an option, ls sorts by name. -h (human-readable) writes a size as 3.9K instead of 3893.

ls -lh sizes
total 12K
-rw-rw-r-- 1 you you   38 Nov 21 10:46 medium.txt
-rw-rw-r-- 1 you you 3.9K Nov 21 10:46 numbers.txt
-rw-rw-r-- 1 you you    5 Nov 21 10:46 small.txt

Three more options change the order. -S (size) puts the biggest file first. -t (time) puts the file that changed last at the top. -r (reverse) turns any order round. These commands have no -l, so they print only the names.

ls -S sizes
ls -t sizes
ls -tr sizes
numbers.txt
medium.txt
small.txt
medium.txt
small.txt
numbers.txt
numbers.txt
small.txt
medium.txt

The three lists are the order by size, the order by time with the newest first, and the same order turned round. ls -lt and ls -lhS combine the options with the long listing.

6. Move with relative and absolute paths

Go into the case folder, go two levels up, then return to the lab folder with an absolute path.

cd cases/2026/alpha
cd ../..
pwd
cd ~/krlab/linux-basics
/home/you/krlab/linux-basics/cases

cd - returns to the directory you were in before. cd with no path goes home.

7. Look at the system folders

With -d (directory), ls lists the directory itself, not its contents.

ls -ld /etc /tmp /var/log /proc
drwxr-xr-x 130 root root   12288 Nov 21 08:02 /etc
dr-xr-xr-x 512 root root       0 Nov 18 09:14 /proc
drwxrwxrwt  18 root root     440 Nov 21 08:30 /tmp
drwxrwxr-x  12 root syslog  4096 Nov 21 00:00 /var/log

Your numbers and dates differ. The owner of each is root, the administrator. Look at /tmp: every user can write there, and the t at the end stops one user from deleting the files of another.

8. Delete, with care

rm (remove) deletes a file. rm -r (recursive) deletes a directory and everything in it. There is no bin and no undo.

rm -r cases/2026
ls cases

The second command prints nothing, because cases is now empty. Before you run rm -r, run ls on the same path and read what you are about to delete.

rmdir (remove directory) is the careful way to delete a directory: it deletes only an empty one. Make one empty directory and one that holds a file, then try both.

mkdir empty full
echo "keep" > full/note.txt
rmdir empty
rmdir full
rmdir: failed to remove 'full': Directory not empty

empty is gone, and full is still there. When you expect a directory to be empty, use rmdir: if you are wrong, it stops and tells you, and rm -r does not. Now clean up with rm -r full.

Your turn

Now do the task of your team lead. There are no steps here: use the commands of the worked example.

In ~/krlab/linux-basics:

  1. Make the directory evidence/host1/logs.
  2. In that directory, make the file auth.txt that holds one line: login failed
  3. Put a copy of that file in evidence/host1 with the name auth-copy.txt.

Then answer from your own terminal.

Last, give the copy the name auth-backup.txt and check with ls that the old name is gone.

Check yourself

You are in /home/you/krlab/linux-basics. Where does "cd ../../.." take you?

To /home. Each .. goes one level up: krlab, then you, then home.

Why does a script that works in your folder fail when another program starts it?

A common cause is a relative path. The other program starts the script in a different working directory, so notes/todo.txt points to a different place. Use absolute paths in scripts, or change to a known directory first.

In the listing, the line for /tmp starts with "drwxrwxrwt". What do the first and the last letter tell you?

d says that it is a directory. t is the sticky bit: in this directory only the file owner, the directory owner or root can delete or rename it, although all can write.

What is the difference between "rm notes" and "rm -r notes"?

rm notes fails, because notes is a directory. rm -r notes deletes the directory and all that it contains, without a question.

Field notes

  • In an investigation, do not change what you examine. cp -a keeps the owner, the permissions and the times of a file; a plain cp gives the copy a new time. Work on the copy.
  • ls does not show everything. A name can hold a space, a newline or a control character. In a script use find -print0 with xargs -0, and quote every variable.
  • A path under /tmp is a classic weakness: a file with a name that another user can predict lets that user put a link in its place. Make temporary files with mktemp.
  • /proc answers questions that files cannot. ls -l /proc/<pid>/exe shows the program that a process really runs, even when the file was deleted from the disk.
  • rm removes the name, not the data. For data that must not come back, use full-disk encryption and destroy the key; do not trust an overwrite on an SSD.