Learn Linux File System and Common File Operations Easily!

Click the blue text above to follow us

Learn Linux File System and Common File Operations Easily!
Learn Linux File System and Common File Operations Easily!

1. Basics of the Linux File System

1. Characteristics of the Linux File System

  • The Linux file system adopts a tree structure, starting from the root directory root (/).

  • The Linux virtual file system allows many different types of file systems to coexist and supports operations across file systems.

  • Linux files are unstructured character stream files, which do not consider the logical structure inside the file, simply viewing files as a sequence of characters.

  • Linux files can be protected by access permissions set by the file owner or superuser.

  • Linux treats all external devices as files, allowing the same system calls used for file systems to read and write external devices.

2. Components of the Linux File System

Learn Linux File System and Common File Operations Easily!

Linux File System Directory Structure:

/ : The root directory of all files.
/bin : Directory for storing binary executable commands.
/home : The base directory for user home directories; by default, each user’s home directory is set under this directory, for example, the default home directory for user user01 is /home/user01.
/lib : Directory for storing standard program libraries, also known as dynamic link shared library directories; files in this directory are similar to .dll files in Windows.
/etc : Directory for storing system management and configuration files.
/dev : Directory for storing special device files, such as sound card files, disk files, etc.
/usr : The largest directory, storing application programs and file directories.
/proc : A virtual directory that maps to system memory; direct access to this directory can provide system information.
/root : The home directory of the system administrator.
/var : Directory for storing frequently changing files generated by the system, such as spool directories for printers and emails, log files, formatted manual pages, and some application data files.
/tmp : Directory for storing public temporary files.
Learn Linux File System and Common File Operations Easily!

2. Operations of the Linux File System

1. Basic Concepts of Files

Files are collections of related elements defined by the creator, identified by a filename; files can be text documents, images, programs, etc.

The length of filenames in the Linux system varies depending on the type of file system; naming files must adhere to the following rules:

(1) All characters except “/” can be used.
(2) It is best to avoid escape characters such as “?”, “*” (asterisk), “ ” (space), “$”, “&”, etc.
(3) Avoid using “+”, “-” or “.” as the first character of a normal filename (files starting with “.” in Linux are hidden files).
(4) Linux filenames are case-sensitive.
Learn Linux File System and Common File Operations Easily!

2. Wildcards and Shortcut Operations in the Linux System

* (asterisk): Wildcard representing any character (0 or more)
? : Wildcard representing one character
# : Comment
\: Escape character, restores special characters or wildcards to normal symbols
| : Delimits two pipeline commands
; (semicolon): Delimits consecutive commands
~ : User’s home directory
$ : Variable value that needs to be prefixed with a variable
! (exclamation mark): Logical operation of “not”
/ : Path delimiter
>, >> : Both are output redirection, representing “replace” and “append” respectively
‘ (single quote): Does not have variable substitution functionality
” (double quote): Has variable substitution functionality
` : Quote symbol, the command between two ` can be executed first
() (parentheses): Marks the start and end of a subshell
[] (brackets): Character combinations in between
{} (curly braces): Command block combinations in between
Ctrl+C : Terminates the current command
Ctrl+D : Ends input
Ctrl+M : Equivalent to Enter
Ctrl+S : Pauses screen output
Ctrl+Q : Resumes screen output
Ctrl+U : Deletes the entire line of command at the prompt
Ctrl+Z : Suspends the current command
&& : Executes the next command if the previous command executes successfully
|| : Executes the next command if the previous command fails
Learn Linux File System and Common File Operations Easily!

3. File Operation Commands

(1) Display File Command

cat command:

  • Format: cat [options] …[files]…

  • Description: Outputs the contents of multiple files to the screen; if “> filename” is added, it outputs to another file.

  • Common options:

-b: Number all non-empty output lines starting from 1.
-n: Number all output lines starting from 1.
-s: Merges two or more consecutive blank lines into one blank line.
Learn Linux File System and Common File Operations Easily!

more command:

  • Format: more [options] [files]

  • Description: This command displays the contents of a text file, one screen at a time; it stops when the screen is full, and can continue with the following keys:

(1) Space key: Displays the next screen of text by default.
(2) Enter key: Displays the next line of text by default.
(3) d or CTRL+D: Displays half a screen of text down, defaulting to 11 lines.
(4) b or CTRL+B: Displays the previous screen of text by default.
(5) q or INTERRUPT key: Exits the more command.
Learn Linux File System and Common File Operations Easily!
  • Common options:

-num: Specifies an integer indicating how many lines to show per screen.
-d: Displays prompt information at the bottom of each screen, including the current percentage displayed, key prompts, etc.
-c or -p: Does not scroll; clears the screen before displaying the next screen.
+num: Starts displaying from line number num.
+/pattern: Defines a string to search for in the file, displaying from that string (e.g., to search for the string “abc” in a.txt and display from that string, the command would be: more +/abc a.txt).
Learn Linux File System and Common File Operations Easily!

less command:

  • Format: less [options] [files]

  • Description: Similar to the more command, displays file contents screen by screen; the less command allows users to browse the file forwards or backwards, pressing the q key to exit.

  • Common options:

-i: Ignores case when searching unless the search string contains uppercase letters.
-I: Ignores case when searching.
-m: Displays the percentage of the file read.
-M: Displays the percentage of the file read, line number, and total number of lines.
-N: Outputs the line number before each line.
-p pattern: Defines a string to search for in the file, displaying from that string (e.g., to search for the string “abc” in a.txt and display from that string, the command would be: less -p abc a.txt).
Learn Linux File System and Common File Operations Easily!

head command:

  • Format: head [options] …[files]…

  • Description: Used to view the beginning part of a text file, the number of lines is determined by the parameter value, default is 10

  • Common options:

-c: Displays the first num bytes of the file (e.g., to display the first 5 bytes of a.txt, the command would be: head -c5 a.txt).
-n: Displays the first num lines of the file; if this parameter is not specified, it displays the first 10 lines (e.g., to display the first 5 lines of a.txt, the command would be: head -n5 a.txt).
Learn Linux File System and Common File Operations Easily!

tail command:

  • Format: tail [options] …[files]…

  • Description: Used to view the last few lines of a text file, the number of lines is determined by the parameter value, default is 10. If the specified file has more than one, a filename header is added before each displayed file.

  • Common options:

-c: Displays the last num bytes of the file (e.g., to display the last 5 bytes of a.txt, the command would be: tail -c5 a.txt).

-n: Displays the last num lines of the file; if this parameter is not specified, it displays 10 lines (e.g., to display the last 5 lines of a.txt, the command would be: tail -n5 a.txt).

Learn Linux File System and Common File Operations Easily!

(2) Search, Sort, and Remove Duplicate Lines Command

grep command:

  • Format: grep [options] search_pattern [files]

  • Description: The grep family includes grep, egrep, and fgrep. egrep is an extension of grep that can use extended string patterns for searching, while fgrep treats all letters as words and does not recognize regular expressions. If no filename is specified, the grep command searches standard input.

  • Common options:

-b: Before displaying matching string lines, marks the position number of the first character of that line.
-c: Counts the number of lines that match the string.
-E: Interprets the search pattern as an extended regular expression, equivalent to egrep.
-F: Interprets the search pattern as a plain string, equivalent to fgrep.
-i: Ignores case.
-n: Before displaying lines containing the matching string, marks the line number of that line.
-v: Inverts the search, displaying text lines that do not contain the matching string.
-V: Displays version information.
-x: Only displays lines that strictly match the entire line.
Learn Linux File System and Common File Operations Easily!
  • Using regular expressions in the grep command allows for pattern matching to add more rules, enabling more choices when extracting information.

sort command:

  • Format: sort [options] …[files]…

  • Description: The sort command sorts all lines in the specified file, displaying the results on standard output; if no input file is specified or using “-”, it indicates that the sorting content comes from standard input.

  • By default, it sorts by line in ASCII character order.

  • Common options:

-m: Merges files if the given files are already sorted.
-c: Checks if the given file is sorted; if not sorted, prints an error message and exits with status value 1.
-u: Keeps only one line for lines that are the same before and after sorting.
-o filename: Writes the sorted output to the output file instead of standard output.
Learn Linux File System and Common File Operations Easily!
  • Options for changing the default sorting rules include:

-d: Sorts in dictionary order.
-f: Ignores case.
-r: Outputs sorting results in reverse order (default sorting output is in ascending order).
+pos1 – pos2: Specifies one or more fields as sorting keywords; field positions start from pos1 to pos2 (including pos1, excluding pos2). If pos2 is not specified, the keyword is from pos1 to the end of the line, with field and character positions starting from 0.
Learn Linux File System and Common File Operations Easily!

uniq command:

  • Format: uniq [options]… [input_file [output_file]]

  • Description: Checks and removes duplicate lines from a text file.

  • Common options:

-c: In the output, adds the number of occurrences of each line at the beginning of each line.
-d: Only displays each repeated line in the file.
-u: Only displays each line that is not repeated in the file.
Learn Linux File System and Common File Operations Easily!

(3) Commands for Comparing File Contents

comm command:

  • Format: comm [options]… file1 file2

  • Description: The comm command compares two already sorted files, reading the text lines from file1 and file2 to generate three lines of output: lines appearing in both files, lines only appearing in file1, and lines only appearing in file2.

  • Common options:

-123: Options 1, 2, and 3 respectively indicate not displaying the first, second, and third columns of the comm output.
Learn Linux File System and Common File Operations Easily!

diff command:

  • Format: diff [options]… file_list

  • Description: The diff command compares text files and identifies their differences.

  • Common options:

-b: Ignores differences caused by spaces.
-B: Ignores differences caused by empty lines.
-i: Ignores case differences.
-r: Recursively compares files in directories when both files being compared are directories.
Learn Linux File System and Common File Operations Easily!

(4) Commands for Copying, Deleting, and Moving Files

cp command:

  • Format: cp [options] source_file or directory target_file or directory

  • Description: The cp command copies files. If the source is a regular file, this command copies the source file to the specified target file or directory; if the source is a directory, and the target is an existing directory, this command copies all files and subdirectories under the source directory to the target directory; if the source is a directory and the target is not an existing directory, the command returns an error message.

  • Common options:

-a: Equivalent to -dpR.
-d: When copying symbolic links (i.e., shortcut files), the target file or directory is also established as a symbolic link pointing to the original file or directory linked by the source file or directory.
-f: Forcefully copies files or directories.
-i: Requires confirmation before overwriting the target file.
-n: Does not overwrite existing target files.
-p: Copies the file attributes while copying the contents of the source file or directory.
-R or -r: Recursively copies directories, copying all files and subdirectories under the source location.
Learn Linux File System and Common File Operations Easily!

rm command:

  • Format: rm [options] …files or directories…

  • Description: The rm command can delete files or directories; to delete a directory, the “-r” option must be added.

  • Common options:

-f: Forcefully deletes files or directories.
-i: Prompts the user for confirmation before deleting files or directories.
-r or -R: Recursively deletes directories and their subdirectories and corresponding files.
Learn Linux File System and Common File Operations Easily!

mv command:

  • Format: mv [options] source_file or directory target_file or directory

  • Description: The mv command can move files or directories; it can also rename files or directories.

  • Common options:

-i: Requires confirmation before overwriting a file.
-f: If the target file or directory already exists, overwrite it directly.
-n: Does not overwrite existing files.
-u: When moving or renaming a file, if the target file exists and its date is newer than the source file, do not overwrite the target file.
Learn Linux File System and Common File Operations Easily!

(5) File Content Statistics Command

wc command:

  • Format: wc [options] …[files]…

  • Description: The wc command counts the number of bytes, words, and lines in the specified file.

  • Common options:

-c or –bytes: Counts the number of bytes.
-m or –chars: Counts the number of characters.
-l or –lines: Counts the number of lines.
-w or –words: Counts the number of words.
Learn Linux File System and Common File Operations Easily!

(6) Commands for Finding Files and Directories

find command:

  • Format: find [pathnames…] [expressions]

  • Description: The find command is used to search for files and directories that meet certain conditions; pathnames are a list of directory names to search for files, separated by spaces; expressions contain matching criteria or descriptions for the files to find. Expressions are evaluated from left to right, and as long as the test result in the expression is true, the next test is performed.

  • Common expressions:

-amin n: Finds files or directories accessed in the last n minutes.
-anewer<reference file or directory>: Finds files or directories whose access time is closer to the current time than that of the specified file or directory.
-atime n: Finds files or directories accessed in the last n 24 hours.
-cmin n: Finds files or directories changed in the last n minutes.
-cnewer<reference file or directory>: Finds files or directories whose change time is closer to the current time than that of the specified file or directory.
-ctime n: Finds files that have had their status changed in the last n 24 hours.
-depth: Starts searching from the deepest subdirectory under the specified directory.
-daystart: Calculates time starting from today.
-exec<command>: If the return value of the find command is True, it executes that command.
-fstype<file system type>: Only finds files or directories under that file system type.
-gid<group identification number>: Finds files or directories that match the specified group identification number.
-group<group name>: Finds files or directories that match the specified group name.
-inum<inode number>: Finds files or directories that match the specified inode number.
-mmin n: Finds files or directories that have been changed in the last n minutes.
-mtime n: Finds files or directories that have been changed in the last n days.
-name<pattern>: Specifies a string as a pattern for finding files or directories.
-path<pattern>: Specifies a string as a pattern for finding directories.
-perm<permission value>: Finds files or directories that match the specified permission value.
-print: If the return value of the find command is True, lists the file or directory names to standard output, formatted with one name per line.
-prune: Does not search for strings as patterns for finding files or directories.
-size<file size>: Finds files that match the specified file size.
-type<file type>: Only finds files that match the specified file type (b-block device file; d-directory; c-character device file; p-pipe file; l-symbolic link file; f-regular file).
-uid<user identification number>: Finds files or directories that match the specified user identification number.
-user<owner name>: Finds files or directories that match the specified owner name.
Learn Linux File System and Common File Operations Easily!

(7) File Compression and Backup Commands

bzip2 command:

  • Format: bzip2 [options] [file to compress]

  • Description: The compression program for .bz2 files, which deletes the original file.

  • Common options:

-c: Sends the results of compression or decompression to standard output.
-d: Performs decompression.
-f: If the output file has the same name as an existing file during compression or decompression, it overwrites the existing file.
-k: Keeps the original file after compression or decompression.
Learn Linux File System and Common File Operations Easily!

gzip command:

  • Format: gzip [options] [files…]

  • Description: Gzip compression produces compressed files with a “.gz” extension and deletes the original files.

  • Common options:

-c: Outputs the compressed file to standard output without changing the original file.
-d: Decompresses the compressed file.
-f: Forcefully compresses the file.
-n: When compressing the file, does not save the original filename and timestamp.
-N: When compressing the file, saves the original filename and timestamp.
-r: Recursively processes all files and subdirectories under the specified directory.
Learn Linux File System and Common File Operations Easily!

tar (tape archive) command:

  • Format: tar [options…] [files or directories]…

  • Description: Tar is a tool program for creating and restoring files; it can add and unpack files within backup files.

  • Common options:

-A: Adds a tar file to an existing backup file.
-c: Creates a new backup file.
-f<backup file>: Lists the contents of the backup file.
-v: Displays the execution process of the command.
-x: Restores files from the backup file.
–delete: Deletes specified files from the backup file.
Learn Linux File System and Common File Operations Easily!
  • For example (this may be a bit difficult to understand, here’s an example):

  • Back up files 1.txt, 2.txt, and 3.txt in the current directory to file 123.tar, displaying the execution process of the backup command, the command is: tar -cvf 123.tar 1.txt 2.txt 3.txt, the execution result is as follows; you can see that the backup file 123.tar has been generated using the command ls -l

Learn Linux File System and Common File Operations Easily!

Restore the backup file 123.tar by first deleting files 1.txt, 2.txt, and 3.txt, then using the command tar -xvf 123.tar to restore; using the ls -l command, you can see that 123.tar has been restored to 1.txt, 2.txt, and 3.txt, the execution result is as follows:

Learn Linux File System and Common File Operations Easily!

4. Directory Operation Commands

(1) Commands for Switching Working Directories and Displaying Directory Contents

cd command:

  • Format: cd [target directory]

  • Description: Switches the directory to the target directory, provided the user has permission to enter the target directory. The target directory can be an absolute or relative path; if the directory name is omitted, it switches to the user’s home directory.

pwd command:

  • Format: pwd

  • Description: The pwd command without any options or parameters shows the complete path of the “current directory” in the Linux system.

ls command:

  • Format: ls [options]… [files or directories]…

  • Description: The ls command lists the contents of a directory, including the names of files and subdirectories.

  • Common options:

-a: Lists all files and directories in the directory, including hidden files starting with “.”.
-A: Lists all files and directories except for “.” (current directory) and “…” (parent directory).
-b: Lists non-printable characters in file names using backslashes and character numbers.
-c: Outputs the last modification time of the file and sorts based on this time.
-C: Displays files and directories in multiple columns.
-F: Adds file type indicators; “*” (asterisk) indicates executable regular files; “/” indicates directories; “@” indicates symbolic links; “|” indicates pipe files; “=” indicates sockets.
-d: Displays directories like files, rather than showing their contents.
-l: Lists detailed information about files, outputting information in a 7-field list, with specific meanings as shown below:
Learn Linux File System and Common File Operations Easily!

Learn Linux File System and Common File Operations Easily!

(2) Commands for Creating and Deleting Directories

mkdir command:

  • Format: mkdir [options]… directory_name…

  • Description: This command creates a directory named after the directory name, while setting the permissions for that directory. The user creating the directory must have write permission in the current directory, and the directory name cannot be the same as any existing directory or file name in the current directory.

  • Common options:

-m: Sets access permissions for the newly created directory.
-p: If the upper directory of the directory being created does not exist, it will be created as well.
Learn Linux File System and Common File Operations Easily!

rmdir command:

  • Format: rmdir [options]… directory_name…

  • Description: This command deletes one or more directories from a directory. To delete a directory, the user must have write permission on the parent directory, and the directory to be deleted should be empty.

  • Common options:

-p: After deleting the specified directory, if the parent directory becomes empty, it will be deleted as well.
Learn Linux File System and Common File Operations Easily!

(3) Commands for Changing File or Directory Access Permissions

Before introducing this part of the commands, we need to understand the concepts of users, user groups, and access permissions in the Linux system.

The Linux system is a multi-user, multi-tasking time-sharing operating system. Any user wanting to use system resources must first apply for an account from the system administrator and then log into the system with that account. Users are identified internally by UID.

A user group (group) is a collection of users with similar characteristics; each user in the Linux system belongs to a user group, which allows the system to manage all users in a user group centrally. User groups are identified internally by GID.

Linux defines four types of users: file owner, group user, other user, and superuser.

Linux defines three types of access permissions: read (r), write (w), and execute (x).

The ls -l command can display detailed information about files or directories, where the first field is the file attribute field. In this field, the first letter indicates the file type; the three segments following the “-” symbol consist of “r”, “w”, and “x”, which represent the file owner permissions, group user permissions, and other user permissions, respectively.

ls -l.

Learn Linux File System and Common File Operations Easily!

After understanding these concepts, we can introduce the related commands.

chmod command:

  • Format: chmod [who] [opt] [mode] file or directory name…

  • Description: Used to change the access permissions of files or directories. Who represents the object, which can be u (file owner), g (group user), o (other users), or a (all users).

    Opt represents the operation, which can be symbols such as + (add a permission), – (remove a permission), = (assign given permissions and remove existing permissions).

    Mode represents permissions, which can be a combination of r (read), w (write), x (execute).

Permissions can also be changed using numeric notation with the chmod command.

  • Format: chmod [mode] file or directory name…

  • Description: Treat rwx as a binary number; if a permission exists, it is represented by 1, and if it does not exist, it is represented by 0. Thus, rwx r-x r– can be represented as 111 101 100, and converting each three bits to an octal number gives 754. The common permission number representation table is shown below:

Learn Linux File System and Common File Operations Easily!

For example: Change the permissions of the files test.txt and test1.txt in the current directory so that the file owner can read and write, the group user can execute, and other users have no access.

Using the first method, the command should be: chmod u=rw,g=x test.txt, the execution result is as follows:

Learn Linux File System and Common File Operations Easily!

Now using the second method to operate on test1.txt, the command should be: chmod 610 test1.txt, the execution result is as follows:

Learn Linux File System and Common File Operations Easily!

(4) Commands for Changing User Group and File Owner

chgrp command:

  • Format: chgrp [options]… group [file or directory]…

  • Description: The chgrp command changes the user group of the specified file; the group can be the group ID or group name. The file is a list of files to change the group, separated by spaces, supporting wildcards. Generally, only superusers can change the group of the file in Linux.

  • Common options:

-R: Recursively changes the group of the specified directory and all its subdirectories and files.
Learn Linux File System and Common File Operations Easily!

chown command:

  • Format: chown [options]… [user][:[group]] file…

  • Description: The chown command changes the owner of the specified file to the specified user or group. The user can be a username or user ID; the group can be a group name or group ID. The file is a list of files to change permissions, separated by spaces, supporting wildcards. Generally, only superusers can use this command in Linux.

  • Common options:

-R: Recursively changes the owner of the specified directory and all its subdirectories and files.
Learn Linux File System and Common File Operations Easily!
  • For example: Change the owner of the directory/home/user/dest and all files and directories within it to root; the command is: chown -R root /home/user/dest

(5) Commands for Linking Files

ln command:

  • Format: ln [options]… source_file or directory target_file or directory

  • Description: The ln command is used to link files or directories.

  • Common options:

  • -s: Creates a soft link (symbolic link) to the source file instead of a hard link.

There are two types of links in the Linux system—hard links and symbolic links, with symbolic links also known as soft links.
Hard links have two restrictions: hard links cannot be created for directories; hard links can only be created between files within the same file system.
Soft links do not have these two restrictions, but they also have drawbacks: the linked file of a soft link contains the path information of the original file, so when the original file is moved to another directory, the link file cannot find the original file; hard links, due to inode, change accordingly with the movement of the file, so this issue does not exist; soft link files require the system to allocate additional space to establish a new inode and save the path of the original file.

Receive Benefits

Learn Linux File System and Common File Operations Easily!

Plant a Tree
The best time was 10 years ago
The second best time is now
Learning is the same
Learn Linux File System and Common File Operations Easily!

Source: Internet; infringement will be deleted.

Leave a Comment