如果您是Linux新手,使用终端可能会有点不知所措。像Linux Mint这样的(Linux Mint)新 Linux(New Linux)发行版具有出色的图形界面,但Linux的核心是内核,这意味着使用命令行。
即使您是Windows用户,您也可能不得不在生活中的某个时刻打开命令提示符窗口来执行任务。使用最新版本的Windows,Windows 10,您甚至可以在 Windows 中安装Ubuntu (Windows)Bash(Ubuntu Bash) shell并直接从Windows运行Linux命令!
在本文中,我将介绍一些非常基本的Linux命令,这些命令在几乎所有Linux发行版中都很常见。由于 bash shell 是最流行的 shell,也是我使用的一种,所以我将在所有命令中使用该语法。此外,我将提到每个命令的一些最有用的参数,但在手册页中可以找到更多参数。
1. ls(列出内容)
在我看来,您应该知道的第一个命令是ls命令。此命令列出当前工作目录的内容。如果您只键入 ls 并按Enter,您将获得当前目录中文件和文件夹的一个非常基本的列表。
在大多数Linux发行版上,目录将以不同的颜色突出显示,例如绿色。文件通常是 shell 提示符的标准颜色,在我的例子中是灰色的。没有任何参数, ls 有点无聊。如果你使用-a和 ls,你将能够看到所有隐藏的文件。
任何以点开头的都是隐藏文件或目录。隐藏的目录都是深蓝色的,有点难看。另一个有用的参数是-l选项,如下所示。
这为您提供了一长串文件和文件夹以及更多信息,例如权限、链接、用户、组、大小和上次修改日期。如果您不确定如何解释权限,请务必阅读我关于了解 Linux 权限(understanding Linux permissions)的帖子。
2. cd(更改目录)
一旦您可以列出目录的内容,了解如何切换到不同的目录就很有用了。默认情况下,当你打开一个 bash shell 时,你总是从你的主目录开始。这由shell 提示符中的波浪号 ( ~
cd命令是您在Linux中更改目录的方式。使用 cd 确实没有太多要学习的东西,但是有一些捷径。一个好处是简单地输入 cd 并按 enter。无论您身在何处,这都会让您回到主目录。
此外,如果您想进入一个无法通过相对路径访问的目录,您可以使用绝对路径。在下面的示例中,我必须使用从root (/)开始的绝对路径来访问 etc/ssh。
3. 人(帮助页面)
man 命令可能是Linux(Linux)中最有用的命令之一。即使是高级Linux用户也无法记住Linux命令的每个参数。手册页将为您提供有关命令的所有不同参数的详细信息。
语法也很简单。它只是 man 后面跟着你想了解的命令。在上面的截图中,我做了一个man ls来了解更多关于 ls 命令的信息。man 的一个有用参数是-k,它允许您使用关键字搜索所有命令。
上面,我搜索了关键字zip并返回了所有在命令名称或描述中包含单词zip的命令。(zip)这是查找您可能不知道的命令的便捷方式。
除了 man 之外,您还可以使用另一个名为info的命令来获取有关如何使用命令的更多示例。只需(Just)键入info 命令( info command)即可调出该命令的信息页面。
4. 点击 (创建文件)
如果要快速创建新文件,最简单的方法是使用touch命令。实际上,touch 命令用于更改文件上的时间戳,但另一个用途是创建新文件。
在Linux(Linux)中创建文件的方法有很多种,以后您可能永远不会使用触摸来创建文件,但在开始时,它非常方便。
如果在使用 touch 命令时文件已经存在,它会简单地更新文件的上次访问和上次修改时间戳,如上所示。
5. cat(连接文件和打印)
另一个有用的命令是cat命令。cat 的主要功能是连接多个文件,但它也可以用于将文件的内容打印到标准输出(即屏幕)。
您可以使用-n参数将行号添加到输出中。如果使用-b选项,它只会将行号添加到非空白行。如果您在比终端窗口高度长的文件上使用 cat,则只会显示文件的底部。您可以通过管道将 cat 的输出传递给less或more命令,以逐页查看文件的内容。
6. mkdir(制作目录)
在某些时候,您会想要创建目录以更好地组织数据,这就是mkdir 命令的用武之地。您可以使用相对或绝对路径来使用此命令创建目录。
在上面的示例中,我使用相对路径和绝对路径在主目录中创建了两个目录。如果需要一次创建多个分层目录,则需要使用-p参数。
在上面的示例中,我使用 -p 参数一次创建了Aseem、Data和Pictures目录,即使它们都不存在。
7. rm(删除)
rm命令是一个强大的命令,可用于删除文件和目录。rm 命令可以删除其中包含文件和目录的目录。
要删除文件,只需输入文件名。如果需要删除非空目录,则需要使用-r参数。在使用 rm 时使用-i( -i)和-v参数也是一个好主意,因为它会在删除任何内容之前询问您。
所以这是七个非常简单但常见的命令,您需要在Linux中了解这些命令才能开始使用。还有更多,我将很快发布更多关于更多命令以及如何使用它们的初学者文章。如果您有任何问题,请发表评论。享受!
7 Linux Commands Every Beginner Should Know
If you’re new to Linux, using thе terminаl can be a bit overwhelming. New Linux distributions like Linux Mіnt haνe great graphical interfaces, but the heart of Linux is the kernel and that meаns uѕing the command line.
Even if you are a Windows user, you’ve probably had to open a command prompt window at some point in your life to perform a task. With the latest version of Windows, Windows 10, you can even install the Ubuntu Bash shell in Windows and run Linux commands directly from Windows!
In this article, I’m going to go over some really basic Linux commands that are common across pretty much all distributions of Linux. Since the bash shell is the most popular shell and the one I use also, I’ll be using that syntax for all the commands. Also, I’ll be mentioning some of the most useful arguments for each command, but there are many more which can be found in the man pages.
1. ls (List Contents)
In my opinion, the first command you should know is the ls command. This command lists the contents of the current working directory. If you just type ls and press Enter, you’ll get a very basic listing of files and folders in the current directory.
On most Linux distros, directories will be highlighted in a different color like green. Files will usually be the standard color of the shell prompt, which is grey in my case. Without any arguments, ls is kind of boring. If you use -a with ls, you’ll be able to see all hidden files.
Anything that starts with a dot is a hidden file or directory. The hidden directories all have a dark blue color, which is kind of hard to see. Another useful argument is the -l option as shown below.
This gives you a long listing of files and folders with a lot more information such as permissions, links, user, group, size and last modification date. If you’re not sure how to interpret the permissions, make sure to read my post on understanding Linux permissions.
2. cd (Change Directory)
Once you can list the contents of a directory, it’s useful to know how to switch to a different directory. By default, you’ll always start in your home directory when you open a bash shell. This is indicated by the tilde symbol (~) in the shell prompt.
The cd command is how you change directories in Linux. There really isn’t a whole lot to learn with cd, but there are a couple of shortcuts. One good is simply typing cd and pressing enter. This will always get you back to the home directory no matter where you are.
Also, you can use an absolute path if you want to get into a directory that is not accessible via a relative path. In the example below, I have to use an absolute path starting at the root (/) to get to etc/ssh.
3. man (Help Pages)
The man command is probably one of the most useful commands in Linux. Even advanced Linux users can’t remember every argument to a Linux command. The man pages will give you detailed info on all of the different arguments for a command.
The syntax is really simple also. It’s just man followed by the command you want to learn about. In the screenshot above, I did a man ls to learn more about the ls command. One useful argument to man is -k, which will allow you to search all commands using a keyword.
Above, I searched for the keyword zip and got back all commands that have the word zip in the command name or in the description. It’s a handy way to find commands you may not have otherwise known about.
Along with man, you can use another command called info to get more examples of how to use a command. Just type info command to bring up the info page for that command.
4. touch (Create File)
If you want to quickly create a new file, the easiest way is to use the touch command. In reality, the touch command is used to change the time stamp on a file, but another use is to create a new file.
There are many ways to create files in Linux and later on you’ll probably never use touch to create a file, but in the beginning, it comes in very handy.
If a file already exists when using the touch command, it simply updates the last access and last modified timestamps for the file as shown above.
5. cat (Concatenate Files & Print)
Another useful command is the cat command. The main function of cat is to concatenate multiple files, but it can also be used to print the contents of a file to standard output (which is the screen).
You can use the -n argument to add line numbers to the output. If you use the -b option, it will only add line numbers to lines that are not blank. If you use cat on a file that is longer than the height of your terminal window, only the bottom of the file will be shown. You can pipe the output of cat to the less or the more command to view the contents of a file page by page.
6. mkdir (Make Directory)
At some point, you’ll want to create directories to organize your data better and that’s where the mkdir command comes in. You can use relative or absolute paths for creating directories using this command.
In the example above, I’ve created two directories in my home directory using a relative path and an absolute path. If you need to create multiple hierarchical directories at once, you need to use the -p argument.
In the above example, I used the -p argument to create the Aseem, Data and Pictures directories all at once even though none of them existed.
7. rm (Remove)
The rm command is a powerful command that can be used to remove files and directories. The rm command can remove directories that have files and directories inside of them.
To remove a file, you just type in the file name. If you need to remove a directory that is not empty, you need to use the -r argument. It’s also a good idea to use the -i and -v arguments when using rm as it will ask you before deleting anything.
So those are seven really simple, yet common commands that you’ll need to know in Linux to get started. There are many more and I’ll be posting more beginner articles soon on more commands and how to use them. If you have any questions, post a comment. Enjoy!