4 Navigation
In this chapter we will learn the basics of the linux folder structure and how to create folders and files and navigate it.
4.1 Linux folders structure (Filesystem Hierarchy Standard)
Linux filesystem follows the FHS (https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard). This hierarchy defines the names and location of the system folders, containing the necessary files and configurations to run the operative system.
In Linux, the base of the filesystem is /, and it serves a the root folder where the rest of the hierarchy resides. The most relevant folders for our course are:
/home: Users’ personal folders
/bin: Commands and tools (binaries) that are necessary to run and maintain the system.
/etc: System wide configuration files.
There is an important difference here between Windows and Linux, and is the character used to separate folders when describing a path to a file. In Linux it would be
/home/victor/Documents/my_file.txt
while in Windows is
C:\Users\victor\Documents\my_file.txt
4.2 Listing files
When you login in a linux server you will be at your user personal folder. It can be checked with the pwd (print working directory) command:
victor@aula:~$ pwd
/home/victorIn this case, I’m in the folder victor (my personal folder), located at /home. To see what’s inside of the folder, we can use the ls (list) command:
victor@aula:~$ ls
victor@aula:~$ As my user is new, and I haven’t created any file or subfolder yet, my personal folder is empty. But is it really?
victor@aula:~$ ls -a
. .. .bash_history .bash_logout .bashrc .cache .cloud-locale-test.skip .profile .sudo_as_admin_successfulMost linux commands and tools have a manual that can tell us the options (flags) we have available. In this case you can use man ls to see all the possibilities.
Files and folders in Linux starting with a dot (.) are hidden, and they don’t appear unless we use the -a flag.
4.3 File operations (create, copy, move, delete)
As a normal user, we can access most of the folders (/bin, /etc…), but we can only modify our personal folder.
We will see later how to become a superuser with permissions to modify system configurations and installing new tools and software.
4.3.1 Creating folders
We can create folders with mkdir (make directory). For example, if we are in our home folder and we want to create a folder named projects we do the following:
victor@aula:~$ mkdir projectsAnd now if we check with ls we can see the new folder:
victor@aula:~$ ls -la
total 36
drwxr-x--- 4 victor victor 4096 Jun 24 10:09 .
drwxr-xr-x 4 root root 4096 Jun 2 15:17 ..
-rw------- 1 victor victor 164 Jun 11 12:55 .bash_history
-rw-r--r-- 1 victor victor 220 Jun 2 15:17 .bash_logout
-rw-r--r-- 1 victor victor 3771 Jun 2 15:17 .bashrc
drwx------ 2 victor victor 4096 Jun 2 15:21 .cache
-rw-r--r-- 1 victor victor 0 Jun 2 15:17 .cloud-locale-test.skip
-rw------- 1 victor victor 20 Jun 24 09:22 .lesshst
-rw-r--r-- 1 victor victor 807 Jun 2 15:17 .profile
drwxrwxr-x 2 victor victor 4096 Jun 24 10:09 projects
-rw-r--r-- 1 victor victor 0 Jun 2 15:22 .sudo_as_admin_successfulIf we want to create a hierarchy of folders, for example projects/CREAF/linux_course, we can do it using the -p flag. This flag tells mkdir to create any intermediate folder that don’t exists yet:
victor@aula:~$ mkdir -p projects/CREAF/linux_course
victor@aula:~$ ls -la projects/CREAF/
total 12
drwxrwxr-x 3 victor victor 4096 Jun 24 10:16 .
drwxrwxr-x 3 victor victor 4096 Jun 24 10:16 ..
drwxrwxr-x 2 victor victor 4096 Jun 24 10:16 linux_course4.4 Moving through folders
To move between folders we can use cd (change directory). So, to navigate to the recently created folder:
victor@aula:~$ cd projects/CREAF/linux_course
victor@aula:~/projects/CREAF/linux_course$ We can see that the prompt has changed, indicating now that we are in the /home/victor/projects/CREAF.
In linux we can use relative paths or absolute paths. A relative path takes as base our current working directory as the base and buiold the path from there. An absolute path always start in the root folder (/). So, if we are in /home/victor and want to move to /home/victor/projects/CREAF/linux_course these two are equivalent:
- absolute path:
victor@aula:~$ cd /home/victor/projects/CREAF/linux_course
- relative path:
victor@aula:~$ cd projects/CREAF/linux_course
4.5 Creating empty files
Sometimes we need to create empty files to start working. This can be easily done with touch:
victor@aula:~/projects/CREAF/linux_course$ touch analysis.R
victor@aula:~/projects/CREAF/linux_course$ ls -la
total 8
drwxrwxr-x 2 victor victor 4096 Jun 25 10:15 .
drwxrwxr-x 3 victor victor 4096 Jun 24 10:16 ..
-rw-rw-r-- 1 victor victor 0 Jun 25 10:15 analysis.R4.6 Editing files
For quick and short edits, we can use nano, a simple terminal editor available in most linux servers:
victor@aula:~/projects/CREAF/linux_course$ nano analysis.R4.7 Copying files
We can copy files with cp, providing the source and the destination:
victor@aula:~/projects/CREAF/linux_course$ cp analysis.R analysis_old.RFor copying folders, we need the -r flag to recursively copy all files and folders inside.
If the destination exists, it WILL BE OVERWRITTEN without any warning
If instead of copying we want to move the files from one folder to another, the we can use mv in the same way as we use cp.
4.8 Deleting files
To remove (delete) files we can use rm and the path to the file (using the -r flag for folders).
This removes permanently the files or folders selected. There is no way back, so be sure what you really want to remove and double check the spelling.