Table of Contents Link to heading
- Introduction
- Directory Structure
- Home Directory
- Current Directory
- Change Directory
- Paths
- Listing Files in a Directory
Introduction Link to heading
- In Linux, everything is stored in files.
- Files are used to store data such as text, graphics, and programs.
- Directories (or folders for Windows and MAC OS X users) are a type of file used to store other files.
- The data that they hold are the names of the files that have been entered into them, along with the inode number (a unique identifier number assigned to each file) for where the data for that file exists on the disk.
- Directories are used to provide a hierarchical organisation structure.
Directory Structure Link to heading
On a Windows system, the top-level of the directory structure is called My Computer.
Physical devices, such as hard drives, USB drives, network drives, show up under My Computer and are each assigned a drive letter, such as C: or D:.
A visual representation of a Windows directory structure:
The Linux directory structure, called a filesystem, also has a top-level called the root directory (symbolised by the slash / character).
Nonetheless,, there are no drives in Linux; each physical device is accessible under a directory, as opposed to a drive letter.
A visual representation of a Linux filesystem:
To view the contents of the root directory, use the ls command with the / character as the argument:
Notice that there are many directories with descriptive names including /boot which contains files to boot the computer.
Home Directory Link to heading
- On most Linux distributions, there is a directory called home under the root / directory.
- Under this /home directory, there is a directory for each user on the system. The directory name is the same as the name of the user, so a user named sysadmin would have a home directory called /home/sysadmin.
- When a user opens a shell, they should automatically be placed in their home
directory.
- The user has the full control to create and delete additional files and directories in their home directory.
- Most other directories in a Linux filesystem are protected with file permissions.
- The home directory has a special symbol used to represent it, the tilde ~ character.
- It is also possible to refer to another user’s home directory by using the tilde ~ character followed by the name of the user account. For example, ~henry will be the equivalent of /home/henry.
Current Directory Link to heading
The
pwd
(print working directory) command can be used to determine where the user is currently located within the filesystem.
Change Directory Link to heading
The
cd
(change directory) command can be used to navigate the filesystem.
- To move from the home directory into the .vim directory, use the directory name as an argument to the cd commandk
- When used with no arguments, the cd command will take the user to their home directory.
- If the user tries to change to a directory that does not exist, the command returns a “No such file or directory” error.
Paths Link to heading
A path is a list of directories separated by the / character. There are two types of paths: absolute and relative.
- For example, /home/sysadmin is a path to the home directory:
Absolute Paths Link to heading
allow the user to specify the exact location of a directory, starting at the root directory and thus always begins with the / character.
- The path /home/sysadmin is an absolute path; it tells the system to begin at the root / directory > move into the home directory > then into the sysadmin directory.
Relative Paths Link to heading
give directions to a file relative to the current location in the filesystem.
- The user must currently be in a directory that contains objects in the path.
- Relative paths start with the name of a directory.
Shortcuts Link to heading
- The single period . character always represents the current directory.
- Two period .. characters always represents one directory higher relative to the current directory, sometimes referred to as the parent directory.
Listing Files in a Directory Link to heading
The
ls
(list) command is used to display the contents of a directory and can provide detailed information about the files. When used with no options or arguments, it lists the files in the current directory.
- The ls command can also be used to list the contents of any directory in the filesystem by being provided with the path to the directory as an argument.
Listing Hidden Files Link to heading
- A hidden file is any file (or directory) that begins with a dot . character.
- The ls command omits hidden files by default.
- To display all files, including hidden files, use the
-a
option to the ls command. - Most of the hidden files are customisation files, designed to customise how
Linux, your shell, or programs work.
- For example, the .bashrc file in the home directory customises features of the shell, such as creating or modifying variables and aliases.
- These customisation files are not ones that you work with on a regular basis, and having them displayed makes it more difficult to find other files.
Long Display Listing Link to heading
Each file has details associated with it called metadata, this can include information such as the size, ownership, or timestamps.
Use the
-l
option to the ls command to view this information.In the output above, each line describes metadata about a single file.
drwxr-xr-x 5 kali kali 4096 Nov 24 22:11 dictionary *rw-r--r-- 1 kali kali 45665 Nov 24 22:11 vimrc
File Type: [drwxr-xr-x 5 kali kali 4096 Nov 24 22:11 dictionary]
- The first character of each line indicates the type of file. The file types are:
Symbol | File Type | Description |
---|---|---|
d | directory | A file used to store other files |
- | regular file | Includes readable files, images files, binary files, and compressed files |
l | symbolic link | Points to another file |
s | socket | Allows for communication between processes |
p | pipe | Allows for communication between processes |
b | block | file Used to communicate with hardware |
c | character file | Used to communicate with hardware |
- Permissions: [drwxr-xr-x 5 kali kali 4096 Nov 24 22:11 dictionary]
- The next nine characters demonstrate the permissions of the file.
- Permissions indicate how certain users can access a file.
- Hard Link Count: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- This number indicates how many hard links point to this file.
- User Owner: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- Every file is owned by a user account.
- This is important because the owner has the rights to set permissions on a file.
- Group Owner: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- Indicates which group owns this file, this is important because any member of this group has a set of permissions on the file.
- File Size: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- Size of files in bytes.
- To present the file size in a more human-readable size, like 1K 234M 2G
etc., add the
-h
option. The command beomes:ls -lh
.
- Timestamp: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- Indicates when a file’s contents were last modified.
- File Name: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
- The name of the file or directory.
Listing Directories Link to heading
The
-d
option is used to refer to the current directory, and not the contents within it.
- To display metadata of of the /home/kali directory, use a combination of -l and -d options.
Recursive Listing Link to heading
When you want to display all of the files in a directory as well as all of the files in all subdirectories under a directory.
- To perform a recursive listing, use the
-R
option to the ls command. - In the example, the files in the current directory were listed first and the files in subdirectories were listed after.
- Be careful with this option; for example, running the command on the root directory would list every file on the filesystem, including all files on any attached USB device and DVD in the system. Limit the use of recursive listings to smaller directory structures.
Sort a Listing Link to heading
- To sort files by size, we can use the
-S
option. - While the -S option works by itself, it is most useful when used with the -l option so the file sizes are visible.
- It may also be useful to use the
-h
option to display human-readable file sizes. - The
-t
option sorts files based on the time they were modified. - For more detailed modification time information you can use the
--full-time
option to display the complete timestamp (including hours, minutes, seconds). It will assume the-l
option automatically. - It is possible to perform a reverse sort by using the
-r
option. It can be used alone, or combined with either the-S
or-t
options.