文章目录
前言
本文是给自己在生产环境中linux操作中比较常用的命令做个备忘录,也是给小白们介绍一下linux的命令基础。(持续更新)
一、Linux命令基础
1.shell是什么
大家都知道我们使用命令去操作Linux,但是你知道linux是怎么读懂命令嘛?这中间就需要有一个翻译官,shell就是在用户与内核之间充当翻译官的角色,而Bash就是Linux系统中默认使用的shell程序,文件位于/bin/bash,用户登陆时自动加载一个Shell程序;说白了,shell就是一个命令解释器。
2.Linux内部命令与外部命令的区别
查看内部还是外部命令“type”
[root@localhost ~]# type cd
cd 是 shell 内嵌
//内部命令
[root@localhost ~]# type useradd
useradd 是 /usr/sbin/useradd
//外部命令
外部命令使用过后会被缓存也就是哈希,下次调用会更快
查看哈希
[root@localhost ~]# hash
若想删除
[root@localhost ~]# hash -r
enable查看所有的内部命令
[root@localhost ~]# enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable compopt
enable continue
enable declare
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable kill
enable let
enable local
enable logout
enable mapfile
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readarray
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable suspend
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait
3.通用命令行使用格式
【命令字】 【选项】 【参数】
操作 功能 对象
命令字:是整条命令中最关键的一部分,唯一确定一条命令
选项:短格式“-”符号引导,字母,分大小写
长格式“--”引导,单词
参数:命令字的处理对象,可以是文件名,目录(路径)名或用户名等内容
个数可以是0到多个
4. 编辑Linux命令行
Tab 键:自动补齐
反斜杠“\”:强制换行
Ctrl+U组合键:清空至行首
Ctrl+K组合键:清空至行尾
Ctrl+L组合键:清屏
Ctrl+C组合键:取消本次命令编辑
5.获得命令帮助
1.help
内部命令“help”
查看shell内部命令的帮助信息
命令的‘–help’选项
适用于大多数Linux外部命令
2.man
使用man命令阅读手册页
使用“↑”“↓”方向键滚动一行文本
使用Page up和Page down 翻页
按Q或q退出阅读环境
按“/”查找内容
查找参数翻页“n”“N”
二、常用命令
查看当前工作目录-pwd
显示用户当前所在的工作目录位置
[root@localhost ~]# pwd
/root
切换工作目录-cd
切换工作目录,将用户的工作目录更改到其他位置
选项:
切换到目标位置 “cd 目标位置”
[root@localhost ~]# cd /etc
[root@localhost etc]#
若不指定目标位置,切换到当前用户的宿主目录“cd ~”
[root@localhost /]# cd /var/log/
[root@localhost log]# cd ~
[root@localhost ~]#
切换到上次执行cd命令之前所在的目录“cd -”
[root@localhost etc]# cd -
/root
[root@localhost ~]#
回到上一层目录“cd …”
[root@localhost log]# cd ..
[root@localhost var]#
回到上上层目录“cd …/ …/”
[root@localhost log]# cd ../../
[root@localhost /]#
带“/”为绝对路径,反之相对
相对路径表现形式:
列表显示目录-ls(常用选项、可结合通配符)
显示目录中内容,包括于目录和文件相关属性信息
选项:
-l 显将文件信息详细列出,通常直接使用ll,因为设置了别名
[root@localhost /]# ls -l
总用量 36
lrwxrwxrwx. 1 root root 7 7月 26 19:07 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 7月 26 19:26 boot
drwxr-xr-x. 19 root root 3280 7月 29 09:09 dev
drwxr-xr-x. 138 root root 8192 7月 29 09:09 etc
drwxr-xr-x. 3 root root 16 7月 26 19:22 home
lrwxrwxrwx. 1 root root 7 7月 26 19:07 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 7月 26 19:07 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 media
drwxr-xr-x. 2 root root 6 11月 5 2016 mnt
drwxr-xr-x. 3 root root 16 7月 26 19:10 opt
dr-xr-xr-x. 168 root root 0 7月 29 09:07 proc
dr-xr-x---. 14 root root 4096 7月 29 22:01 root
drwxr-xr-x. 38 root root 1120 7月 29 09:09 run
lrwxrwxrwx. 1 root root 8 7月 26 19:07 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 11月 5 2016 srv
dr-xr-xr-x. 13 root root 0 7月 29 09:07 sys
drwxrwxrwt. 50 root root 8192 7月 29 22:42 tmp
drwxr-xr-x. 13 root root 155 7月 26 19:07 usr
drwxr-xr-x. 21 root root 4096 7月 26 19:23 var
[root@localhost /]#
-R 递归显示,比如一个目录下还有文件,则吧那个文件也一起列出
[root@localhost log]# cd /home
[root@localhost home]# ls -R
.:
lz
./lz:
公共 模板 视频 图片 文档 下载 音乐 桌面
./lz/公共:
./lz/模板:
./lz/视频:
./lz/图片:
./lz/文档:
./lz/下载:
./lz/音乐:
./lz/桌面:
[root@localhost home]#
-d 查看目录本身信息,不显示目录下的内容
[root@localhost /]# ls -d
.
-i显示inode值
[root@localhost home]# ls -i
102025755 l
-h 友好显示,以单位为例
[root@localhost /]# ls -h
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
-lh 将文件信息以友好的方式详细列出
[root@localhost /]# ls -lh
总用量 36K
lrwxrwxrwx. 1 root root 7 7月 26 19:07 bin -> usr/bin
dr-xr-xr-x. 5 root root 4.0K 7月 26 19:26 boot
drwxr-xr-x. 19 root root 3.3K 7月 29 09:09 dev
drwxr-xr-x. 138 root root 8.0K 7月 29 09:09 etc
drwxr-xr-x. 3 root root 16 7月 26 19:22 home
lrwxrwxrwx. 1 root root 7 7月 26 19:07 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 7月 26 19:07 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 media
drwxr-xr-x. 2 root root 6 11月 5 2016 mnt
drwxr-xr-x. 3 root root 16 7月 26 19:10 opt
dr-xr-xr-x. 169 root root 0 7月 29 09:07 proc
dr-xr-x---. 14 root root 4.0K 7月 29 22:01 root
drwxr-xr-x. 38 root root 1.1K 7月 29 09:09 run
lrwxrwxrwx. 1 root root 8 7月 26 19:07 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 11月 5 2016 srv
dr-xr-xr-x. 13 root root 0 7月 29 09:07 sys
drwxrwxrwt. 50 root root 8.0K 7月 29 23:42 tmp
drwxr-xr-x. 13 root root 155 7月 26 19:07 usr
drwxr-xr-x. 21 root root 4.0K 7月 26 19:23 var
-a 遍历所有文件及目录(ls内定将文件名或目录名称开头为‘.’视为隐藏,不会列出)
[root@localhost /]# ls -a
. bin dev home lib64 mnt proc run srv tmp var
.. boot etc lib media opt root sbin sys usr
-A 跟-a唯一的不同是不列出当前目录和上一层目录
[root@localhost /]# ls -A
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
结合通配符:
“?”:匹配文件名中的一个未知字符
“*”:匹配文件名中的任意多个字符
设置别名 -alias
通过别名机制简化常用的,比较长的命令
alias 命令别名 =命令
例:
设置myls为ls -alh的别名
alias myls=‘ls -alh’
永久生效方式:
[root@localhost /]# vi /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
~
~
~
~
~
~
~
~
~
~
~
"~/.bashrc" 12L, 176C
在其中加上alias myls=‘ls -alh’
然后刷新一下,才会生效
[root@localhost /]# source /root/.bashrc
统计目录及文件空间占用情况-du
统计指定目录(或文件)所占用磁盘空间的大小
选项:
-a 统计磁盘空间占用是包括所有的文件
[root@localhost log]# du -a
0 ./tallylog
4 ./grubby_prune_debug
44 ./lastlog
16 ./wtmp
4 ./btmp
0 ./samba/old
0 ./samba
0 ./ppp
0 ./glusterfs
8 ./cups/access_log
0 ./cups/error_log
0 ./cups/page_log
8 ./cups
1536 ./audit/audit.log
1536 ./audit
0 ./pluto/peer
0 ./pluto
0 ./ntpstats
0 ./chrony
0 ./libvirt/qemu
0 ./libvirt
0 ./speech-dispatcher
0 ./sssd
32 ./gdm/:1.log
0 ./gdm/:1-greeter.log
4 ./gdm/:0.log.4
0 ./gdm/:0.log.3
32 ./gdm/:0.log.2
32 ./gdm/:0.log.1
20 ./gdm/:0.log
0 ./gdm/:0-greeter.log.4
0 ./gdm/:0-greeter.log.3
0 ./gdm/:0-greeter.log.2
0 ./gdm/:0-greeter.log.1
0 ./gdm/:0-greeter.log
120 ./gdm
1664 ./messages
32 ./secure
4 ./maillog
0 ./spooler
128 ./sa/sa26
152 ./sa/sa27
296 ./sa/sa28
204 ./sa/sa29
192 ./sa/sar29
8 ./sa/sa30
980 ./sa
8 ./tuned/tuned.log
8 ./tuned
0 ./qemu-ga
36 ./anaconda/anaconda.log
244 ./anaconda/syslog
24 ./anaconda/X.log
32 ./anaconda/program.log
532 ./anaconda/packaging.log
156 ./anaconda/storage.log
4 ./anaconda/ifcfg.log
0 ./anaconda/ks-script-cMeFQy.log
0 ./anaconda/ks-script-vcezRj.log
1808 ./anaconda/journal.log
2836 ./anaconda
0 ./rhsm
0 ./boot.log
12 ./vmware-vgauthsvc.log.0
20 ./vmware-vmsvc.log
4 ./firewalld
36 ./cron
24 ./Xorg.9.log
4 ./wpa_supplicant.log
0 ./yum.log
124 ./dmesg.old
36 ./Xorg.0.log.old
12 ./vmware-vmusr.log
56 ./boot.log-20210727
16 ./boot.log-20210728
40 ./Xorg.1.log
124 ./dmesg
28 ./Xorg.0.log
28 ./boot.log-20210729
7824 .
-h 以更人性化的方式(默认以kb计数,但不显示单位)显示出统计结果
[root@localhost log]# du -h
0 ./samba/old
0 ./samba
0 ./ppp
0 ./glusterfs
8.0K ./cups
1.5M ./audit
0 ./pluto/peer
0 ./pluto
0 ./ntpstats
0 ./chrony
0 ./libvirt/qemu
0 ./libvirt
0 ./speech-dispatcher
0 ./sssd
120K ./gdm
980K ./sa
8.0K ./tuned
0 ./qemu-ga
2.8M ./anaconda
0 ./rhsm
7.7M .
-s 只统计所占用空间总的(Summary)大小,而不是统计每个子目录,文件的大小
[root@localhost log]# du -s
7824 .
-sh 以更人性化的方式统计所占用空间总的大小
du -sh/var/log/
创建新目录 -mkdir
选项 -p 一次性创建嵌套的多层目录
[root@localhost home]# mkdir -p /a/b/c
[root@localhost home]# tree /a
/a
└── b
└── c
2 directories, 0 files
[root@localhost home]#
创建文件 touch
[root@localhost home]# touch cc dd ee
[root@localhost home]# ls
aa b.txt class13 dd d.txt e.txt lz test1.txt
a.txt cc c.txt demo1.txt ee kgc passwd
创建链接文件 -ln
软连接:ln -s 相当与创建了一个快捷方式
[root@localhost home]# ln -s demo1.txt test.txt
[root@localhost home]# ls -lh
总用量 16K
drwxr-xr-x. 11 root root 88 7月 30 16:50 aa
-rw-r--r--. 1 root root 0 7月 30 17:20 a.txt
-rw-r--r--. 1 root root 0 7月 30 17:20 b.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 cc
drwxr-xr-x. 2 root root 45 7月 30 17:20 class13
-rw-r--r--. 1 root root 0 7月 30 17:20 c.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 dd
-rw-r--r--. 2 root root 13 7月 30 17:12 demo1.txt
-rw-r--r--. 1 root root 0 7月 30 17:20 d.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 ee
-rw-r--r--. 1 root root 0 7月 30 17:20 e.txt
drwxr-xr-x. 2 root root 6 7月 30 16:43 kgc
drwx------. 14 lz lz 4.0K 7月 28 11:42 lz
-rw-r--r--. 1 root root 2.1K 7月 30 09:24 passwd
-rw-r--r--. 2 root root 13 7月 30 17:12 test1.txt
lrwxrwxrwx. 1 root root 9 7月 30 21:07 test.txt -> demo1.txt
//软连接
硬链接:ln 创建硬链接,相当于复制了一个副本
[root@localhost home]# ln demo1.txt test2.txt
[root@localhost home]# ll
总用量 20
drwxr-xr-x. 11 root root 88 7月 30 16:50 aa
-rw-r--r--. 1 root root 0 7月 30 17:20 a.txt
-rw-r--r--. 1 root root 0 7月 30 17:20 b.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 cc
drwxr-xr-x. 2 root root 45 7月 30 17:20 class13
-rw-r--r--. 1 root root 0 7月 30 17:20 c.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 dd
-rw-r--r--. 3 root root 13 7月 30 17:12 demo1.txt
-rw-r--r--. 1 root root 0 7月 30 17:20 d.txt
-rw-r--r--. 1 root root 0 7月 30 21:02 ee
-rw-r--r--. 1 root root 0 7月 30 17:20 e.txt
drwxr-xr-x. 2 root root 6 7月 30 16:43 kgc
drwx------. 14 lz lz 4096 7月 28 11:42 lz
-rw-r--r--. 1 root root 2098 7月 30 09:24 passwd
-rw-r--r--. 3 root root 13 7月 30 17:12 test1.txt
-rw-r--r--. 3 root root 13 7月 30 17:12 test2.txt
//硬链接
lrwxrwxrwx. 1 root root 9 7月 30 21:07 test.txt -> demo1.txt
复制文件或目录 -cp
将需要复制的文件或目录(源)重建一份,并保存为新的
选项:
-f 覆盖目标同名文件或目录时不进行提醒,直接强制复制
-i 覆盖目标,同名文件或目录时,提醒用户确认
-p 复制时,保持源文件的权限、属性及时间标记等属性不变
-r 复制目录时,必须使用此选项,表示递归复制所有文件及子目录
例:
cp a.txt b.txt c.txt class
//复制a,b,c .txt 到class
cp -r classs/ /opt
//复制class到opt
查找命令/文件存放目录 -which
搜索范围由环境便令PATH决定
[root@localhost home]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
或which -a
[root@localhost home]# which -a tree
/usr/bin/tree
删除指定的文件或目录 -rm
谨慎使用
选项:
-f 强制删除
-i 删除时,提醒用户确认
-r 删除目录时,必须使用此选项
例:
rm -rf aa/
rm -a aa/
移动文件或目录-mv
将指定的文件或目录转移位置,如果目标位置相同,则相当于重命名操作
例:
mv cc cc1
//改名
mv cc /opt
//移动到opt
查找文件或目录 -find
采用递归方式,根据目标的名称、类型、大小等不同属性进行精细查找
常用查找类型:
-name 按名称查找 根据目标的名称进行查找,允许使用“*”及“?"通配符
-size 按文件大小查找 根据目标文件的大小进行查找,一般使用”+“及”-”号设置超过或小于指定的大小作为查找条件
-user 按文件属性查找 根据文件是否属于目标用户进行查找
-type 按文件类型查找 根据文件的类型查找,文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)
使用find命令实现多个查找条件
-a 表示 而且(and)
-o 表示 或者 (or)
例:
find /boot -size +1024k -a -name"vmlinuz"
find /boot -size +1024k -o -name"vmlinuz"
总结
其实这么多命令,总的来说也就“增删改查”四个大类。要想记住这些命令,还是需要多敲。