Linux 初学者必读:10 条终端入门命令
所以你想学习 Linux?我不会在接下来的两段中喋喋不休地谈论以下命令将如何为你其余的技术经验奠定形而上学的基础,而是直接进入正题。没有废话,没有暴露——只有命令及其工作原理。让我们开始吧。
注意:我建议在对文件和目录进行更改时将 GUI 和 CLI 并排放置,以巩固您在终端中所做的事情实际上在系统上正在发生的事情。
环顾四周
如果您想查看文件系统,您将需要知道如何列出文件和目录、在目录之间移动以及查看您当前所在的位置。
当你打开终端时,你会看到类似这样的提示:
[tcarrigan@server ~]$
提供的唯一信息是您登录的用户 ( tcarrigan )、您登录的计算机的主机名 ( server )、您当前所在的目录 (用 ~表示) 以及访问级别 ( $ 表示用户, # 表示 root)。如果您要四处查看,了解从哪里开始会有所帮助。另外,如果这是您的第一次, ~ 可能对您来说意义不大。
1. pwd——打印工作目录
pwd命令 会告诉您当前正在工作的目录:
[tcarrigan@server ~]$ pwd
/home/tcarrigan
/home/tcarrigan
如上所述, 我目前正在目录中工作 。现在,如果您想查看其中有哪些文件和目录,该怎么办/home/tcarrigan
?
2. ls -l
ls命令 将列出给定目录中包含的所有非隐藏文件。我建议将它与 -l 选项结合使用 ,以使输出更清晰一些(并获得有关列出的文件和目录的更多信息)。
[tcarrigan@server ~]$ ls -l
total 0
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb 6 2020 article_submissions
drwxrwxr-x. 2 tcarrigan tcarrigan 45 Aug 30 11:59 demo
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Pictures
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Videos
现在,正如我上面所说,ls通常只显示非隐藏文件和目录。那么隐藏的内容怎么办?
[ 你可能还喜欢: 书评:Linux 命令行]
3. ls -al
ls -a命令 就是您要找的答案。与 -l 选项结合使用,可获得与之前看到的相同的“漂亮”输出,其中包括隐藏文件。
[tcarrigan@server ~]$ ls -al
total 108
drwx------. 19 tcarrigan tcarrigan 4096 Oct 20 16:34 .
drwxr-xr-x. 9 root root 106 Sep 15 22:56 ..
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb 6 2020 article_submissions
-rw-------. 1 tcarrigan tcarrigan 2959 Oct 19 14:58 .bash_history
-rw-r--r--. 1 tcarrigan tcarrigan 18 Aug 30 2019 .bash_logout
-rw-r--r--. 1 tcarrigan tcarrigan 179 Feb 13 2020 .bash_profile
-rw-r--r--. 1 tcarrigan tcarrigan 312 Aug 30 2019 .bashrc
-rw-r--r--. 1 tcarrigan tcarrigan 12288 Feb 25 2020 .bashrc.swp
drwx------. 13 tcarrigan tcarrigan 4096 Apr 28 12:08 .cache
drwxrwxr-x. 3 tcarrigan tcarrigan 28 Feb 13 2020 .cargo
drwx------. 13 tcarrigan tcarrigan 4096 Feb 3 2020 .config
drwxrwxr-x. 2 tcarrigan tcarrigan 45 Aug 30 11:59 demo
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Desktop
*Output Omitted*
您会注意到,此输出中的条目比以前多得多。 .
现在显示以 字符开头的任何文件或目录。
Now, let’s navigate around the file system a bit. Let’s say you want to see a file inside /home/tcarrigan/article_submissions
.
4. cd (dir) - change to (directory)
The change directory command is self-explanatory. It allows you to change your working directory.
[tcarrigan@server ~]$ cd article_submissions/
[tcarrigan@server article_submissions]$ pwd
/home/tcarrigan/article_submissions
You can see that we changed our working directory to ~/article_submissions
. What if I wanted to get back to where I started?
5. cd - with no options
The cd command, when used with no additional options, will return you to the home directory of the user you are logged in as.
[tcarrigan@server article_submissions]$ cd
[tcarrigan@server ~]$ pwd
/home/tcarrigan
6. Going backward
If you need to go back to a previous directory, use the following:
[tcarrigan@server ~]$ pwd
/home/tcarrigan
[tcarrigan@server ~]$ cd ..
[tcarrigan@server home]$ pwd
/home
Making and removing
Now that you know how to move around, let’s look at creating and removing directories and files.
7. mkdir (X) - make (X) directory
To create a new directory, we use the mkdir command.
[tcarrigan@server ~]$ mkdir Test
[tcarrigan@server ~]$ ls -l
total 0
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb 6 2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Pictures
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Templates
drwxrwxr-x. 2 tcarrigan tcarrigan 6 Oct 20 17:05 Test
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Videos
What if we want to create a new file inside the newly created Test directory?
8. touch (file) - creates a new file (filename)
The touch command creates and updates individual files. To create a file called touch_test inside the Test directory:
[tcarrigan@server Test]$ touch touch_test
[tcarrigan@server Test]$ ls -l
total 0
-rw-rw-r--. 1 tcarrigan tcarrigan 0 Oct 20 17:12 touch_test
Ok, so we created a new directory, as well as a file within that directory. How do we get rid of them?
9. rm (file) - remove (filename)
First, the file:
[tcarrigan@server Test]$ rm touch_test
[tcarrigan@server Test]$ ls -l
total 0
10. Now the directory: rm -r (directory)
[tcarrigan@server ~]$ rm -r Test/
[tcarrigan@server ~]$ ls -l
total 0
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb 6 2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Pictures
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan 6 Jan 27 2020 Videos
[ Download now: A sysadmin's guide to Bash scripting. ]
What's next?
If today was your first time using Linux, congrats on making the leap. We looked at basic navigation, creation, and removal of files and directories. Keep an eye out for the next 10 commands, coming soon. We will look at moving and copying files, creating links, and the various ways to read files. In the meantime, keep practicing what we did today on your favorite virtual machine.