首页 > 技术文章 > Linux的基础命令(1)

LiangGaRy 2020-12-25 08:44 原文

一、基础命令的讲解

1、了解命令的格式

命令的一般格式

 1     命令 选项 参数
 2 比如说:
 3 [root@rhel8 ~]# ls -l /etc/fstab 
 4 -rw-r--r--. 1 root root 579 Dec 17 22:22 /etc/fstab
 5 
 6 前面九位解释:
 7 -rw-r--r--    
 8    -:代表的是文件
 9    d:代表的是目录
10    l:代表的是链接文件
11    c:块设备文件
12    s:套接字文件
13 rw-:所有者的权限
14 r--:所属组的权限        
15 r--:其他人的权限            
16 
17 选项:
18     又分长格式选项:如 --long
19     短格式选项:-l
20     复合选项:-ald  

 

绝对路径和相对路径

绝对路径:从根/开始的路径就是绝对路径
[root@rhel8 ~]# ls /etc/fstab
/etc/fstab
 
相对路径:不是从/开始的路径就是相对路径
[root@rhel8 etc]# ls fstab
fstab

 

2、ls命令

作用:查看目录下的文件列表
语法:
ls 【选项】 参数
选项:
-l:长格式显示内容
-d:仅仅查看目录本身,不查看目录下的内容
-t:按照文件或者目录的修改时间排序
-a:查看所有的内容,包括. ..
-h:人性化显示内容的大小
-R:递归查看目录下的内容
#普通不带选项查看
[root@rhel8 ~]# ls /etc/fstab 
/etc/fstab

#长格式显示
[root@rhel8 ~]# ls -l /etc/fstab 
-rw-r--r--. 1 root root 579 Dec 17 22:22 /etc/fstab

#查看目录本身
[root@rhel8 ~]# ls -ld /etc
drwxr-xr-x. 135 root root 8192 Dec 25 00:29 /etc

#人性化显示查看内容大小
[root@rhel8 ~]# ls -ldh /etc/
drwxr-xr-x. 135 root root 8.0K Dec 25 00:29 /etc/

#递归查看内容
[root@rhel8 ~]# mkdir -p a/b/c/d
[root@rhel8 ~]# ls -Rt a
a:
b

a/b:
c

a/b/c:
d

a/b/c/d:

 

3、pwd命令

作用:查看当前路径
[root@rhel8 ~]# cd /etc/
[root@rhel8 etc]# pwd
/etc

 

4、cd命令

作用:切换目录
语法:
cd 路径
#切换到用户家目录
[root@rhel8 etc]# cd ~
[root@rhel8 ~]# cd

#切换到上级目录
[root@rhel8 ~]# cd ..
[root@rhel8 /]# pwd
/

#切换到当前目录
[root@rhel8 /]# cd .
[root@rhel8 /]# pwd
/

#切换到上次所在的地方
[root@rhel8 /]# cd /etc/
[root@rhel8 etc]# cd -
/

 

5、cat命令

作用:查看文件内容
语法:
cat 【选项】 文件名
选项
-n:显示行号(包括空白行)
-b:显示行号(不包括空白行)
-A:显示行尾结束符
[root@rhel8 /]# cat /etc/issue
\S
Kernel \r on an \m

[root@rhel8 /]# cat /etc/issue -n
     1    \S
     2    Kernel \r on an \m
     3    
[root@rhel8 /]# cat /etc/issue -v
\S
Kernel \r on an \m

[root@rhel8 /]# cat /etc/issue -A
\S$
Kernel \r on an \m$
$

 

6、mkdir命令

作用:创建目录
语法:
mkdir 【选项】 目录
选项:
-p:递归创建
-m:指定创建目录的权限
[root@rhel8 ~]# mkdir /data
[root@rhel8 ~]# mkdir /data/aa

#递归创建
[root@rhel8 ~]# mkdir aa/bb/cc -p
[root@rhel8 ~]# tree  aa
aa
└── bb
    └── cc

2 directories, 0 files

#直接指定权限
[root@rhel8 ~]# mkdir -m ug=rwx lala
[root@rhel8 ~]# ll -d lala
drwxrwxrwx. 2 root root 6 Dec 25 08:08 lala

 

7、touch命令

作用:创建空白文件,修改文件的时间戳
语法:
touch 【选项】 文件名
选项
-t:指定使用的日期,修改时间戳
-a:只更改访问时间
-m:只修改修改时间
[root@rhel8 tmp]# ll a.txt 
-rw-r--r--. 1 root root 0 Dec 25 08:10 a.txt

#直接修改时间戳
[root@rhel8 tmp]# touch -t 201904262312 a.txt 
[root@rhel8 tmp]# ll a.txt 
-rw-r--r--. 1 root root 0 Apr 26  2019 a.txt

 

8、more命令

作用:分页显示文件内容
语法:
more 文件名
more 的使用
q:退出
回车:下一页
空格:下一行
/:向下查找字符串
?字符串:向上查找字符串

9、less命令

作用:分页显示文件内容,可以向上翻页
语法:
less 文件名
less的使用
q:退出
回车:下一页
空格:下一行
/:向下查找字符串
?字符串:向上查找字符串

10、head命令

作用:查看文件头部信息,默认前十行
语法:
head 【选项】 参数
选项:
-n:指定显示的行数

 

[root@rhel8 tmp]# head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

 

11、tail命令

作用:显示文件的尾部信息,默认是后10行
语法:
tail 【选项】 参数
选项
-n:指定尾部多少汗
-f:动态显示内容
#查看尾部后两行
[root@rhel8 tmp]# tail -n 2 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
zhangsan:x:1000:1000:zhangsan:/home/zhangsan:/bin/bash

#动态查看日志文件
[root@rhel8 tmp]# tail -f  /var/log/messages

 

12、grep命令

作用:按照要求查找字符串
语法:
grep 【选项】 文件
选项:
-v:反选
-i:忽略大小写查找
-n:显示行号
#查找/etc/passwd中不包含root的行
[root@rhel8 tmp]# grep -v "root" /etc/passwd

#查找/etc/passwd中包含root的行
[root@rhel8 tmp]# grep  "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

推荐阅读