Linux基础二:最常用的基本命令 入门

这篇博客介绍了Linux的基础命令,包括ls、cd、pwd、mkdir、rm、cp、mv等,详细阐述了命令的语法、选项和用途。还提到了man手册的使用,以及常见的归档和压缩文件格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、命令语法:

<命令> [选项] [参数]
rm -r -f =rm -rf

二、命令类型:

可使用type命令来查看某条命令是何种类型:

[root@ymm ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@ymm ~]# type cd
cd 是 shell 内嵌

三、基础命令之ls

-l:这里是小写的L,不是数字1,长格式显示

[root@ymm ~]# ls -l
总用量 3963932
-rw-r--r--. 1 root root          0 12月 11 09:04 1.txt
drwxr-xr-x. 5 root root         60 2月  19 12:04 A
-rw-------. 1 root root       1505 11月 26 03:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root         79 12月 30 09:14 dir1
-rw-r--r--. 1 root root       2611 12月 30 09:24 dir1.zip
drwxr-xr-x. 3 root root         18 12月 30 09:24 dir2

-h:做单位转换

[root@ymm ~]# ls -h
1.txt                   mystery_chapter1.odf     tv_season1_episode4.ogg
A                       mystery_chapter2.odf     tv_season1_episode5.ogg
anaconda-ks.cfg         mystery_chapter3.odf     tv_season1_episode6.ogg
dir1                    mystery_chapter4.odf     tv_season2_episode1.ogg
dir1.zip                mystery_chapter5.odf     tv_season2_episode2.ogg
dir2                    mystery_chapter6.odf     tv_season2_episode3.ogg
Documents               mystery_chapter7.odf     tv_season2_episode4.ogg

-a:显示隐藏文件

[root@ymm ~]# ls -a
.                       ifcfg-etho            tv_season1_episode1.ogg
..                      manman.repo           tv_season1_episode2.ogg
1.txt                   man.sh                tv_season1_episode3.ogg
A                       mycd.iso              tv_season1_episode4.ogg

-d:显示目录自身的属性

[root@ymm ~]# ls -d

.
-t:以时间排序

[root@ymm ~]# ls -t
A                        tv_season1_episode4.ogg  dir1.zip
mystery_chapter1.odf     tv_season1_episode5.ogg  dir1
mystery_chapter2.odf     tv_season1_episode6.ogg  mycd.iso
mystery_chapter3.odf     tv_season2_episode1.ogg  manman.repo
mystery_chapter4.odf     tv_season2_episode2.ogg  Documents

-r:逆序显示

[root@ymm ~]# ls -r
Watched                  tv_season1_episode1.ogg  manman.repo
tv_season2_episode6.ogg  pingtest.sh              ifcfg-etho
tv_season2_episode5.ogg  mystery_chapter8.odf     home.2019-12-30.tar.gz
tv_season2_episode4.ogg  mystery_chapter7.odf     Documents
tv_season2_episode3.ogg  mystery_chapter6.odf     dir2

四、常用基础命令(一)

cd:切换目录,不能进入文件夹需用vi编辑才可以进入

[root@ymm ~]# cd A
[root@ymm A]# 

pwd:打印当前工作目录

[root@ymm A]# pwd
/root/A

mkdir:创建目录
-p:创建目录时若父目录不存在则自动创建

[root@ymm A]# mkdir 123
[root@ymm A]# ls
123  2  3  4  c  d  e

-v:显示创建目录的过程

[root@ymm A]# mkdir -v 456
mkdir: 已创建目录 "456"

touch:创建空文件或更新时间戳创建空文件夹

[root@ymm A]# touch 234
[root@ymm A]# ls
123  2  234  3  4  456  678  c  d  e
更新时间戳
[root@ymm A]# ll
总用量 0
drwxr-xr-x. 2 root root  6 2月  19 14:20 123
-rw-r--r--. 1 root root  0 2月  19 10:17 2
[root@ymm A]# touch 2
[root@ymm A]# ll
总用量 0
drwxr-xr-x. 2 root root  6 2月  19 14:20 123
-rw-r--r--. 1 root root  0 2月  19 14:24 2

stat:显示文件或文件系统的状态

[root@ymm A]# stat 234
  文件:"234"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:51505503    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2020-02-19 14:23:38.979238754 +0800
最近更改:2020-02-19 14:23:38.979238754 +0800
最近改动:2020-02-19 14:23:38.979238754 +0800
创建时间:-

rm:删除文件
-r:递归删除,删除目录时必须使用此选项

[root@ymm A]# rm -r c
rm:是否删除目录 "c"?y

-f:强制删除,不询问

删除文件,不能删除目录
[root@ymm A]# rm -f 234

-rf 强制删除目录,不询问

[root@ymm A]# rm -rf 456
rm -rf /*删除所有数据

cp:复制文件,一个文件到一个文件,多个文件到一个目录

[root@ymm A]# cp 2 5
[root@ymm A]# ls
123  2  3  4  5  678  d  e
-a:归档复制,常用于备份[root@ymm A]# cp -a 2 5
cp:是否覆盖"5"? y
[root@ymm A]# ls
123  2  3  4  5  678  d  e

-r:递归拷贝,拷贝目录时必须使用此选项

[root@ymm A]# cp -r 123 456
[root@ymm A]# ls
123  2  3  4  456  5  678  d  e

-p:拷贝时保留原权限

[root@ymm A]# cp -p 123 456
cp: 略过目录"123"
[root@ymm A]# ls
123  2  3  4  456  5  678  d  e

mv:移动文件,多个文件和目录同时移动的话后面要是目录

[root@ymm A]# mv 123 456
[root@ymm A]# ls
2  3  4  456  5  678  d  e
多个文件和目录同时移动的话后面要是目录
[root@ymm A]# mv 2 3  4 456/
[root@ymm A]# ls
456  5  678  d  e

五、常用基础命令(二)

cat:拼接文件内容并输出至标准输出(屏幕)

[root@ymm A]# cat 5
dfbh
dfs
vgt
bgh
ct
fyv

-n:显示行号

[root@ymm A]# cat -n 5
     1	dfbh
     2	dfs
     3	vgt
     4	bgh
     5	ct

tac:连接文件并倒序打印内容至标准输出

[root@ymm A]# tac 5
hjfxgdf
b
g
cv
gbvb
bcvf
dxfdgfbv
fyv

more:全屏查看文本文件内容,只能从前往后看,看完自动退出,more 按空格往下翻按B往上翻

[root@ymm A]# more 5
dfbh
dfs
vgt

less:全屏查看文本文件内容,可从前往后看亦可从后往前看,看完不会自动退出,按q退出

[root@ymm A]# tac 5
hjfxgdf
b
g
cv
gbvb

head:从文件首部开始打印文件内容,默认打印10行

[root@ymm A]# head 5
dfbh
dfs
vgt
bgh
ct
fyv
dxfdgfbv
bcvf
gbvb
cv

-n 15:查看文件前15行的内容

[root@ymm A]# head -3 5
dfbh
dfs
vgt

tail:从文件尾部开始打印文件内容,默认打印10行

[root@ymm A]# tail 5
bgh
ct
fyv
dxfdgfbv
bcvf
   -n 3:查看文件尾部3行的内容
[root@ymm A]# tail -3 5
g
b
hjfxgdf

-f:实时查看文件的更新

[root@ymm A]# tail -f 5
bgh
ct
fyv
dxfdgfbv
bcvf
gbvb

六、常用基础命令(三)

wc:文本统计

[root@ymm A]# wc 5
13 13 58 5

-c:统计文本字节数

[root@ymm A]# wc -c 5
58 5

-w:统计文本单词数

[root@ymm A]# wc -w 5
13 5

-l:统计文本行数

[root@ymm A]# wc -l 5
13 5

du:查看文件或目录占用的磁盘空间大小

[root@ymm A]# du 5
4	5

-s:显示总的占用空间大小

[root@ymm A]# du -s 5
4	5

-h:单位转换,以更友好的方式显示大小

[root@ymm A]# du -h 5
4.0K	5

df:报告文件系统磁盘空间使用情况

[root@ymm A]# df
文件系统                 1K-块    已用     可用 已用% 挂载点
/dev/mapper/rhel-root 17811456 6028952 11782504   34% /
devtmpfs                922444       0   922444    0% /dev
tmpfs                   933512       0   933512    0% /dev/shm
tmpfs                   933512    8836   924676    1% /run
tmpfs                   933512       0   933512    0% /sys/fs/cgroup
/dev/sda1              1038336  146084   892252   15% /boot
tmpfs                   186704       0   186704    0% /run/user/0

-h:单位转换,以更友好的方式显示大小

[root@ymm A]# df -h
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root   17G  5.8G   12G   34% /
devtmpfs               901M     0  901M    0% /dev
tmpfs                  912M     0  912M    0% /dev/shm
tmpfs                  912M  8.7M  904M    1% /run
tmpfs                  912M     0  912M    0% /sys/fs/cgroup
/dev/sda1             1014M  143M  872M   15% /boot
tmpfs                  183M     0  183M    0% /run/user/0

七、常用基础命令(四)

hostname:查看或临时修改主机名,重开终端有效,重启失效

[root@ymm ~]# hostname china
[root@ymm ~]# bash
[root@china ~]# 

hostnamectl:查看或永久修改主机名,重开终端有效,重启依然有效

[root@ymm ~]# hostnamectl set-hostname ymm-frr
[root@ymm ~]# bash
[root@ymm-frr ~]# 

clear:清屏whoami:显示当前登录用户

[root@ymm-frr ~]# whoami
root

w:显示当前在线用户并显示其正在运行的命令

[root@ymm-frr ~]# w
 15:28:34 up  8:09,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      二14   25:17m  0.01s  0.01s -bash
root     pts/0    192.168.20.1     10:26    3:24m  0.13s  0.13s -bash
root     pts/1    192.168.20.1     15:14    2.00s  0.04s  0.02s w

who:查看当前在线用户

[root@ymm-frr ~]# who
root     tty1         2020-02-18 14:11
root     pts/0        2020-02-19 10:26 (192.168.20.1)
root     pts/1        2020-02-19 15:14 (192.168.20.1)

which:显示指定命令的绝对路径

[root@ymm-frr ~]# which
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
  --help,          Print this help and exit successfully.
  --skip-dot       Skip directories in PATH that start with a dot.
  --skip-tilde     Skip directories in PATH that start with a tilde.
  --show-dot       Don't expand a dot to current directory in output.
  --show-tilde     Output a tilde for HOME directory for non-root.
  --tty-only       Stop processing options on the right if not on tty.
  --all, -a        Print all matches in PATH, not just the first
  --read-alias, -i Read list of aliases from stdin.
  --skip-alias     Ignore option --read-alias; don't read stdin.
  --read-functions Read shell functions from stdin.
  --skip-functions Ignore option --read-functions; don't read stdin.

Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.

If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.

Report bugs to .

cal:打印日历,后可直接接入年份

[root@ymm-frr ~]# cal
      二月 2020     
日 一 二 三 四 五 六
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29

[root@ymm-frr ~]# cal 1999
                               1999                               

        一月                   二月                   三月        
日 一 二 三 四 五 六      日 一 二 三 四 五 六     日 一 二 三 四 五 六
                1  2       1  2  3  4  5  6       1  2  3  4  5  6
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    7  8  9 10 11 12 13
10 11 12 13 14 15 16   14 15 16 17 18 19 20   14 15 16 17 18 19 20
17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27
24 25 26 27 28 29 30   28                     28 29 30 31
31
        四月                   五月                   六月        
日 一 二 三 四 五 六      日 一 二 三 四 五 六      日 一 二 三 四 五 六
             1  2  3                      1          1  2  3  4  5
 4  5  6  7  8  9 10    2  3  4  5  6  7  8    6  7  8  9 10 11 12
11 12 13 14 15 16 17    9 10 11 12 13 14 15   13 14 15 16 17 18 19
18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26
25 26 27 28 29 30      23 24 25 26 27 28 29   27 28 29 30
                       30 31

ldd:查看指定程序有哪些依赖库文件

八、常用基础命令(五)

date:显示或设置日期与时间

[root@ymm-frr ~]# date
2020年 02月 19日 星期三 15:33:44 CST

-s:以字符串方式设置时间
格式化输出时间:+
%Y:年
%m:月
%d:日
%H:时
%M:分
%S:秒

[root@ymm-frr ~]# date '+%Y-%m-%d  %H:%M;%S'
2020-02-19  15:37;46

九、man手册注意事项:按空格往下翻按B往上翻 按q退出

[]:可选
<>:必选
…:可出现多次
|:多选一
{}:分组
NAME:命令名称及功能简要说明
SYNOPSIS:用法说明,包括可用的选项
DESCRIPTION:命令功能的详尽说明,可能包括每一个选项的意义
OPTIONS:说明每一个选项的意义
FILES:此命令相关的配置文件
BUGS:报告bug
EXAMPLES:命令示例
SEE ALSO:另外参照
翻屏:空格(向后翻一屏),b键(向前翻一屏)
查找:
	/KEYWORD:从前往后查找KEYWORD
	?KEYWORD:从后往前查找KEYWORD
	n:下一个匹配的行
	N:前一个匹配的行
	q:退出

十、通配符
*:匹配任意长度的任意字符(表示所有)
?:匹配任意单个字符

[root@ymm-frr A]# ls ?
5   d    c   e

[]:匹配指定范围内的任意单个字符

[root@ymm-frr A]# ls [1-4]
1  2  3  4
    [[:space:]]:表示空白字符
[[:punct:]]:表示标点符号
[root@ymm-frr A]# ls [[:punct:]]
,  !
[[:lower:]]:表示小写字母
[root@ymm-frr A]# ls [[:lower:]]
d    c      e
[[:upper:]]:表示大写字母
[root@ymm-frr A]# ls [[:upper:]]
A  B  C
[[:alpha:]]:表示大小写字母
[root@ymm-frr A]# ls [[:alpha:]]
A  B  C    d    c     e
[[:digit:]]:表示数字
[root@ymm-frr A]# ls [[:digit:]]
1  2  3  4  5
[[:alnum:]]:表示数字和大小写字母
[root@ymm-frr A]# ls [[:alnum:]]
1  2  3  4  5  A  B  C   d   c  e

[^]:匹配指定范围之外的任意单个字符

十一、常见的归档与压缩文件格式.gz

.gz
.bz2
.xz
.zip
.Z

十二、压缩工具

gzip

[root@ymm-frr A]# gzip 1
[root@ymm-frr A]# ls
,  !  1.gz  2  3  4  456  5  678  A  B  C  d  e

gzip解压缩

[root@ymm-frr A]# gzip -d 1.gz
[root@ymm-frr A]# ls
,  !  1  2.bz2  3.xz  4  456  5  678  A  B  C  d  e

bzip2

[root@ymm-frr A]# ls
,  !  1.gz  2.bz2  3  4  456  5  678  A  B  C  d  e

bzip2 解压缩

[root@ymm-frr A]# ls
,  !  1  2  3.xz  4  456  5  678  A  B  C  d  e

xz

[root@ymm-frr A]# xz 3
[root@ymm-frr A]# ls
,  !  1.gz  2.bz2  3.xz  4  456  5  678  A  B  C  d  e

xz解压缩

[root@ymm-frr A]# xz -d 3.xz
[root@ymm-frr A]# ls
,  !  1  2  3  4  456  5  678  A  B  C  d  e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值