Linux and Unix

Linux files and Directory Structure, Explained

In Linux, everything is a file.

The files are designated by their path; This represents the position of the file in the directory hierarchy. At the top of this hierarchy is the root, designated by

« / ». For Example:

/var/log/btmp

This path designates the btmp file or directory contained in the log directory, itself in the var directory at the root.

The main directories at the root of the file system are the following:

/bin and/sbin Executable files (binaries), respectively for all users or only for root
/boot Boot files
/dev Hardware devices
/etc System configuration files
/Home User directories
/lib Libraries required by programs
/media and mnt Mounting points for non-local file systems
/proc System process information
/root Superuser directory
/var Variable (contains system logs, some libraries, user mailboxes, tasks sent to printers…)

There are two ways to designate a file or directory in Linux: either by its absolute path or by its relative path. The absolute path is not ambiguous; the relative path depends on the context. The difference between the two is that the “/” must always precede the absolute path, and is still absent in the relative path. The relative path preceded by the directory where we are always gives the absolute path.

For example, suppose you find yourself in the directory /Home/Bob/tricks. If you want to see (with the command Cat) a file named File1 in this directory, it can be done with two commands:

Cat /home/Bob/Stuff/File1 ( absolute Path)

(relative path)

Cat file1

And be careful, if we make the following command:

Cat home/Bob/stuff/file1

This is not an absolute path (the initial “/” is missing). Linux will try to find a file whose absolute path would correspond to /home/Bob/trucs/home/Bob/trucs/file1.

Basic commands

Cat Displays the contents of a file
Cd Change Directory
Cp Copying a file
less Displays the contents of a file line by line (see also more)
Ls Displays the contents of a directory
Man Shows how to use a command
Mkdir Creates a Directory
Mv Moves a file
Nano Basic text editor
Pwd Displays current Directory
Rm Deletes the specified file
Rmdir Deletes the specified directory
Knew Log in as another user
Touch Creates an empty file
Whoami Displays the name of the current user

The grep command

Allows you to search for a sequence of characters in a file. The sequence of characters can be represented by a regular expression. The result of the command displays all the rows where the sequence was found.

Syntax : grep [option] string file

Option description
-c Displays only the number of matching rows in each

file

-I Ignores case when searching
-l Displays only the names of the files where a match has been

Found

-L Displays the names of files that do not contain the searched string
R Search recursively from the specified directory

To find the word “http” in the /etc/services file:

grep http /etc/services

To search for the word “http” in all files in the /etc directory:

grep http /etc/*

To find all files that contain “term” in the /var/log directory:

Grep -l term /var/log/*

To find all files that contain “term” or “TERM” in the /var/logdirectory:

Grep -Li term /var/log/*

It is also possible to redirect the output of a command to grep; for example, to find all active bash processes :

PS- e | grep bash

File system structure

The structure of the ext4 file system can be represented as follows:

The information on the disk is logically grouped into block groups.

Each block group contains a superblock and inodes, which consist of metadata about the files in the Group (permissions, creation dates etc.). The superblock is the same in all block groups: this redundancy increases security in the event of an error, because the superblock contains information about the entire file system.

Each inode contains information about a file in the block group and pointers to the physical allocation units where that file is written.

Each inode is associated with an entry in the list of files as it is presented to the user:

Shortcuts

There are two ways to create shortcuts to files in Linux: using links “hard” (hardlinks) or with symbolic links. The usual method is that of symbolic links.

A hard link is to create an entry in the file system that points to an inode

existing, as follows:

The peculiarity of this type of links is that when the user deletes the file A, the inode will not be deleted because there is an entry that points to it.

Symbolic links are associated with their own inode, and this inode points to data that contains the path to the original file:

The peculiarity of this type of link is that if the original file is deleted, there will remain no traces and the symbolic link will point to a path that does not exist.

The command to create links is LN ; the -s option has the effect that the link will be symbolic.

Alias

An alias is a shortcut to a command. It is defined in the . bashrc file according to the following syntax:

alias name= ‘command to replace