# Simple File System Manipulation
# Filesystem Structure
In Linux the file system is structured like a tree. As all classical trees in computer science the root sits at the top and the branches are at the bottom. The sharp observer will note that this is contrary to the way this is done by nature as can be observed while taking a walk through a park.
/
denotes the root of the tree.
TIP
Run ls /
to see the first set of nodes located directly under the root.
Be aware that in Linux everything is a file. This also holds true for directories and even hardware devices. This simple concept enables some incredibly powerful interaction concepts.
# The home directory
There is no place like home. The home directory might be a confusing term for
new Linux users. Contrary to intuition it does not refer to the directory
/home
but rather to the directory that you as user have full control over,
that is YOUR home directory.
Your home directory is a sub-directory in the directory /home
and is named
identical to your user. If your username is john
then your home directory is
located at /home/john
. The shorthand ~
or more precisely ~username
can be
used to reference a home directory. For John ~
will expand to ~john
and
cd ~
will take you home.
# Basic orientation and movement
Command | Meaning |
---|---|
pwd | prints the path of the current working directory |
cd | changes the directory |
. | denotes the current directory |
.. | denotes the parent of the current working directory |
# ls
The ls
command as you might already have guessed is used to list the content of a directory
TIP
Look it up in the man pages to learn more.
# Copying files
TIP
Explore the cp
command. Check how you can avoid overwriting files during copy
and how you can copy directories.
# Moving files
TIP
Explore the mv
command. Figure out how to rename files, move directories and
avoid overwriting targets.
# Creating and deleting files
TIP
Find a command to create an empty file and how you can create a file containing some simple text without using an editor. Delete the files you just created.
# Creating and deleting directories
TIP
Create a directory, add a file to it and delete it without separately deleting the file first.
# Finding Files
One of the bigger challenges of a file system user is usually finding stuff. Linux offers a very handy command for helping with this issue
find [starting dir] [search options] [criteria] [result options]
for example
find /etc -name hosts
will find all files named hosts
in /etc
usually you would write
find /etc -name hosts 2> /dev/null
what this does will be explained later 😃
Another example would be
find /etc -size 10c -ls 2>/dev/null
This will return all files in /etc
that are exactly 10 bytes (10c
) and
return them as list with details.
Here are some useful search options for you:
Option | Meaning |
---|---|
-maxdepth | How deep to search the directory tree |
-group | Find files belonging to this group (more on groups later) |
-user | Find files belonging to a specific user |
-iname | case insensitive name search |
-mmin | Find files based on last modified in minutes |
-type | Find files of a specific type |