3——ls-wc-管道符号-alias-路径-touch-通配符

一、ls

  1. 👍-l 以长格式显示
  2. -d 先是目录本身的属性
  3. -t 按文件修改时间进行排序
  4. -r 将目录的内容清单以英文字母顺序的逆序显示
  5. 👍-a 显示所有子目录和文件信息,包括隐藏文件
  6. 👍-A 类似于"-a",但不显示".“和”…"目录的信息
  7. 👍-h 以更易读的字节单位(K->M->G->T->P->E)显示信息【必须要和-l一起使用】 human readable
    【注】ll -h = ls -l -h 只能看文件的大小,无法显示文件夹大小
  8. -R 递归显示内容
[root@192 lianxi]# ls
hunan
[root@192 lianxi]# mkdir .niu120
[root@192 lianxi]# ls
hunan
[root@192 lianxi]# ls -a
.  ..  hunan  .niu120
[root@192 lianxi]# ls -A
hunan  .niu120
[root@192 boot]# ls -l -h
总用量 110M
-rw-r--r--. 1 root root 150K 10月 20 2020 config-3.10.0-1160.el7.x86_64
drwxr-xr-x. 3 root root   17 12月  6 17:10 efi
drwxr-xr-x. 2 root root   27 12月  6 17:10 grub
drwx------. 5 root root   97 12月  6 17:12 grub2
-rw-------. 1 root root  60M 12月  6 17:11 initramfs-0-rescue-66ed8c3df27f4c9c94ccdf20cdeac658.img
-rw-------. 1 root root  21M 12月  6 17:12 initramfs-3.10.0-1160.el7.x86_64.img
-rw-------. 1 root root  14M 12月  6 17:13 initramfs-3.10.0-1160.el7.x86_64kdump.img
-rw-r--r--. 1 root root 314K 10月 20 2020 symvers-3.10.0-1160.el7.x86_64.gz
-rw-------. 1 root root 3.5M 10月 20 2020 System.map-3.10.0-1160.el7.x86_64
-rwxr-xr-x. 1 root root 6.5M 12月  6 17:11 vmlinuz-0-rescue-66ed8c3df27f4c9c94ccdf20cdeac658
-rwxr-xr-x. 1 root root 6.5M 10月 20 2020 vmlinuz-3.10.0-1160.el7.x86_64

【拓展】du:统计文件或文件夹大小

  1. -s,–summarize 汇总
  2. -h,–human-readable 人类能识别的大小格式
    <一般s和h一起使用>

二、| 管道符号

将前面命令的输出送给后面的命令作为输入(借花献佛)

2.1管道

是实现进程和进程之间通信的

2.2wc

统计命令:统计行,单词,字节

  1. -w:
    -w, --words
    print the word counts
  2. -l:
    -l, --lines
    print the newline counts
[root@192 lianxi]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@192 lianxi]# wc /etc/hosts
  2  10 158 /etc/hosts
  # 2行 10个单词 158字节

三、alias

查看命令的别名
别名=快捷键 可以起到加速作用

3.1临时定义别名

临时定义lk是ls -l -a --color=auto的别名
[root@192 lianxi]# alias lk='ls -l -a --color=auto'
[root@192 lianxi]# lk
总用量 0
drwxr-xr-x.  4 root root  34 1月  20 12:12 .
dr-xr-xr-x. 18 root root 238 1月   5 20:33 ..
drwxr-xr-x.  3 root root  22 1月   5 21:43 hunan
drwxr-xr-x.  2 root root   6 1月  20 12:12 .niu120

3.2别名不能与原来的linux系统里的命令冲突,如果冲突会导致原来的命令不能使用

unalias:取消定义的别名
**【示例】**故意定义mkdir是ls的别名

[root@192 lianxi]# alias mkdir='ls'
[root@192 lianxi]# mkdir fzt
ls: 无法访问fzt: 没有那个文件或目录
[root@192 lianxi]# mkdir
hunan
[root@192 lianxi]# unalias mkdir

【为什么】发生冲突时,原来的命令不能用

因为在解析时,会优先解析别名

[root@192 lianxi]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

3.3shell解析过程

在这里插入图片描述
关键字: 各种编程语言里预留的已经给本语言使用的名字 key word

3.4定义永久别名

3.4.1名词解释

  1. vim 是linux里记事本软件,用来新建和修改文件
  2. 脚本 是linux里存放命令的文件

3.4.2操作步骤(写入文件)

======
==1. [root@192 lianxi]# vim /root/.bashrc
==2. 按i进入输入模式,开始输入内容 insert
==3. 输入内容
==4. 按ESC键,离开输入模式 escape 离开,逃离
==5. 输入:wq 退出并保存 write quit
==6. [root@192 lianxi]# source /root/.bashrc 执行/root/.bashrc脚本

【为什么】source和bash执行脚本的区别?

==bash [root@192 lianxi]# bash /root/.bashrc
产生一个子进程bash去执行脚本

==source [root@192 lianxi]# source /root/.bashrc
在当前bash进程里执行脚本
示例:

[root@192 lianxi]# sg="zhangjian"
[root@192 lianxi]# mn="shimengmeng"
[root@192 lianxi]# echo $sg $mn
zhangjian shimengmeng
[root@192 lianxi]# vim output.sh
[root@192 lianxi]# cat output.sh
#!/bin/bash      ==》申明这个脚本默认使用/bin/bash这个解释器去执行

echo $sg
echo $mn
city='changsha'
echo "$city is a good place"
[root@192 lianxi]# source output.sh
zhangjian
shimengmeng
changsha is a good place
[root@192 lianxi]# bash output.sh


changsha is a good place

bash是产生一个子进程去执行脚本,但sg和mn在子进程里没有申明,即没有这两个变量,故输出空值
export: 输出变量为全局变量

将sg和mn输出为全局变量,这样子进程就可以使用
[root@192 lianxi]# export sg mn
[root@192 lianxi]# bash output.sh
zhangjian
shimengmeng
changsha is a good place

【补充】权限

[root@192 lianxi]# ./output.sh    ==》也是执行当前目录下的output.sh
-bash: ./output.sh: Permission denied  ==》权限被拒绝
[root@192 lianxi]# chmod +x output.sh  ==》授予可执行权限
[root@192 lianxi]# ./output.sh  ==》执行当前目录下的output.sh
zhangjian
shimengmeng
changsha is a good place
[root@192 lianxi]# chmod -x output.sh  ==》去除可执行权限

rwxr-xr-x 代表权限 r read 读 w write 写 x execute 执行
执行:可以理解为运行这个文件里的命令
分三类:user group others
user ==>u
group ==>g
others ==>o
all ==>a

-rwxr-xr-x. 1 root root 78 1月 20 19:57 output.sh
此时第一个root对应user,第二个root对应group


[root@192 lianxi]# chmod u+x output.sh
[root@192 lianxi]# ll output.sh
-rwxr--r--. 1 root root 78 1月  20 19:57 output.sh

[root@192 lianxi]# chmod a+x output.sh
[root@192 lianxi]# chmod g+x output.sh
[root@192 lianxi]# chmod o+x output.sh

四、常用命令选项

文件类型缩写应用
常规文件-保存数据
目录d存放文件
符号链接l指向其他文件
字符设备节点c访问设备
块设备节点b访问设备

五、文件类型

drwxr-xr-x. 3 root root 22 1月 5 21:43 hunan
-rwxr-xr-x. 1 root root 78 1月 20 19:57 output.sh

  • -代表普通文件
  • d 代表文件夹
  • l 代表链接文件 link

5.1 ln 创建链接文件

-s 创建符号链接(软连接) ==》相当于Windows的快捷方式
删除源文件会导致链接文件不可用

[root@192 lianxi]# mkdir guangdong
[root@192 lianxi]# ls
guangdong  hunan  output.sh  yue
[root@192 lianxi]# ln -s guangdong yue   创建链接文件yue指向guangdong   访问yue等同于访问guangdong
[root@192 lianxi]# ll
总用量 4
drwxr-xr-x. 2 root root 23 1月  20 20:43 guangdong
drwxr-xr-x. 3 root root 22 1月   5 21:43 hunan
-rwxr-xr-x. 1 root root 78 1月  20 19:57 output.sh
lrwxrwxrwx. 1 root root 10 1月  20 20:38 yue -> guangdong/
[root@192 lianxi]# ls
guangdong  hunan  output.sh  yue
[root@192 lianxi]# cd yue
[root@192 yue]# mkdir guangzhou
[root@192 yue]# ls
guangzhou
[root@192 yue]# cd ..
[root@192 lianxi]# ls
guangdong  hunan  output.sh  yue
[root@192 lianxi]# cd guangdong/
[root@192 guangdong]# ls
guangzhou
[root@192 guangdong]#

六、相对路径和绝对路径

6.1 Linux文件系统层次结构

在这里插入图片描述

6.2绝对路径

从/开始,一层一层往下走,绝对不会出错的路径,与当前在哪里无关
在这里插入图片描述
命令 + 路径 mkdir /china/hunan/changsha/nongda


[root@192 china]# cd /china/hunan/changsha/nongda/
[root@192 nongda]# pwd
/china/hunan/changsha/nongda
[root@192 nongda]# cd /china/hubei/wuhan/
[root@192 wuhan]# pwd
/china/hubei/wuhan

6.2 相对路径

不是/开头的路径,以当前文件夹作为参照物,可进可退

七、touch

两个作用:
1.新建空文件
2.touch一个已存在的文件,更新文件的创建时间

【date】查看当前的时间


[root@192 hunan]# date
2022年 01月 22日 星期六 17:10:18 CST
[root@192 hunan]# touch sc.txt
[root@192 hunan]# ll
总用量 0
-rw-r--r--. 1 root root  0 1月  22 17:08 cao.txt
drwxr-xr-x. 3 root root 20 1月  22 16:55 changsha
-rw-r--r--. 1 root root  0 1月  22 17:10 sc.txt
drwxr-xr-x. 2 root root  6 1月  22 17:09 yueyang
[root@192 hunan]# touch sc.txt
[root@192 hunan]# ll
总用量 0
-rw-r--r--. 1 root root  0 1月  22 17:08 cao.txt
drwxr-xr-x. 3 root root 20 1月  22 16:55 changsha
-rw-r--r--. 1 root root  0 1月  22 17:11 sc.txt
drwxr-xr-x. 2 root root  6 1月  22 17:09 yueyang

八、rm命令

  1. -rf 强制删除文件夹或文件,不给予提醒,不会在回收站里存放
  2. -r 递归删除整个目录树(株连九族的感觉)
  3. -f 强制删除文件或目录,不进行提醒

❗【】rm -rf / 会删除根目录,后果非常严重。新系统(centos7/8/9)不能执行,但旧系统可以(centos6)

九、通配符

*:可以表示任意个任意字符
?:表示一个任意字符
【rm -rf *】删除当前目录下所有文件和文件夹,但无法删除隐藏文件和文件夹。如果想要删除隐藏文件或文件夹,使用 rm -rf .或rm -rf.文件名 进行删除
【禁止使用
cd /
rm -rf *
和rm -rf /
。会把根下所有内容全部删除,只能重装系统】


[root@192 /]# cd lianxi
[root@192 lianxi]# ls
guangdong  hunan  output.sh  yue
[root@192 lianxi]# rm -rf *
[root@192 lianxi]# ls
[root@192 lianxi]# mkdir .ying liu .xiong
[root@192 lianxi]# ls
liu
[root@192 lianxi]# ls -a
.  ..  liu  .niu120  .xiong  .ying
[root@192 lianxi]# rm -rf *
[root@192 lianxi]# ls -a
.  ..  .niu120  .xiong  .ying
[root@192 lianxi]# rm -rf .ying
[root@192 lianxi]# ls -a
.  ..  .niu120  .xiong

【拓展1】{1…10}表示从1到10的集合

[root@192 lianxi]# touch liuhong{1..10}.txt
[root@192 lianxi]# ls
liuhong10.txt  liuhong2.txt  liuhong4.txt  liuhong6.txt  liuhong8.txt
liuhong1.txt   liuhong3.txt  liuhong5.txt  liuhong7.txt  liuhong9.txt
[root@192 lianxi]# touch fanming{1..10}.txt
[root@192 lianxi]# ls
fanming10.txt  fanming3.txt  fanming6.txt  fanming9.txt   liuhong2.txt  liuhong5.txt  liuhong8.txt
fanming1.txt   fanming4.txt  fanming7.txt  liuhong10.txt  liuhong3.txt  liuhong6.txt  liuhong9.txt
fanming2.txt   fanming5.txt  fanming8.txt  liuhong1.txt   liuhong4.txt  liuhong7.txt
# 仅删除liuhong的相关文件

[root@192 lianxi]# rm -rf liuhong*
[root@192 lianxi]# ls
fanming10.txt  fanming2.txt  fanming4.txt  fanming6.txt  fanming8.txt
fanming1.txt   fanming3.txt  fanming5.txt  fanming7.txt  fanming9.txt
[root@192 lianxi]# rm -rf fanming??.txt
[root@192 lianxi]# ls
fanming1.txt  fanming3.txt  fanming5.txt  fanming7.txt  fanming9.txt
fanming2.txt  fanming4.txt  fanming6.txt  fanming8.txt

【拓展2】read 接受用户的输入,并复制给变量

-p 只是给于提醒的作用

[root@192 lianxi]# read -p"请输入文件名:" name
请输入文件名:feng
[root@192 lianxi]# echo $name
feng
[root@192 lianxi]# read name1 name2
zhao zhou  ==>本行要自己输入,因为没有-p,所以没有任何提示
# 可以一次性赋值给两个变量
[root@192 lianxi]# echo $name1 $name2
zhao zhou
[root@192 lianxi]#

十、小练习

=》 编写一个脚本实现新建100个指定前缀的文件和100个文件夹
命令提示:read mkdir touch

# 创建文件
[root@192 lianxi]# vim create_file.sh
[root@192 lianxi]# cat create_file.sh
#!bin/bash

read -p"请输入文件名前缀:" prefix_name

#创建100个文件
touch ${prefix_name}{1..100}.txt
#创建100个文件夹
mkdir ${prefix_name}{1..100}

echo "完成新建100个文件和文件夹"

 #执行脚本
[root@192 lianxi]# bash create_file.sh
请输入文件名前缀:lihua

【补充】用{}避免变量混淆

[root@192 lianxi]# sg="lihua"
[root@192 lianxi]# echo $sg1  ==>系统误将sg1识别成了一个变量名

 #用{}将变量名括起来,以免混淆
[root@192 lianxi]# echo ${sg}1
lihua1

==》编写一个脚本rm_rf.file.sh实现删除指定前缀的文件和文件夹

[root@192 lianxi]# vim rm_file.sh
[root@192 lianxi]# cat rm_file.sh
#!bin/bash

read -p"请输入文件名前缀:" prefile_name

#删除文件和文件夹
rm -rf ${prefile_name}*

echo "完成删除指定前缀文件和文件夹"
[root@192 lianxi]# bash rm_file.sh

请输入文件名前缀:lihua

==》
1.临时定义cl是clear的别名
2.永久定义kk是mkdir -p的别名(提示:需要修改文件/root/.bashrc
3.查看当前的系统的时间
4.查看/var目录的大小
5.使用绝对路径进入/var/log/目录
6.使用绝对路径进入/etc/ssh目录,使用相对路径进入/var/log目录
7.使用绝对路径新建/wulin/wudang/zhangwuji
8.在/wulin目录下新建wudang的链接文件叫wd
9.统计/var/log目录下有多少个文件和文件夹
10.给rm_rf.file.sh授予可执行权限
11.定义2个变量sg=“zhangwuji” mn=“zhaomin”,输出为全局变量
12.尝试用source或者bash执行rm_rf.file.sh脚本

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值