Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Linux | Red Hat Certified | IT Technology | Operations Engineer

👇 Join the technical exchange QQ group of 1000 people, note 【Public Account】 for faster access

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

What is an Operating System

An operating system is software that manages hardware and software resources.

The operating system manages hardware and software resources (means) to provide users with a more efficient, stable, and secure operating environment (purpose).

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Linux is an open-source operating system that is primarily used on the server backend.

What is a File

File = File Content + File Attributes

File operations: 1. Operate on file content 2. Operate on file attributes

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

If a file is empty, it still occupies disk space because it contains file attributes such as file name, type, and modification time, which also require storage space.

In Linux, files that start with a dot (.) are hidden files; those that start with ‘d’ are directories (dir), and those that start with ‘-‘ are regular file types.

What is a Path

In the Linux system, files and directories on the disk form a directory tree, where each node is a directory or file.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

This tree structure:

Each leaf node must be an empty directory or a regular file.

Any parent node may have multiple child nodes.

Any child node must have only one parent node.

Therefore, starting from /, we can locate a file: /home/bit/test.c, this path has uniqueness.

Using folders (directories) and path separators == a path –> specified file

Note: A path refers to a specific location, which can be a file or a folder. A directory refers specifically to the folder where the file is located.

In Linux, any directory has . and .., creating a directory is the same.

One . acts like a pointer to itself, while two .. act like a pointer to the parent directory. /: The root directory is special; . points to itself, and .. also points to itself. This is because .. helps us find the parent directory, and . helps execute executable programs, such as: ./a.out.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

01. ls Command

Syntax: ls [options] [directory or file]

Function: For directories, this command lists all subdirectories and files under that directory. For files, it will list the file names and other information. Common options:

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

-a List all files in the directory, including hidden files that start with .

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

-l List detailed information about files; files starting with – are regular files, while those starting with d are directories.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

-d Display the directory as a file rather than showing its contents. For example: ls -d specified_directory

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

02. pwd Command

Syntax: pwd

Function: Displays the current path of the user

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

In Windows, the path separator is \, while in Linux, the path separator is /. Through the path, we can locate specific files.

Display the contents of files in a specified path

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

03. cd Command

Syntax: cd directory_name

Function: Change the working directory. Changes the current working directory to the specified directory.

Example:

cd .. returns to the parent directory

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

cd /home/litao/linux/: absolute path

cd ../day02/: relative path

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Absolute Path vs. Relative Path:

a. Generally, when operating in the command line, relative paths are preferred.

b. Absolute paths can find the specified file from any path – used in configuration files.

cd ~: enter user home directory

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

When the root user logs in for the first time, the path is the home directory: /root

When a regular user logs in for the first time, the path is the user’s home directory: /home/username

cd -: jump to the most recent previous directory

04. touch Command

Syntax: touch [options]… file…

Function: The touch command can change the date and time of documents or directories, including access time and modification time, or create a new file if it does not exist.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

05. mkdir Command (Important):

Syntax: mkdir [options] dirname…

Function: Create a directory named “dirname” in the current directory

Common Options:

-p Can be a path name. If some directories in the path do not exist, this option will automatically create those non-existent directories, allowing multiple directories to be created at once.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

06. rmdir Command && rm Command (Important):

rmdir Command

Syntax: rmdir [-p][dirName]

Applicable to: All users with current directory operation permissions

rmdir is a command corresponding to mkdir. mkdir creates directories, while rmdir deletes them.

Function: Delete empty directories

Common Options:

-p If a parent directory becomes empty after a child directory is deleted, it will also be deleted.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

rm Command

The rm command can delete files or directories simultaneously.

Syntax: rm [options][directory/file]

Applicable to: All users

Function: Delete files or directories

Common Options:

-f Even if the file attribute is read-only (i.e., write-protected), it will be deleted directly.

-i Ask for confirmation before deleting each file.

-r Delete directories and all files within them.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Note: * is a wildcard that can match any content.

rm -rf file/* means delete all contents under the file directory.

rm -rf file means delete the file directory and all its contents.

07. man Command (Important):

There are many parameters for Linux commands, and we cannot remember them all. We can get help by checking the online manual. The command to access the Linux manual page is man Syntax: man [options] command

Download the man manual: sudo yum install -y man-pages

Common Options

-k Search online help by keyword.

num Only search in section num.

-a Display all sections. For example, man printf defaults to searching from the first chapter and stops when it finds it. With the a option, when you press q to exit, it will continue searching until all sections are searched.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

is a normal command

is a system call, such as open, write, etc. (With this, at least we can conveniently find out what header files need to be included when calling this function)

is a library function, such as printf, fread. 4 is a special file, which are various device files under /dev.

is the file format, for example, passwd, which explains the meaning of each field in this file.

is reserved for games, defined by each game itself.

is an attachment with some variables, such as global variables like environ are explained here.

is a command for system management, which can only be used by root, such as ifconfig.

08. cp Command (Important):

Syntax: cp [options] src[Source file or directory] dst[Target file/directory]

Common Options:

f or –force Forcefully copy files or directories, regardless of whether the target file or directory already exists.

i or –interactive Ask the user before overwriting the file.

r Recursively process, handling all files and subdirectories under the specified directory. If the source file or directory is not a directory or symbolic link, it is treated as a regular file.

R or –recursive Recursively process, handling all files and subdirectories under the specified directory.

Function: Copy files or directories

1. cp [options] src[Source file or directory] dst[Target file]

In the specified directory (default is current), create and copy src, creating a new file that is identical to the file being copied, using the target file as the file name.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

2. cp [options] src[Source file or directory] dst[Directory]

Copy the specified file/directory (src) to the specified directory

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Note: The cp command is used to copy files or directories. If two or more files or directories are specified, and the last destination is an existing directory, it will copy all previously specified files or directories to that directory.

If multiple files or directories are specified and the last destination is not an existing directory, an error message will appear.

08. mv Command (Important):

The mv command is short for move, which can be used to move files or rename files (move (rename) files). It is a commonly used command in the Linux system, often used for backing up files or directories.

Syntax: mv [options] source file or directory target file or directory

Common Options:

-f: force, meaning if the target file already exists, it will not ask and will directly overwrite.

-i: If the target file (destination) already exists, it will ask whether to overwrite.

Function:

When the second parameter is a file, the mv command renames the file.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

When the second parameter is a directory, the mv command renames the file or moves it to a new directory.

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

Conclusion

To learn Linux well, some basic knowledge is essential. In this article on Linux-related knowledge, we introduced operating systems, files, paths, and various commands such as ls, pwd, cd, etc., which have their respective functions and significance in viewing, operating, creating, deleting, copying, and moving files and directories.

For course inquiries, add: HCIE666CCIE

↓ Or scan the QR code below ↓

Comprehensive Guide to Linux File Management: Essential Knowledge and Practical Commands!

What technical points and content do you want to see?

You can leave a message below to let me know!

Leave a Comment