shell补-命令补缺-命令回顾(重要)

发布时间 2023-12-15 11:36:41作者: WeChat2834
shell补-命令补缺-命令回顾
  1. 命令行概述
  • 参数(parameter ) 选项(option)
  • 选项:命令的不同功能
  • 参数:把是什么东西传递给命令(目录/文件....)
命令 选项(optiongs) 参数parameter
ls -l /etc/hosts
sh -x /etc/init.d/network restart
  1. 在linux命令行下查看命令帮助
查询帮助 含义
man 查询linux大部分命令,配置文件的帮助
cmd --help 查询简单帮助
help cmd 查询内置命令帮助
info 查询命令详细帮助比man更新详细

man-manual手册

级别 含义
1 User Commands 用户命令帮助(也是默认级别,最常用)
2 System call系统调用
3 Libary calls 库文件调用
4 Special files 特殊而文件
5 File formats and conventions
6 Games
7 Miscellangeous
8 System adminstration commands
选项
-k 查找包含指定内容的man帮助
###man -k 过滤查询
[root@localhost ~]# man -k rsync
rsync (1)            - a fast, versatile, remote (and local) file-copying tool
rsyncd.conf (5)      - configuration file for rsync in daemon mode
[root@localhost ~]# man 5 rsyncd.conf

help cmd 显示内置命令

[root@localhost ~]# man cd

BASH_BUILTINS(1)                           General Commands Manual                                                                           BASH_BUILTINS(1)

NAME
####当man 命令的时候如果是显示BASH_BUILTINS(1) 则是内置命令
###查看内置命令
[root@localhost ~]# man bash


SHELL BUILTIN COMMANDS
       Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.  The :, true, false, and test builtins do not
       accept options and do not treat -- specially.  The exit, logout, break, continue, let, and shift builtins accept and process arguments beginning with - without requiring  --.   Other  builtins  that
       accept arguments but are not specified as accepting options interpret arguments beginning with - as invalid options and require -- to prevent this interpretation.
       : [arguments]
              No effect; the command does nothing beyond expanding arguments and performing any specified redirections.  A zero exit code is returned.

        .  filename [arguments
        .......................
        

Linux关机,重启,注销命令

重启 关机 注销
rebot halt logout
shutdown -r now/shutdown -r 0 shutdown -h now ctrl+d
poweroff t
init 6 init 0
systemctl reboot==reboot system halt/poweroff==poweroff

文件和目录操作命令

命令 含义
pwd 显示当前所在的位置
cd 切换目录
tree 以树形结构显示目录下的内容
mkdir 创建目录
ls 显示目录下的相关内容及像刚刚属性信息
cp 复制文件或目录
mv 移动或重命名文件
rm 删除文件或目录
rmdir 删除空目录
ln 硬链接与软链接
readlink 查看符号链接文件的内容
find 查找目录下的文件
xargs 将标准输入转换成命令行参数
rename 重命名文件
basename 显示文件名或目录名
dirname 显示文件或目录路径
chattr 改变文件的扩展属性
lsattr 查看文件扩展属性
file 显示文件类型
md5sum 计算和校验文件的MD5值
chown 改变文件或目录的用户和用户组
chmod 改变文件或目录权限
chgrp 更改文件用户组
umask 显示或设置权限掩码

pwd 显示当前所在的位置

选项 含义
-L logic逻辑,显示目录忽略软连接 (默认)
-P physical无力,显示五路目录不忽略软连接(及少使用)

cd 切换目录

选项
cd 活到当前用户家目录
-(减号) 回到上一次所在目录cd -== cd $OLDPWD
~ cd ~==cd
.. 回到上级目录

tree

选项
-d 只显示目录
-a 显示所有文件
-F 给不同类型文件加上标记 目录/
-L 指定最多显示几层
-f 显示完整路径
-i 不显示每行提示符与 -f 使用

mkdir 创建空目录

选项
-p 递归创建
-v 显示创建过程
[root@localhost tmp]# mkdir -pv  mytest/hello
mkdir: 已创建目录 "mytest"
mkdir: 已创建目录 "mytest/hello"
[root@localhost tmp]# ll

touch 修改时间戳

  • 目前用来创建文件

ls 显示目录下的内容及相关属性

选项 含义
-a 显示所有文件包含隐藏文件
-d 显示目录的本身信息,而不显示目录内容
--color 默认参数,不同类型文件加上颜色
--full-time(等同于-l --time-style=long-iso) 显示文件的详细时间
-h 以人类可读的形式显示大小
-i 显示文件或目录的inode
-r 逆序显示一般配合-t使用
-t 根据文件修改时间排序
#1.显示所有
[root@localhost tmp]# ls -lah
总用量 124K
drwxr-xr-x. 16 root root 4.0K 11月 27 16:04 .
dr-xr-x---. 17 root root 4.0K 11月 27 15:59 ..
#2.
[root@localhost tmp]# find ./  -type d|xargs ls -l注意之前没加xargs经常又问题
./:
总用量 116
-rw-r--r--. 1 root root   253 9月   5 15:48 !
drwxr-xr-x. 2 root root    24 11月 21 11:28 array
-rw-r--r--. 1 root root    27 9月  19 14:41 mytest
-rw-r--r--. 1 root root   400 9月   1 16:39 MyVar.sh

....
#3.只显示目录,-d一般结合着其他命令使用 ;注意之前没加xargs经常又问题
[root@localhost tmp]# find ./  -type d|xargs ls -ld
drwxr-xr-x. 16 root root 4096 11月 27 16:04 ./
drwxr-xr-x.  2 root root   24 11月 21 11:28 ./array
drwxr-xr-x.  2 root root   59 10月 30 14:46 ./color
drwxr-xr-x.  2 root root   77 10月 30 15:29 ./cycle
drwxr-xr-x.  2 root root   24 10月 11 17:25 ./hello
drwxr-xr-x.  2 root root   36 9月  26 18:08 ./Instal
drwxr-xr-x.  2 root root   48 11月 23 10:56 ./myfile
[root@localhost tmp]# 
#4.显示文件详细信息,时间统一格式
[root@localhost tmp]# ls -lah --full-time
总用量 124K
drwxr-xr-x. 16 root root 4.0K 2023-11-27 16:04:57.625863766 +0800 .
dr-xr-x---. 17 root root 4.0K 2023-11-27 15:59:39.133986830 +0800 ..
-rw-r--r--.  1 root root  253 2023-09-05 15:48:22.967150944 +0800 !
drwxr-xr-x.  2 root root   24 2023-11-21 11:28:33.529096171 +0800 array
-rw-r--r--.  1 root root   27 2023-09-19 14:41:24.506416024 +0800 calcualation2.sh
-
[root@localhost tmp]# ls -l --time-style=long-iso
总用量 116
-rw-r--r--. 1 root root   253 2023-09-05 15:48 !
drwxr-xr-x. 2 root root    24 2023-11-21 11:28 array
-rw-r--r--. 1 root root    27 2023-09-19 14:41 calcualation2.sh
-rw-r--r--. 1 root root  1886 2023-09-19 14:19 calcualation.sh
-rw-r--r--. 1 root root   970 2023-09-19 17:32 checked.sh
drwxr-xr-x. 2 root root    59 2023-10-30 14:46 color
d
#5.-h以人类可读的形势显示大小
[root@localhost tmp]# ls -lh
总用量 116K
calcualation2.sh
-rw-r--r--. 1 root root 1.9K 9月  19 14:19 
.......
#.-t根据文件修改时间
[root@localhost tmp]# ls -laht
总用量 124K
drwxr-xr-x. 16 root root 4.0K 11月 27 16:04 .
drwxr-xr-x.  3 root root   19 11月 27 16:04 mytest
dr-xr-x---. 17 root root 4.0K 11月 27 15:59 ..
drwxr-xr-x.  2 root root   48 11月 23 10:56 myfile
drwxr-xr-x.  2 root root   24 11月 21 11:28 array
drwxr-xr-x.  2 root root   30 11月 20 16:12 usr
#. -r逆序
[root@localhost tmp]# ls -lahtr
总用量 124K
-rwxr--r--.  1 root root  111 8月  30 15:52 myshell.sh
-rw-r--r--.  1 root root  400 9月   1 16:39 MyVar.sh
-rw-------.  1 root root  31K 9月   5 15:38 nohup.out
-rw-r--r--.  1 root root  253 9月   5 15:48 !
-rwxr-xr-x.  1 root root   78 9月  15 16:29 test01.sh

cp 复制文件或目录

选项
-a,-pdr 递归复制并保持文件属性不变
-r 递归复制目录
-d 复制软连接,不复制对应的源文件
-p 保持属性不变(权限,所有者,时间)
-t 调换目标与源的位置
#. 把着三文件复制到/tmp下、-t就是把目标和源文件调换;以下两个灯头
cp /etc/hosts   /etc/fstab  /etc/sysconfig/network    /tmp
cp -t /tmp /etc/hosts   /etc/fstab  /etc/sysconfig/network

MV :移动或重命名文件

  • 最危险的命令之一.
选项
-t 调换目标与源的位置

rm:删除文件或目录

选项
-r 递归删除
-f 强制删除

注意:删除文件,最好的是先备份,可以先把文件移动到一个指定目录就行

alias  rmf='mv -t /root/tmp/rmdirs/  $*'     ###防止误删
[root@localhost rmdirs]# rmf test02.sh 
[root@localhost rmdirs]# ls /root/tmp/rmdirs
test02.sh
[root@localhost rmdirs]# 


rmdir删除空目录

ln 硬连接与软连接

选项
默认创建硬连接
-s 创建软连接

软链接,硬链接的区别:

readlink:查看符号链接的文件的内容

find:查找目录下的文件

选项
-type 类型f d
-name 指定命令
-size 指定大小 +100k -10M
-mtime 修改时间 +7 7天之前
-mmin 修改时间(是分钟) -5 5分钟内修改过的
-iname 不区分文件大小写
-maxdepth 指定最大深度
-inum 根据inode号码查找文件,(一般删除文件名乱码的文件)
-print0 在每个文件结尾上加个标记(空标记)一般与|xargs -0结合使用
能处理一些因特殊符号查找不出结果的情况
-perm 指定权限/u+r,/a+w,/777
[root@localhost test]# ll
-rw-r--r--. 1 root root 0 11月 27 17:37 hellowr011.txt
-rw-r--r--. 1 root root 0 11月 27 17:35 hellowr012
-rw-r--r--. 1 root root 0 11月 27 17:37 hellowr012.txt
-rw-r--r--. 1 root root 0 11月 27 17:34 hellowr01 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 hellowr02 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 hellowr03 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 hellowr04 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 hellowr05 .txt
[root@localhost test]# find ./ -name "*.txt"|xargs ls  -l      ####处理加了特殊符号的文件是容易出现问题
ls: 无法访问./hellowr01: 没有那个文件或目录
ls: 无法访问.txt: 没有那个文件或目录
ls: 无法访问./hellowr02: 没有那个文件或目录
ls: 无法访问.txt: 没有那个文件或目录
ls: 无法访问./hellowr03: 没有那个文件或目录
ls: 无法访问.txt: 没有那个文件或目录
ls: 无法访问./hellowr04: 没有那个文件或目录
ls: 无法访问.txt: 没有那个文件或目录
ls: 无法访问./hellowr05: 没有那个文件或目录
ls: 无法访问.txt: 没有那个文件或目录
-rw-r--r--. 1 root root 0 11月 27 17:37 ./hellowr011.txt
-rw-r--r--. 1 root root 0 11月 27 17:37 ./hellowr012.txt
[root@localhost test]# find ./ -name "*.txt" -print0
./hellowr01 .txt./hellowr02 .txt./hellowr03 .txt./hellowr04 .txt./hellowr05 .txt./hellowr011.txt./hellowr012.txt[root@localhost test]# find ./ -name "*.txt" -print0|xargs -0  ls -l
-rw-r--r--. 1 root root 0 11月 27 17:37 ./hellowr011.txt
-rw-r--r--. 1 root root 0 11月 27 17:37 ./hellowr012.txt
-rw-r--r--. 1 root root 0 11月 27 17:34 ./hellowr01 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 ./hellowr02 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 ./hellowr03 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 ./hellowr04 .txt
-rw-r--r--. 1 root root 0 11月 27 17:34 ./hellowr05 .txt
###前面-print0给每个文件结束加上空标记看起来是一行。后面跟 xargs -0接收这些标记取出这些数据

xargs 将标准输入转换为命令行参数

  • 应用场景:配合|xargs
  • 管道符与|xargs的区别:前者传递的仅仅是字符,后者传递的是行参数
选项
-n 指定xargs 一次传递几个参数
-0 与find -print0的配合使用。
[root@localhost tmp]# seq  1 7
1
2
3
4
5
6
7
[root@localhost tmp]# seq  1 7 |xargs
1 2 3 4 5 6 7
[root@localhost tmp]# seq  1 7 |xargs  -n2
1 2
3 4
5 6
7
[root@localhost tmp]# 
[root@localhost tmp]# seq 10 12|touch  
touch: 缺少了文件操作数
Try 'touch --help' for more information.
[root@localhost tmp]# seq 10 12|xargs touch    #####成功创建10到12 的文件
[root@localhost tmp]#