Linux系统常用命令
Linux系统常用命令
一.两种符号
(1)“ > ” 重定向符号:重定向符号前面的内容覆盖到指定文件(重定向符号后面的文件)中,
若文件原本就存在,其内容会被全新的输出所替代。
(2)“ | ” 管道符 :管道符前面命名执行的结果给管道符后面命令当参数(对象)使用
二.Linux命令的辅助工具
Ctrl + l ------清屏
Tab ------补齐
\ ------换行
Ctrl + u -------删除光标之前
Ctrl + k -------删除光标之后
Ctrl + c -------取消当前命令的编辑
三.命令管理文件和目录
-
pwd ------print working directory(查看当前工作目录)
-
cd ------change directory (切换工作目录)
/etc/sysconfig/ //绝对路径,以根目录"/"为起点 etc/sysconfig/ //相对路径,以当前工作目录为起点cd . //表示切换到当前目录(不变) cd .. //表示切换到当前目录的上一级目录 cd ~ //表示切换到当前用户的宿主目录(家目录) cd - //表示返回原来所在目录(一般用于两个目录之间的切换) [root@stw ~]# cd /etc/sysconfig/network-scripts/ [root@stw network-scripts]# cd /boot/ [root@stw boot]# cd /etc/sysconfig/network-scripts/ [root@stw network-scripts]# cd - /boot [root@stw boot]# cd - /etc/sysconfig/network-scripts
-
ls ------list (列表显示目录内容)
-a //显示所有(all)的子目录和文件,包括隐藏文件-A //显示除了隐藏文件外的其他子目录和文件-l //以长格式(long)显示文件和目录列表的详细信息,包括文件类型、权限、大小等等-d //显示目录(directory)本身的属性,而不是显示目录的内容-r //reverse,倒序列出目录和文件-R //以递归(Recursive)的方式显示置顶目录及其子目录中的所有内容-h //human-readable,为了方便阅读,给列出的文件信息中文件大小加一个单位(K,M,G),一 般是和-l一起使用-t //time 按照时间信息排序,默认是由后往前排序。-S //size 按文件大小排序,由大到小
-
-
du -------disk usage(统计目录及文件的空间占用情况)
-a //统计该目录中所有目录及文件的磁盘空间占用情况 -h //给占用空间大小赋予单位, -s //统计参数(对象)的总共占用空间大小。 //一般使用du -sh [root@stw ~]# du -sh /boot/ 131M /boot/
-
mkdir ------make directory (创建新目录) 格式; mkdir 目录名
-p //一次性创建嵌套的多层目录
[root@stw tmp]# mkdir a
[root@stw tmp]# ls
a
[root@stw tmp]# mkdir b c d
[root@stw tmp]# ls
a b c d
[root@stw tmp]# mkdir -p /aa/bb/cc //注意一次性创建嵌套多层目录需要 -p //不带 -p 就需要前面的 aa 目录和 bb 目录都存在才能创建
[root@stw tmp]# ls
a b c d
[root@stw tmp]# cd /
[root@stw /]# ls
aa boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv text usr
[root@stw /]# cd /aa
[root@stw aa]# ls
bb
[root@stw aa]# cd /aa/bb
[root@stw bb]# ls
cc
- touch (创建空文件) 格式: touch 文件名
touch a b c //同一目录下,可以同时创建多个文件
[root@stw tmp]# touch file1 file2 file3
[root@stw tmp]# ls
file1 file2 file3
[root@stw tmp]# touch file{4..6}
[root@stw tmp]# ls
file1 file2 file3 file4 file5 file6
- ln ------link (创建链接)
a、创建硬链接 ln 源文件路径 链接文件路径
b、创建软链接 ln -s 源文件路径 链接文件路径
共同点:无论是访问软连接还是硬链接,与直接访问原始文件的效果是一样的。
修改链接文件内容,源文件也随之改动。
区别:
硬链接:只能对文件创建链接,源文件删除后,链接的文件仍可用
软连接:可以对文件也可以对目录创建软连接,源文件删除后,软连接就失效。
[root@stw tmp]# ln a harda
[root@stw tmp]# ls -l
total 8
-rw-r--r--. 2 root root 5 Jul 24 11:35 a
-rw-r--r--. 2 root root 5 Jul 24 11:35 harda
[root@stw tmp]# cat harda
abcd
[root@stw tmp]# echo 123 > harda
[root@stw tmp]# cat harda
123
[root@stw tmp]# cat a
123[root@stw tmp]# rm -rf a
[root@stw tmp]# cat harda
123
//可以看出源文件删除后,链接的文件仍可用
[root@stw tmp]# rm -rf *
[root@stw tmp]# echo abc > a
[root@stw tmp]# ls
a
[root@stw tmp]# cat a
abc
root@stw tmp]# ln -s a softa
[root@stw tmp]# ll
total 4
-rw-r--r--. 1 root root 4 Jul 24 11:38 a
lrwxrwxrwx. 1 root root 1 Jul 24 11:39 softa -> a
[root@stw tmp]# echo 111 > a
[root@stw tmp]# cat a
111
[root@stw tmp]# cat softa
111
//共同点:可以看出修改了链接的文件内容,源文件内容页随之改变
[root@stw tmp]# rm -rf a
[root@stw tmp]# ll
total 0
lrwxrwxrwx. 1 root root 1 Jul 24 11:39 softa -> a //这里的softa -> a会闪红色
[root@stw tmp]# cat softa
cat: softa: No such file or directory
//可以看出源文件删除后,软连接就失效
- cp ------copy (复制文件或目录)
-i //interactive 交互式的 覆盖目标同名目录或者文件是提醒用户确认
-f //force强制 覆盖目标同名目录或文件时不进行提醒,可直接强制复制
-p //复制时保留原文件的权限、时间标记等属性不变。
-r //复制目录时必须使用此选项,表示递归复制所有文件及子目录//cp目录的时候一定要加上-r选项
[root@stw tmp]# cp /etc/fstab /tmp
[root@stw tmp]# ls
fstab
[root@stw tmp]# cp -r /boot/grub2/ /tmp/
[root@stw tmp]# ls
fstab grub2// - p(保留原来的属性)
[root@stw tmp]# ll /etc/fstab
-rw-r--r--. 1 root root 501 Jul 23 11:55 /etc/fstab
[root@stw tmp]# cp /etc/fstab /tmp/
[root@stw tmp]# ll
total 4
-rw-r--r--. 1 root root 501 Jul 24 11:46 fstab
[root@stw tmp]# cp -p /etc/fstab /tmp/
cp: overwrite ‘/tmp/fstab’? y
[root@stw tmp]# ll
total 4
-rw-r--r--. 1 root root 501 Jul 23 11:55 fstab
- rm ------remove (移除 删除文件或目录)
-f //删除文件或目录时不进行提醒,可直接强制删除。
-i //删除文件或目录时提醒用户确认。
-r //删除目录时必须使用此选项,表示递归删除整个目录树(应谨慎使用)
- mv ------move (移动 移动文件或目录、重命名)
[root@stw ~]# touch file1
[root@stw ~]# ls
anaconda-ks.cfg Documents file1 Music Public Videos
Desktop Downloads initial-setup-ks.cfg Pictures Templates
[root@stw ~]# cd /tmp/
[root@stw tmp]# mv /root/file1 /tmp
[root@stw tmp]# ls /root/
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@stw tmp]# ls
file1
[root@stw tmp]# mv file1 file111
[root@stw tmp]# ls
file111
- alias ------自定义别名
unalias myls //移除别名
vim .bashrc //永久修改别名
source .bashrc[root@stw ~]# cd /etc/sysconfig/network-scripts/
[root@stw network-scripts]# alias netdir="cd /etc/sysconfig/network-scripts/"
[root@stw network-scripts]# cd
[root@stw ~]# netdir
[root@stw network-scripts]# cd
[root@stw ~]# alias myls="ls -l /boot/"
[root@stw ~]# myls
total 117748
-rw-r--r--. 1 root root 151918 Nov 9 2018 config-3.10.0-957.el7.x86_64
drwx------. 3 root root 17 Nov 9 2018 efi
drwxr-xr-x. 2 root root 27 Jul 23 11:56 grub
drwx------. 5 root root 97 Jul 23 12:10 grub2
-rw-------. 1 root root 74000923 Jul 23 12:06 initramfs-0-rescue-d79c4a23e02b4ba18b95056537e1bedc.img
-rw-------. 1 root root 29265416 Jul 23 12:10 initramfs-3.10.0-957.el7.x86_64.img
-rw-r--r--. 1 root root 314036 Nov 9 2018 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root 3543471 Nov 9 2018 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 Jul 23 12:06 vmlinuz-0-rescue-d79c4a23e02b4ba18b95056537e1bedc
-rwxr-xr-x. 1 root root 6639904 Nov 9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@stw ~]# unalias netdir //取消修改别名
[root@stw ~]# netsir
bash: netsir: command not found...
[root@stw ~]# unalias myls //取消修改别名
[root@stw ~]# myls
bash: myls: command not found...
[root@stw ~]#
- find ------查找文件和目录
-name //根据文件名进行查找 *代表任意字符 ?代表单个字符---区分大小写
-iname //不区分大小写
-size //根据文件大小查询 +代表大于 -代表小于 常用的单位K、M、G
-user //根据文件是否属于目标用户查询
-type //根据文件类型查找//普通文件(f)//目录文件(d)//块设备文件(b):指的是成块读取数据的设备(磁盘、内存等)//字符设备文件(c):指的是按单个字符读取数据的设备(鼠标、键盘等)//链接文件(l)
-exec //执行命令[root@stw ~]# find / -name ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@stw ~]# find / -name *ens33*
/proc/irq/19/ens33
/proc/sys/net/ipv4/conf/ens33
/proc/sys/net/ipv4/neigh/ens33
/proc/sys/net/ipv6/conf/ens33
/proc/sys/net/ipv6/neigh/ens33
/proc/1/task/1/net/dev_snmp6/ens33
/proc/1/net/dev_snmp6/ens33
/proc/2/task/2/net/dev_snmp6/ens33
......
/proc/60362/net/dev_snmp6/ens33
/proc/60371/task/60371/net/dev_snmp6/ens33
/proc/60371/net/dev_snmp6/ens33
/sys/devices/pci0000:00/0000:00:11.0/0000:02:01.0/net/ens33
/sys/class/net/ens33
/etc/sysconfig/network-scripts/ifcfg-ens33[root@stw ~]# find / -size +10M //查找"/"目录下大小超过10M的文件和目录
[root@stw ~]# find / -size +10M -size -20M //查找"/"目录下大小超过10M小于20M的文件和目录
[root@stw ~]# find / -user stw //查找stw用户的文件
find: ‘/proc/60505/task/60505/fd/5’: No such file or directory
find: ‘/proc/60505/task/60505/fdinfo/5’: No such file or directory
find: ‘/proc/60505/fd/6’: No such file or directory
find: ‘/proc/60505/fdinfo/6’: No such file or directory
/var/spool/mail/stw
/home/stw
/home/stw/.mozilla
/home/stw/.mozilla/extensions
/home/stw/.mozilla/plugins
/home/stw/.bash_logout
/home/stw/.bash_profile
/home/stw/.bashrc
- mount ----挂载–将分区挂载到目录下,从而将分区和目录联系起来,访问该目录就相当于访问分区了。
mount 【选项】 【设备/分区】【挂载点】选项可有可无-t 文件系统类型
[root@stw ~]# df -Th //查看挂载分区
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 3.5G 47G 7% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 163M 852M 17% /boot
tmpfs tmpfs 394M 32K 394M 1% /run/user/0
/dev/sr0 iso9660 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
[root@stw ~]# mount -t iso9660 /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@stw ~]# cd /mnt
[root@stw mnt]# ls
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
[root@stw mnt]# cd
[root@stw ~]# umount /mnt //取消挂载
- cat ------一次性显示文件所有内容
- more ------分页查看文件内容,显示查看内容所占文档的百分比 无法逐行向上查看
enter -----逐行向下查看
空格键 -----向下翻页
b -----向上翻页
q -----退出,显示已经查看过的文件内容 或者文件内容浏览完后自动退出
-
less ------ 分页查看文件内容 能够向上逐行查看
enter -----逐行向下查看
空格键 -----向下翻页
b -----向上翻页
q -----退出,但是不会显示已经查看过的文件内容 -
head ------默认查看文件内容的前10行
tail ------默认查看文件内容的后10行
[root@stw ~]# head /etc/passwd //默认查看/etc/passwd文件内容的前10行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@stw ~]# tail /etc/passwd //默认查看/etc/passwd文件内容的后10行
setroubleshoot:x:991:985::/var/lib/setroubleshoot:/sbin/nologin
saned:x:990:984:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
stw:x:1000:1000:stw:/home/stw:/bin/bash
[root@stw ~]# head -n 3 /etc/passwd //查看/etc/passwd文件内容的前3行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@stw ~]# tail -n 5 /etc/passwd 查看/etc/passwd文件内容的后5行
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
stw:x:1000:1000:stw:/home/stw:/bin/bash[root@stw ~]# head -n 20 /etc/passwd | tail -n 10 //查看文件/etc/passwd的第11-20行内容
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
- wc ------统计文件中的字节数、单词数、行数
-c 统计文件中的字节数
-w 统计文件中的单词数
-l 统计文件中的行数
- grep ------过滤、筛选
grep "字符串" //过滤出带有“字符串”的内容
grep -v "字符串" //过滤出不带有“字符串”的内容
grep ^"字符串" //过滤出以“ ”开头的内容
grep "字符串"$ //过滤出以“ ”结尾的内容
grep ^$ //过滤空行
grep -i //不区分大小写[root@stw ~]# grep "root" /etc/passwd //过滤出/etc/passwd下带有"root"的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@stw ~]# grep -v "nologin" /etc/passwd //过滤出/etc/passwd下不带有"nologin"的内容
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
stw:x:1000:1000:stw:/home/stw:/bin/bash
[root@stw ~]# grep ^o /etc/passwd //过滤出/etc/passwd下以"o"开头的内容
operator:x:11:0:operator:/root:/sbin/nologin
[root@stw ~]# grep h$ /etc/passwd //过滤出/etc/passwd下以"h"结尾的内容
root:x:0:0:root:/root:/bin/bash
stw:x:1000:1000:stw:/home/stw:/bin/bash
[root@stw ~]# grep ^$ /etc/passwd //过滤/etc/passwd中的空行//过滤出/tmp/passwd下以m开头的内容,然后把这些内容写入到file1文件中,并且文件中不能有空行
[root@stw ~]# grep ^m /tmp/passwd | grep -v ^$ > file1
-
gzip —压缩—.gz结尾
bzip2 —压缩—.bz2结尾
注意:gzip和bzip2都不支持对目录进行压缩或者解压
[root@stw tmp]# ls file111 passwd //通过gzip给文件压缩,自动生成一个.gz结尾的压缩文件,且源文件自动删除 [root@stw tmp]# gzip passwd [root@stw tmp]# ls file111 passwd.gz //通过gunzip解压压缩文件,解压后,压缩文件自动删除。 [root@stw tmp]# gunzip passwd.gz [root@stw tmp]# ls file111 passwd //通过bzip2给文件压缩,自动生成一个.bz2结尾的压缩文件,且源文件自动删除 [root@stw tmp]# bzip2 passwd [root@stw tmp]# ls file111 passwd.bz2 //通过bunzip2解压压缩文件,解压后,压缩文件自动删除。 [root@stw tmp]# bunzip2 passwd.bz2 [root@stw tmp]# ls file111 passwd
21.tar
压缩:
tar -czvf
tar -cjvf
解压:
tar -xzvf
tar -xjvf
-c //创建tar格式的包文件
-x //释放tar格式的包文件
-z //调用gzip程序进行压缩或者解压
-j //调用bzip2程序进行压缩或者解压
-v //显示压缩或者解压的过程
-f //归档
-C //解压到指定的目录下
[root@stw tmp]# ls
dir1 file111 passwd
[root@stw tmp]# tar -czvf dir1.tar.gz dir1
dir1/
[root@stw tmp]# ls
dir1 dir1.tar.gz file111 passwd
[root@stw tmp]# tar -cjvf dir1.tar.bz2 dir1
dir1/
[root@stw tmp]# ls
dir1 dir1.tar.bz2 dir1.tar.gz file111 passwd
[root@stw tmp]# mkdir /text
[root@stw tmp]# tar -xzvf dir1.tar.gz -C /text
dir1/
[root@stw tmp]# ls
dir1 dir1.tar.bz2 dir1.tar.gz file111 passwd
[root@stw tmp]# cd /text
[root@stw text]# ls
dir1
- date ----查看当前系统时间
date -s 20200916 修改当前系统时间为2020年9月16日0点
hwclock -w 将系统的时间同步到硬件中去
- history :列出历史使用命令
!历史命令的编号
!以最近使用什么开头的命令
!996 //执行第996这一条命令
!cd //执行最近使用以cd开头的命令