注:机翻,未校。
Common Linux Commands: The Ultimate Guide for Beginners
By Paul Smith September 14, 2024
Linux is known for its powerful command-line interface (CLI), which offers users precise control over their system. While the terminal may seem intimidating at first, mastering common Linux commands will open up a world of possibilities, allowing users to navigate the file system, manage files, and configure their environments with ease.
Linux 以其强大的命令行界面 (CLI) 而闻名,它为用户提供了对其系统的精确控制。虽然终端乍一看可能令人生畏,但掌握常见的 Linux 命令将打开一个充满可能性的世界,使用户能够轻松导航文件系统、管理文件和配置他们的环境。
Whether you’re a seasoned IT professional or just starting with Linux, understanding the most common Linux commands is crucial. In this guide, we will cover a wide range of essential commands that will help you work efficiently and effectively in the Linux environment.
无论您是经验丰富的 IT 专业人员还是刚开始使用 Linux,了解最常见的 Linux 命令都至关重要。在本指南中,我们将介绍广泛的基本命令,这些命令将帮助您在 Linux 环境中高效工作。
Introduction to Common Linux Commands 常用 Linux 命令介绍
Linux commands form the backbone of system administration and user management. These text-based commands are entered into the terminal and provide direct access to a computer’s operating system, making it easier to manipulate files, manage users, and configure software.
Linux 命令构成了系统管理和用户管理的主干。这些基于文本的命令被输入到终端中,并提供对计算机操作系统的直接访问,从而更容易操作文件、管理用户和配置软件。
The Linux command line is both versatile and robust. However, it requires some familiarity to use effectively. For beginners, learning and practicing these basic commands will make your experience smoother and more rewarding. So, let’s dive into the most essential Linux commands and see how they can improve your day-to-day work.
Linux 命令行既通用又健壮。但是,它需要一定的熟悉度才能有效使用。对于初学者来说,学习和练习这些基本命令将使您的体验更顺畅、更有意义。那么,让我们深入研究最基本的 Linux 命令,看看它们如何改善您的日常工作。
What are Linux Commands? 什么是 Linux 命令?
Linux commands are instructions that a user inputs in a terminal to perform specific tasks. Unlike the graphical user interface (GUI) of most operating systems, Linux is primarily controlled through the terminal, which offers far more flexibility and control than a traditional point-and-click interface. These commands allow users to navigate directories, manage files, configure network settings, and perform administrative tasks.
Linux 命令是用户在终端中输入的用于执行特定任务的指令。与大多数操作系统的图形用户界面 (GUI) 不同,Linux 主要通过终端进行控制,与传统的点击式界面相比,它提供了更多的灵活性和控制。这些命令允许用户导航目录、管理文件、配置网络设置和执行管理任务。
Why Learn Common Linux Commands? 为什么要学习常见的 Linux 命令?
Linux powers a large percentage of servers worldwide, and many tech companies and industries rely on Linux for its stability and performance. Whether you’re managing Offshore Dedicated servers, working in cybersecurity, or developing software, understanding Linux commands is crucial for maximizing productivity. Learning these commands allows for automation, scripting, and a more efficient workflow, particularly in environments where a graphical interface isn’t available.
Linux 为全球大部分服务器提供支持,许多科技公司和行业都依赖 Linux 的稳定性和性能。无论您是管理离岸专用服务器、从事网络安全工作还是开发软件,了解 Linux 命令对于最大限度地提高生产力都至关重要。学习这些命令可以实现自动化、脚本编写和更高效的工作流程,尤其是在没有图形界面的环境中。
Understanding the Linux Terminal 了解 Linux 终端
The terminal is a text-based interface used to interact with your Linux system. To open the terminal, press Ctrl + Alt + T
on most Linux distributions. Once the terminal is open, you can type commands and press “Enter” to execute them.
终端是一个基于文本的界面,用于与您的 Linux 系统交互。要打开终端,请按Ctrl + Alt + T
大多数 Linux 发行版。终端打开后,您可以键入命令并按“Enter”来执行它们。
Now, let’s explore the most common Linux commands and how they are used.
现在,让我们探索最常见的 Linux 命令及其使用方法。
Basic Navigation Commands 基本导航命令
One of the first things you’ll need to do in Linux is learn how to navigate the file system. These commands will help you move around directories and locate files efficiently.
在 Linux 中,您需要做的第一件事是学习如何导航文件系统。这些命令将帮助您有效地移动目录和查找文件。
pwd (Print Working Directory) pwd (打印工作目录)
The pwd
command stands for “print working directory,” and as the name suggests, it displays the current directory you’re in. It’s useful for knowing your exact location in the directory structure.
该pwd
命令代表 “print working directory”,顾名思义,它显示您当前所在的目录。它对于了解您在目录结构中的确切位置非常有用。
pwd
Example output: 输出示例:
/home/user/Documents
cd (Change Directory) cd (更改目录)
The cd
command is used to navigate between directories. It’s one of the most frequently used commands, as it allows you to change from one directory to another.
该cd
命令用于在目录之间导航。它是最常用的命令之一,因为它允许您从一个目录更改为另一个目录。
cd /path/to/directory
For example, to move to the /home/user/Downloads
directory, use:
例如,要移动到目录,/home/user/Downloads
请使用:
cd ~/Downloads
To go back to the previous directory, you can use:
要返回上一个目录,您可以使用:
cd ..
ls (List Directory Contents) ls(列出目录内容)
The ls
command lists the contents of a directory. By default, it shows the names of files and subdirectories. It has several useful options for displaying additional information such as file permissions, sizes, and modification times.
该ls
命令列出目录的内容。默认情况下,它显示文件和子目录的名称。它有几个有用的选项来显示其他信息,例如文件权限、大小和修改时间。
ls
To list files with detailed information, use:
要列出包含详细信息的文件,请使用:
ls -l
For a list that includes hidden files (those starting with a dot), use:
对于包含隐藏文件(以点开头的文件)的列表,请使用:
ls -a
File Management Commands 文件管理命令
Being able to manage files effectively is critical in any operating system. These commands will help you create, delete, move, and modify files in Linux.
能够有效地管理文件在任何操作系统中都至关重要。这些命令将帮助您在 Linux 中创建、删除、移动和修改文件。
touch (Create Empty File) touch (创建空文件)
The touch
command is used to create an empty file or update the timestamp of an existing file.
该touch
命令用于创建空文件或更新现有文件的时间戳。
touch filename.txt
This command will create a file name filename.txt
if it doesn’t already exist.
如果文件名不存在,此命令将创建一个filename.txt
文件名。
cp (Copy Files and Directories) cp (复制文件和目录)
The cp
command allows you to copy files and directories. It’s especially helpful for backing up important data.
该cp
命令允许您复制文件和目录。它对于备份重要数据特别有用。
cp source_file destination
For example, to copy file.txt
to the /home/user/backup
directory, you would use:
例如,要复制到file.txt
目录,/home/user/backup
您可以使用:
cp file.txt ~/backup/
To copy an entire directory, use the -r
option (recursive):
要复制整个目录,请使用-r
选项 (recursive):
cp -r /source_directory /destination_directory
mv (Move or Rename Files) mv (移动或重命名文件)
The mv
command is used to move or rename files and directories.
该mv
命令用于移动或重命名文件和目录。
To move a file from one directory to another:
要将文件从一个目录移动到另一个目录:
mv file.txt /new/location/
To rename a file:
要重命名文件:
mv oldname.txt newname.txt
rm (Remove Files and Directories) rm (删除文件和目录)
The rm
command deletes files and directories. Be cautious when using it, as deleted files cannot be recovered unless you have a backup.
该rm
命令将删除文件和目录。使用时要小心,因为除非您有备份,否则无法恢复已删除的文件。
To remove a file:
要删除文件:
rm filename.txt
To delete a directory and its contents, use the -r
option (recursive):
要删除目录及其内容,请使用-r
选项 (recursive):
rm -r directory_name
Viewing and Editing Files 查看和编辑文件
Linux provides several commands for viewing and editing files directly from the terminal. Here are the most common ones.
Linux 提供了几个命令,用于直接从终端查看和编辑文件。以下是最常见的。
cat (Concatenate and Display Files) cat (连接和显示文件)
The cat
command is used to display the contents of a file. It’s perfect for quickly viewing a text file in the terminal.
该cat
命令用于显示文件的内容。它非常适合在终端中快速查看文本文件。
cat filename.txt
To view large files more conveniently, use the less
command, which displays files page by page:
要更方便地查看大型文件,请使用以下less
命令,该命令将逐页显示文件:
less filename.txt
nano (Text Editor) nano (文本编辑器)
Nano is a simple text editor that comes pre-installed on most Linux distributions. It’s easy to use and great for editing configuration files or writing simple scripts.
Nano 是一个简单的文本编辑器,预装在大多数 Linux 发行版上。它易于使用,非常适合编辑配置文件或编写简单的脚本。
nano filename.txt
After making your edits, press Ctrl + O
to save and Ctrl + X
to exit.
进行编辑后,按 Ctrl + O
保存 和 Ctrl + X
退出。
vim (Advanced Text Editor) vim (高级文本编辑器)
Vim is a powerful and highly configurable text editor preferred by many advanced users. It offers extensive customization, but it has a steeper learning curve than Nano.
Vim 是许多高级用户首选的功能强大且高度可配置的文本编辑器。它提供广泛的定制,但它的学习曲线比 Nano 更陡峭。
To edit a file with Vim:要使用 Vim 编辑文件:
vim filename.txt
System Information Commands 系统信息命令
These commands provide valuable information about your Linux system, such as memory usage, disk space, and running processes.
这些命令提供有关 Linux 系统的宝贵信息,例如内存使用情况、磁盘空间和正在运行的进程。
df (Disk Free) df (Disk Free) (可用磁盘)
The df
command displays the amount of disk space available on your file system. It’s useful for checking how much space is free and where your storage is being used.
该df
命令显示文件系统上的可用磁盘空间量。它可用于检查有多少可用空间以及存储空间的使用情况。
df -h
The -h
option displays the output in a human-readable format (MB, GB).
该-h
选项以用户可读格式 (MB, GB) 显示输出。
du (Disk Usage) of (磁盘使用情况)
The du
command shows the disk space used by specific files and directories. To see a summary of a directory’s size:
该du
命令显示特定文件和目录使用的磁盘空间。要查看目录大小的摘要,请执行以下操作:
du -sh /path/to/directory
The -s
flag provides a summary, and the -h
flag gives a human-readable format.
该-s
标志提供摘要,该-h
标志提供人类可读的格式。
top (Task Manager) top (任务管理器)
The top
command displays a real-time list of running processes, along with information about CPU and memory usage. It’s an excellent tool for monitoring system performance.
该top
命令显示正在运行的进程的实时列表,以及有关 CPU 和内存使用情况的信息。它是监控系统性能的绝佳工具。
top
To exit top
, press q
.
要退出 top
,请按 q
。
User Management Commands 用户管理命令
Linux is a multi-user system, and understanding user management commands is essential for administering a system.
Linux 是一个多用户系统,了解用户管理命令对于管理系统至关重要。
adduser (Add a New User) adduser(添加新用户)
The adduser
command creates a new user and sets up a home directory for them.该adduser
命令将创建一个新用户并为其设置主目录。
sudo adduser username
You will be prompted to set a password and other user details.
系统将提示您设置密码和其他用户详细信息。
passwd (Change User Password) passwd (更改用户密码)
The passwd
command is used to change the password for a user. To change your own password, simply type:
该passwd
命令用于更改用户的密码。要更改您自己的密码,只需键入:
passwd
To change another user’s password (as a superuser):
要更改其他用户的密码(作为超级用户):
sudo passwd username
whoami (Display Current User) whoami (显示当前用户)
The whoami
command shows which user account is currently logged in.
该whoami
命令显示当前登录的用户帐户。
whoami
groups (Show User Groups) groups (显示用户组)
The groups
command lists the groups that a specific user belongs to.
该groups
命令列出了特定用户所属的组。
groups username
Network Commands 网络命令
Understanding how to manage network connections is vital for any Linux user, especially those working on servers.
了解如何管理网络连接对于任何 Linux 用户都至关重要,尤其是在服务器上工作的用户。
ifconfig (Interface Configuration)ifconfig(接口配置)
The ifconfig
command displays or configures the network interfaces on your system. It’s commonly used to check the IP address of your machine.
该ifconfig
命令显示或配置系统上的网络接口。它通常用于检查计算机的 IP 地址。
ifconfig
To bring a network interface up or down:
要启动或关闭网络接口:
sudo ifconfig eth0 up
sudo ifconfig eth0 down
ping (Test Network Connectivity) ping (测试网络连接)
The ping
command checks network connectivity by sending packets to a specific IP address or domain and measuring the response time.
该ping
命令通过向特定 IP 地址或域发送数据包并测量响应时间来检查网络连接。
ping google.com
To stop the command, press Ctrl + C
.
要停止该命令,请按 Ctrl + C
。
Permissions and Ownership Commands 权限和所有权命令
In Linux, file permissions and ownership determine who can read, write, or execute files. These commands help you manage access control.
在 Linux 中,文件权限和所有权决定了谁可以读取、写入或执行文件。这些命令可帮助您管理访问控制。
chmod (Change Permissions) chmod (更改权限)
The chmod
command changes the permissions of files and directories该chmod
命令更改文件和目录的权限
. Permissions are represented by three groups: owner, group, and others…
权限由三个组表示:所有者、组和其他组。
To give the owner read, write, and execute permissions:
要授予所有者读取、写入和执行权限,请执行以下操作:
chmod 700 filename
To give all users read and write permissions:要
授予所有用户读取和写入权限,请执行以下操作:
chmod 666 filename
chown (Change Ownership) chown (更改所有权)
The chown
command changes the ownership of a file or directory. For example, to change the owner of a file:
该chown
命令更改文件或目录的所有权。例如,要更改文件的所有者:
sudo chown newowner filename
Process Management Commands 进程管理命令
Linux provides powerful tools for managing system processes. These commands help you control which programs are running and how they interact with the system.
Linux 为管理系统进程提供了强大的工具。这些命令可帮助您控制正在运行的程序以及它们如何与系统交互。
ps (Process Status) ps(处理状态)
The ps
command displays information about currently running processes. It’s helpful for identifying which programs are consuming system resources.该ps
命令显示有关当前正在运行的进程的信息。它有助于识别哪些程序正在消耗系统资源。
ps aux
This will show all running processes along with details such as CPU and memory usage.
这将显示所有正在运行的进程以及 CPU 和内存使用情况等详细信息。
kill (Terminate Process) kill (终止进程)
The kill
command sends a signal to a process, usually to terminate it. First, use ps
or top
to find the process ID (PID), then run:
该kill
命令向进程发送信号,通常是终止它。首先,使用 ps
or top
查找进程 ID (PID),然后运行:
kill PID
To forcefully terminate a process, use:
要强制终止进程,请使用:
kill -9 PID
Conclusion 结论
Linux commands provide the foundation for managing and interacting with a Linux system. By mastering the basic commands covered in this guide, you’ll be well-equipped to navigate directories, manage files, view system information, and control processes. While the terminal might seem daunting at first, practice and familiarity with these common Linux commands will transform your experience, making tasks faster and more efficient.
Linux 命令为管理 Linux 系统并与之交互提供了基础。通过掌握本指南中介绍的基本命令,您将有能力导航目录、管理文件、查看系统信息和控制进程。虽然终端乍一看可能令人生畏,但练习和熟悉这些常见的 Linux 命令将改变您的体验,使任务更快、更高效。
Remember, the more you use these commands, the more intuitive they will become. Don’t be afraid to experiment and explore the flexibility that Linux offers. By taking the time to learn and understand these commands, you can unlock the full potential of the Linux operating system and become more proficient in managing your system.
请记住,使用这些命令的次数越多,它们就会变得越直观。不要害怕尝试和探索 Linux 提供的灵活性。通过花时间学习和理解这些命令,您可以释放 Linux 操作系统的全部潜力,并更加熟练地管理系统。
FAQs 常见问题
How do I open the Linux terminal? 如何打开 Linux 终端?
To open the terminal in most Linux distributions, press Ctrl + Alt + T
.
要在大多数 Linux 发行版中打开终端,请按 Ctrl + Alt + T
。
What does sudo
mean in Linux? Linux 中sudo
是什么意思?
The sudo
command allows a permitted user to execute a command as the superuser or another user, granting elevated permissions for specific tasks.
该sudo
命令允许允许的用户以超级用户或其他用户的身份执行命令,从而为特定任务授予提升的权限。
How can I list all files, including hidden files? 如何列出所有文件,包括隐藏文件?
Use the ls -a
command to list all files, including hidden ones.
使用该ls -a
命令列出所有文件,包括隐藏的文件。
Can I recover files deleted with the rm
command? 我可以恢复使用命令rm
删除的文件吗?
Once a file is deleted with the rm
command, it cannot be easily recovered. Always use caution with this command.
使用rm
命令删除文件后,将无法轻松恢复。请始终谨慎使用此命令。
What is the difference between cp
and mv
? cp
和 mv
有什么区别?
The cp
command copies files or directories, while mv
moves or renames them.
该命令会cp
复制文件或目录,同时mv
移动或重命名它们。
How do I check my IP address in Linux? 如何在 Linux 中检查我的 IP 地址?
You can use the ifconfig
command to check your network configuration, including your IP address.
您可以使用该ifconfig
命令检查您的网络配置,包括您的 IP 地址。
via:
-
Common Linux Commands: The Ultimate Guide for Beginners - BlueAngelHost - Official Blog
Linux Shell Comparison
~