一、ls指令
上篇文章已经说了一点点的ls指令,不过那还是不够的,这篇文章会介绍更多的指令,最起码能使用命令行进行一些简单的操作,下面开始介绍了
ls常用选项
接着就是实测,这里主要说的ls -l和-a,如上面所说,第一个就是列出隐藏文件,测试如下方代码,可以看出这里是我这里是没有文件所以是0,但是有两个隐藏文件,是.和..也就是一个点的就是当前目录,两个点就是上一级目录,在每一行前面都有很多信息,这里就是详细信息,用法也没啥难的,但是要注意的是空格,比如ls -l中间是有空格的,而且这些指令是可以组合的,如下方代码二,这个就是ls的一点用法。
[root@VM-24-9-centos ~]# ls
[root@VM-24-9-centos ~]# ls -l
total 0
[root@VM-24-9-centos ~]# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc .pip .pydistutils.cfg .ssh .tcshrc
[root@VM-24-9-centos ~]# ls -l -a
total 52
dr-xr-x---. 6 root root 4096 Mar 23 13:10 .
dr-xr-xr-x. 19 root root 4096 Mar 23 20:50 ..
-rw------- 1 root root 629 Mar 23 20:50 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwxr-xr-x 3 root root 4096 Mar 7 2019 .cache
drwxr-xr-x 3 root root 4096 Mar 7 2019 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
drwxr-xr-x 2 root root 4096 Mar 23 13:10 .pip
-rw-r--r-- 1 root root 73 Mar 23 13:10 .pydistutils.cfg
drwx------ 2 root root 4096 Nov 5 2019 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[root@VM-24-9-centos ~]# ^C
[root@VM-24-9-centos ~]#
[root@VM-24-9-centos ~]# ls -la
total 52
dr-xr-x---. 6 root root 4096 Mar 23 13:10 .
dr-xr-xr-x. 19 root root 4096 Mar 23 20:57 ..
-rw------- 1 root root 650 Mar 23 20:50 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwxr-xr-x 3 root root 4096 Mar 7 2019 .cache
drwxr-xr-x 3 root root 4096 Mar 7 2019 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
drwxr-xr-x 2 root root 4096 Mar 23 13:10 .pip
-rw-r--r-- 1 root root 73 Mar 23 13:10 .pydistutils.cfg
drwx------ 2 root root 4096 Nov 5 2019 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[root@VM-24-9-centos ~]# ls -al
total 52
dr-xr-x---. 6 root root 4096 Mar 23 13:10 .
dr-xr-xr-x. 19 root root 4096 Mar 23 20:57 ..
-rw------- 1 root root 669 Mar 23 20:58 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwxr-xr-x 3 root root 4096 Mar 7 2019 .cache
drwxr-xr-x 3 root root 4096 Mar 7 2019 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
drwxr-xr-x 2 root root 4096 Mar 23 13:10 .pip
-rw-r--r-- 1 root root 73 Mar 23 13:10 .pydistutils.cfg
drwx------ 2 root root 4096 Nov 5 2019 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[root@VM-24-9-centos ~]# ll
total 0
[root@VM-24-9-centos ~]#
二、pwd指令
pwd指令就是用来查看当前所属目录,也就是所在目录,用法如下代码,就是可以看出我用的是root就是在root目录,因为我也没有创建其他的文件夹,一般这个指令都是配合其他指令使用的。
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]#
三、cd指令
cd指令就是改变工作目录,在Linux环境下可以看成是一颗树,在这颗树下有许许多多的文件夹,就相当于树的子节点,形状大概就是下图这样。
/就是根目录,下面有很多文件夹,我着就随便画了几个,那么接下来用代码演示如何使用这个指令,并且配合pwd指令展示效果,这个就是如何用cd去移动位置,可以看到在根目录时在进去cd 。。时会进不去,因为这是到了根目录,所以无法进入了。
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# cd ..
[root@VM-24-9-centos /]# pwd
/
[root@VM-24-9-centos /]# ll
total 72
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Feb 19 15:49 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3040 Mar 23 13:10 dev
drwxr-xr-x. 95 root root 12288 Mar 23 13:10 etc
drwxr-xr-x. 3 root root 4096 Mar 23 13:10 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Mar 23 13:10 opt
dr-xr-xr-x 107 root root 0 Mar 23 13:09 proc
dr-xr-x---. 6 root root 4096 Mar 23 13:10 root
drwxr-xr-x 26 root root 940 Mar 23 13:10 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Mar 23 13:09 sys
drwxrwxrwt. 9 root root 4096 Mar 23 15:20 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
[root@VM-24-9-centos /]# cd ..
[root@VM-24-9-centos /]# pwd
/
[root@VM-24-9-centos /]# ll
total 72
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Feb 19 15:49 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3040 Mar 23 13:10 dev
drwxr-xr-x. 95 root root 12288 Mar 23 13:10 etc
drwxr-xr-x. 3 root root 4096 Mar 23 13:10 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Mar 23 13:10 opt
dr-xr-xr-x 107 root root 0 Mar 23 13:09 proc
dr-xr-x---. 6 root root 4096 Mar 23 13:10 root
drwxr-xr-x 26 root root 940 Mar 23 13:10 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Mar 23 21:12 sys
drwxrwxrwt. 9 root root 4096 Mar 23 15:20 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
[root@VM-24-9-centos /]# cd /root
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]#
下面这个代码就是演示一下cd的别的用法:
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# cd ..
[root@VM-24-9-centos /]# pwd
/
[root@VM-24-9-centos /]# cd /root
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# cd ../home
[root@VM-24-9-centos home]# pwd
/home
[root@VM-24-9-centos home]# cd -
/root
[root@VM-24-9-centos ~]# cd ..
[root@VM-24-9-centos /]# pwd
/
[root@VM-24-9-centos /]# cd ~
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]#
四、touch指令
这个指令就是创建一个文件,对就是文件不是文件夹,他的常用选项如下
我这里就创建了一个文本的示例代码。
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# touch test.txt
[root@VM-24-9-centos ~]# ls
test.txt
[root@VM-24-9-centos ~]# ^C
[root@VM-24-9-centos ~]#
五、mkdir指令
这个就是创建 一个文件夹,如下就是常用选项,然后下方代码就时利用这个指令创建一个文件夹,和递归创建多个文件夹的示例。
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# ls
test.txt
[root@VM-24-9-centos ~]# mkdir test
[root@VM-24-9-centos ~]# ls
test test.txt
[root@VM-24-9-centos ~]# mkdir -p test1/test2
[root@VM-24-9-centos ~]# ls
test test1 test.txt
[root@VM-24-9-centos ~]# cd test1
[root@VM-24-9-centos test1]# ls
test2
[root@VM-24-9-centos test1]#
六、rmdir指令和rm指令
rmdir指令就是和mkdir相对应的指令这个指令就是删除一个文件夹,但是要求是空文件
rm指令就是可以同时删除文件和目录,常用选项如下
这个代码就是这几个指令的是用方式,但是要注意的是使用rm时会问你确定要删除吗,这是可以打yes和y都是同意,但是no和n就是拒绝,如果时-f就不会提示,如下代码,我有创建了文件夹,然后使用rm -rf d1就没问我直接删除了。
[root@VM-24-9-centos test1]# cd ..
[root@VM-24-9-centos ~]# ls
test test1 test.txt
[root@VM-24-9-centos ~]# rmdir test
[root@VM-24-9-centos ~]# ls
test1 test.txt
[root@VM-24-9-centos ~]# rm test.txt
rm: remove regular empty file ‘test.txt’? y
[root@VM-24-9-centos ~]# ls
test1
[root@VM-24-9-centos ~]# rm -r test1
rm: descend into directory ‘test1’? y
rm: remove directory ‘test1/test2’? y
rm: remove directory ‘test1’? y
[root@VM-24-9-centos ~]# ls
[root@VM-24-9-centos ~]# ll
total 0
[root@VM-24-9-centos ~]#
[root@VM-24-9-centos ~]# mkdir -p d1/d2
[root@VM-24-9-centos ~]# ls
d1
[root@VM-24-9-centos ~]# rm -p d1
rm: invalid option -- 'p'
Try 'rm --help' for more information.
[root@VM-24-9-centos ~]# rm -rf d1
[root@VM-24-9-centos ~]# ls
[root@VM-24-9-centos ~]#
七、tree指令
这个指令就是把文件夹用树状的形式展示,如下方代码所示,但是我在第一次使用时并没有这个指令,显示没有,然后我去网上插资料,发现是我这个里面没有装,只需要敲下下方块中的代码就可以了,但是要在root权限下,然后就会出现下方代码二的情况,等待完成就好了。
[root@VM-24-9-centos ~]# tree
.
`-- d1
|-- d2
| |-- t1
| |-- t2
| |-- t3
| |-- t4
| |-- t5
| `-- t6
|-- d3
|-- d4
`-- d5
11 directories, 0 files
[root@VM-24-9-centos ~]#
[root@VM-24-9-centos ~]# yum install -y tree
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
tree x86_64 1.6.0-10.el7 os 46 k
Transaction Summary
==================================================================================================================================================================================================================
Install 1 Package
Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1
Installed:
tree.x86_64 0:1.6.0-10.el7
Complete!
八、man指令
man指令就是用来查找指令用法的,因为Linux的指令有很多,不可能每个都去查,所以就有这个指令的出现,下方就是常用选项
这个指令贼牛逼甚至可以查看自己的用法,测试如下截图,按上下键移动,按q退出,接着演示一下其他的用法,查一下指令的用法,这里测试的是printf,但是我发现我这个手册不够全,然后上网搜了一下发现是缺少了需要敲下下方代码就可以了,测试代码如下。
[root@VM-24-9-centos ~]# clear
[root@VM-24-9-centos ~]# man man
[root@VM-24-9-centos ~]# man man
[root@VM-24-9-centos ~]# man 1 printf
[root@VM-24-9-centos ~]# man 2 printf
No manual entry for printf in section 2
[root@VM-24-9-centos ~]# clear
[root@VM-24-9-centos ~]# man 1 printf
[root@VM-24-9-centos ~]# man 3 printf
No manual entry for printf in section 3
[root@VM-24-9-centos ~]# yum install -y man-pages
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package man-pages.noarch 0:3.53-5.el7 will be installed
--> Finished Dependency Resolution
--> Running transaction check
---> Package man-pages-zh-CN.noarch 0:1.5.2-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
man-pages noarch 3.53-5.el7 os 5.0 M
man-pages-zh-CN noarch 1.5.2-4.el7 os 2.3 M
Transaction Summary
==================================================================================================================================================================================================================
Install 2 Packages
Total download size: 7.3 M
Installed size: 6.8 M
Downloading packages:
(1/2): man-pages-zh-CN-1.5.2-4.el7.noarch.rpm | 2.3 MB 00:00:00
(2/2): man-pages-3.53-5.el7.noarch.rpm | 5.0 MB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 24 MB/s | 7.3 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : man-pages-zh-CN-1.5.2-4.el7.noarch 1/2
Installing : man-pages-3.53-5.el7.noarch 2/2
Verifying : man-pages-3.53-5.el7.noarch 1/2
Verifying : man-pages-zh-CN-1.5.2-4.el7.noarch 2/2
Installed:
man-pages.noarch 0:3.53-5.el7 man-pages-zh-CN.noarch 0:1.5.2-4.el7
Complete!
[root@VM-24-9-centos ~]# man 3 printf
[root@VM-24-9-centos ~]# ^C
[root@VM-24-9-centos ~]#
九、nano
这个就是Linux自带的记事本,可以写个二三十行代码是没有问题的,所以这里就用它写一个hello nano,敲下nano hello.txt就会打开记事本,如下方代码,这时写好就可以看到下面的^x这个需要按下ctrl+x就可以保存然后跳出图二的选项,选择Y回车就可以了,这是用cat就可以查看写的了,这里我发现我因该写的时.c文件写成txt了,不过问题不大也能演示,代码如下
[root@VM-24-9-centos ~]# nano hello.txt
[root@VM-24-9-centos ~]# cat hello.txt
#include <stdio.h>
int main()
{
printf("Hello nano\n");
return 0;
}
[root@VM-24-9-centos ~]#
十、cp指令
这个看到我就想到了copy,就是复制指令,相当于正常使用的ctrl+c ctrl+v了,常用选项如下,测试代码如下。
[root@VM-24-9-centos ~]# pwd
/root
[root@VM-24-9-centos ~]# ls
d1 hello.txt
[root@VM-24-9-centos ~]# cp hello.txt d1
[root@VM-24-9-centos ~]# cd d1
[root@VM-24-9-centos d1]# ls
d2 d3 d4 d5 hello.txt
[root@VM-24-9-centos d1]# ^C
[root@VM-24-9-centos d1]#
十一、mv指令
这个指令就是相当于剪切了,把这个文件移到别的文件,有三个功能:
1. 视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中。
2. 当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。
3. 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。
常用选项如下,测试代码及效果如下
[root@VM-24-9-centos ~]# ls
d1 hello.txt
[root@VM-24-9-centos ~]# mv hello.txt d1
mv: overwrite ‘d1/hello.txt’? y
[root@VM-24-9-centos ~]# ls
d1
[root@VM-24-9-centos ~]# cd d1
[root@VM-24-9-centos d1]# ls
d3 d4 d5 hello.txt
十二、cat指令
这个指令在上面用过一次就是查看目标文件内容,常用选项如下,因为我不小心把上面的文件全删了,所以只好再次写一个hello.c了刚好格式改了过来,测试代码如下,值得一提的时cat这样大是正常输出,但是打成tac就是倒着输出,今天就学了这么多,如有错误欢迎补充。
[root@VM-24-9-centos ~]# nano hello.c
[root@VM-24-9-centos ~]# ls
d1 hello.c
[root@VM-24-9-centos ~]# cat hello.c
#include <stdio.h>
int main()
{
printf("hello nano\n");
return 0;
}
[root@VM-24-9-centos ~]# tac hello.c
}
return 0;
printf("hello nano\n");
{
int main()
#include <stdio.h>
[root@VM-24-9-centos ~]#