Comprehensive Collection of Common Linux Commands

Comprehensive Collection of Common Linux Commands

1. Linux Directory Structure

The directory structure of Linux is tree-like, with the top-level directory being the root directory /.

Other directories can be added to the tree through mounting, and removed by unmounting them.

Absolute Path and Relative Path:

Absolute Path: Starts from the root directory /, for example: /usr/share/doc.

Relative Path: Does not start with /, for example, to go from /usr/share/doc to /usr/share/man, you can write: cd ../man, which is a relative path.

1.1. View Directory

In Linux systems, the ls command is probably the most frequently run.

Syntax:

ls: View the file names in the current path

ls -a: List all files, including hidden files (files that start with .) (commonly used)

ls -d: Only list the directory itself, without listing the files within the directory (commonly used)

ls -l: List in long format, including file attributes and permissions, etc. (commonly used)

ll: Equivalent to ls -l

ls -al: List all files in the directory (including attributes and hidden files)

1.2. Change Directory

cd is short for Change Directory, which is used to change the working directory.

Syntax:

cd [relative path or absolute path]

# Use absolute path to switch to local directory

cd /usr/local/

# Go back to your home directory, which is /root

cd ~

# Go to the current upper-level directory, which means the upper-level of /root; cd ..

1.3. Show Current Directory

pwd is short for Print Working Directory, which is the command to display the current directory.

Syntax:

pwd

1.4. Create Directory

mkdir (make directory) is used to create a new directory.

Syntax: mkdir [-mp] directory name

-m: Set file permissions directly, without considering default permissions

-p: Recursively create the required directory (including the upper-level directory)!

View help: mkdir –help

1.5. Delete Directory

rmdir command is used to delete empty directories.

Syntax: rmdir [-p] directory name

-p: Also delete the upper-level [empty] directory

2. File Operations

2.1. View File Content

cat displays all content

Syntax: cat starts displaying file content from the first line

more displays file content page by page

Syntax: more file name

During the operation of the more program, the following key functions are available:

Space: Move down one page;

Enter: Move down one line;

f: Immediately display the file name and the currently displayed line number;

q: Immediately exit more, no longer display the file content.

less scrolls page by page

Syntax: less file name

Commands that can be entered during less operation include:

Space: Move down one page;

[pagedown]: Move down one page;

[pageup]: Move up one page;

q: Exit the less program;

2.2. Create File

Syntax: touch creates an empty ordinary file

2.3. Write Content

echo redirects content to the specified file, opens if it exists, creates if it does not

Syntax:

echo ‘content’ > file name (overwrite mode)

echo ‘content’ >> file name (append mode)

2.4. Copy/Move Files

Syntax: cp [-adfilprsu] source file (source) destination file (destination)

-a: Equivalent to -pdr

-d: If the source file is a link file, copy the link file attributes instead of the file itself;

-f: Force, if the destination file already exists and cannot be opened, remove it and try again;

-i: If clear action is performed

-l: Create a hard link instead of copying the file itself;

-p: Copy the file attributes along with it, instead of using default attributes (commonly used for backup)

-r: Recursively copy, used for directory copying (commonly used);

-s: Copy as a symbolic link, which is a shortcut file;

-u: Upgrade destination only if it is older than source

2.5. Move Files

mv can move files and directories, or rename them.

Syntax: mv [-fiu] source destination

-f: Force, if the destination file already exists, will not ask and directly overwrite;

-i: If the destination file already exists, will ask whether to overwrite;

-u: Only upgrade if the destination file already exists and source is newer;

2.6. Delete Files

rm can remove files or directories

Syntax: rm [-fir] file or directory

-f: Ignore nonexistent files, no warning message will appear.

-i: Interactive mode, will ask the user whether to proceed before deleting

-r: Recursive delete, most commonly used for directory deletion, this operation is dangerous

3. Packaging and Unpacking

The commonly used compression method in Linux is to use tar to package many files into one file, and then compress it with gzip to create a xxx.tar.gz (or xxx.tgz) file.

Common parameters:

-c: Create a new tar file

-v: Show information about the running process

-f: Specify the file name

-z: Call gzip compression command for compression

-t: View the contents of the compressed file

-x: Unpack the tar file

Packaging: tar -cvf xxx.tar List of files or directories to be packaged, separated by spaces

Package and compress: tar -zcvf xxx.tar.gz List of files or directories to be packaged, separated by spaces

Unpack: tar -xvf xx.tar

tar -zxvf xx.tar.gz -C /usr/my Note -C is uppercase to prevent the unpacking path from being unavailable

4. View Process Snapshot

ps -aux shows the current process snapshot

View Java process: ps -aux | grep java

View MySQL process: ps -aux | grep mysql

5. Pipe |

Pipes are a very important and commonly used feature in Linux, which allows the output of one command to be used as input for another command, combining them.

ls –help | more # View help information page by page, press q to exit

6. VIM Editor

vim is divided into three modes: Command Mode, Insert Mode, and Last Line Mode

Command Mode:

When the user just starts vi/vim, they enter Command Mode.

In this state, keyboard actions are recognized by vim as commands, not character inputs. For example, if we press i now, a character will not be entered; i is treated as a command.

Here are some commonly used commands:

i: Switch to Insert Mode to input characters.

x: Delete the character at the current cursor position.

: Switch to Last Line Mode to input commands in the bottom line.

If you want to edit text: Start vim, enter Command Mode, press i to switch to Insert Mode.

Command Mode has only some basic commands, so you still need to rely on Last Line Mode to input more commands.

Insert Mode:

Pressing i in Command Mode enters Insert Mode.

In Insert Mode, you can use the following keys:

Character keys and Shift combinations to input characters

ENTER: Enter key, line break

BACKSPACE: Delete the character before the cursor

DEL: Delete the character after the cursor

Arrow keys: Move the cursor in the text

HOME/END: Move the cursor to the beginning/end of the line

PageUp/PageDown: Scroll up/down

Insert: Switch the cursor to input/replace mode, the cursor will change to a line/underscore

ESC: Exit Insert Mode, switch to Command Mode

Last Line Mode:

Pressing : (colon) in Command Mode enters Last Line Mode.

In Last Line Mode, you can input single or multiple character commands, and there are many available commands.

In Last Line Mode, the basic commands are (the colon is omitted):

q: Exit the program

w: Save the file

Press ESC to exit Last Line Mode at any time.

Comprehensive Collection of Common Linux Commands

8. Linux File Permissions

Permission management in the Linux operating system is very strict. In Linux systems, not only are users and groups managed according to UID and GID, but files in the Linux system are also classified according to users and groups, and permissions are managed for different groups to determine who can access and operate through what means and directories.

Comprehensive Collection of Common Linux Commands

There are a total of 10 characters for permissions, which we will divide into 4 main parts for understanding:

Part 1: Indicates the type of file

– indicates a file

d indicates a directory

l indicates a link (understood as a shortcut)

Part 2: The permissions that the current user has for that file (owner, abbreviated as u)

Part 3: The permissions of other users in the current group for that file (group, abbreviated as g)

Part 4: The permissions of users from other groups for that file (other, abbreviated as o)

r: Read

w: Write

x: Execute

Execute permissions are added to directories, but not to files (as having execute permissions on files poses security risks)

For files and directories, r, w, and x have different functions and meanings

For files: r: Read file content

w: Modify file content

x: Execute permission is meaningless for files other than binary programs

For directories: Directories can essentially be seen as files that store file lists, node numbers, etc.

r: View the list of files in the directory

w: Delete and create files in the directory

x: Can cd into the directory, view the detailed attributes of files in the directory, and access the contents of files in the directory (basic permissions)

PS: The root account is not subject to read/write restrictions on file permissions; execution permissions are restricted

Comprehensive Collection of Common Linux Commands

The order of obtaining file permissions for users is: first check if they are the owner; if so, the subsequent permissions are ignored; then check if they are in the group; if so, the subsequent permissions are ignored.

8.2. Modify File Permissions

chown means change owner, and its main function is to change the owner of a file or directory.

chmod modifies the read/write/execute attributes of files and folders. Usage permissions: all users

chown modifies the user and group attributes of files and folders. Usage permissions: root

8.2.1. Mode Method

Syntax: chmod who opt per file

who: ugoa (all) (u: user, g: group, o: other users, a: all users by default)

opt: + add a permission – remove a permission = assign permission

per: rwxX

Example:

chmod u=rwx,g=r a.txt

chmod u+x,g+w,o+w test.log // r: read, w: write, x: execute

8.2.2. Numeric Method

Syntax:

chmod xxx file

rwx rw- r–

421 420 400

7 6 4

0 indicates no permission, 1 indicates executable = x, 2 indicates writable = w, 4 indicates readable = r

For example:

-rwxr–r– 1 root root 10 oct 16 02:55 yhp.log

User permissions = rwx = 4+2+1 = 7

Group permissions = r– = 4+0+0 = 4

Other user permissions = r– = 4+0+0 = 4

Combination: 744

Modify permissions:

All add write permission +2

Add write permission for group: +2

Add execute permission for other users: +1

chmod 765 a.txt

9. Common Network Operations in Linux

9.1 Hostname Operations

hostname: Display hostname

hostname XXX: Modify hostname, not recommended, temporary effect

To permanently modify the hostname, you need to edit the /etc/sysconfig/network file

9.2 Query Complete System Information

uname -a: Display complete system information

9.3 IP Address Operations

View IP address: ip addr

Modify IP address: Edit /etc/sysconfig/network-scripts/

Restart network service: service network restart

9.4 Domain Name Mapping

Edit /etc/hosts file

9.5 Network Service Management

View network service status: systemctl status network

Start network service: systemctl start network

Stop network service: systemctl stop network

Restart network service: systemctl restart network

Set to start on boot: systemctl enable network

9.6 Firewall Settings

View firewall status: systemctl status firewalld

Start firewall: systemctl start firewalld

Stop firewall: systemctl stop firewalld

Check if firewall service is enabled at boot: systemctl is-enabled firewalld

Enable firewall service at boot: systemctl enable firewalld

Disable firewall service at boot: systemctl disable firewalld

Query the list of services that have started: systemctl list-unit-files | grep enabled

Query the list of services that failed to start: systemctl –failed

Comprehensive Collection of Common Linux Commands

Leave a Comment