Linux基础命令(三)

1.Linux文件命名规则

  • 长度不能超过255个字符
  • 不能使用/当文件名
  • *严格区分大小写

2.文本查找——grep

语法:grep [option…] ‘关键字’ file
根据该模式搜索文本并显示与该模式匹配的文本

[En]

Search for text according to the pattern and display the text that matches the pattern

[root@zzd ~]# grep zzd greptest         //过滤greptest文件里zzd关键字
hello zzd

-i 忽略大小写

[root@zzd ~]# grep -i zzd greptest      //过滤greptes文件里zzd关键字,忽略大小写
hello zzd
heLLo Zzd

–color 将匹配到的内容高亮显示(color命令默认的命令别名带有此选项)

[root@zzd ~]# which grep        //查看命令grep的绝对路径
alias grep='grep --color=auto'
    /usr/bin/grep

-v 显示没有被模式匹配到的行

[root@zzd ~]# grep -v zzd greptest      //过滤greptes文件里没有zzd关键字的行
Hello Word!

Hello ZIC
Hello zic
Hello tgg
heLLo Zzd

-o 只显示被匹配到的字符串

[root@zzd ~]# grep -o zic greptest      //只显示匹配到的字符串zic
zic

-E 使用正则表达式。相当于egrep

[root@zzd ~]# grep -E 'hello|zic' greptest      //使用正则表达式匹配有hello和zic的行
hello zzd
Hello zic

[root@zzd ~]# grep 'hello\|zic' greptest        //也是正则表达式的用法,不加-E
hello zzd
Hello zic

-q 静默输出,不输出任何信息

[root@zzd ~]# grep  Hello greptest
Hello Word!

Hello ZIC
Hello zic
Hello tgg
[root@zzd ~]# grep -q Hello greptest        //静默输出,一般用于shell脚本语言
[root@zzd ~]#

-A 1 显示被匹配到的内容以及前一行,如果把1换成2,就表示前两行

[root@zzd ~]# grep -A 1 tgg greptest        //过滤关键字tgg,并显示其以及其后一行
Hello tgg
heLLo Zzd

-B 1 显示被匹配的内容以及后一行,如果把1换成2,就表示后两行

[root@zzd ~]# grep -B 1 tgg greptest        //过滤关键字tgg,并显示其以及其前一行
Hello zic
Hello tgg

-C 1 显示被匹配到的内容以及前后各一行,如果把1换成2则表示前后各两行

[root@zzd ~]# grep -C 1 tgg greptest        //过滤关键字tgg,并显示其以及其前后各一行
Hello zic
Hello tgg
heLLo Zzd

fgrep 不支持正则表达式,执行速度快,但一般很少用到

egrep 使用正则表达式,和grep -E一个效果

3.文件查找——find

实时搜索,准确率高,遍历指定目录下的所有文件,完成搜索

[En]

Real-time search with high accuracy, traversing all the files in the specified directory to complete the search

语法: find [选项…] 查找路径 查找标准 查找到以后的处理动作
查找路径:默认为当前目录

[En]

Find path: default to current directory

查找条件:默认为指定路径下的所有文件

[En]

Find criteria: default to all files under the specified path

-name ‘filename’ 对文件名做精确查找,支持通配符

[root@zzd ~]# find -name greptest       //在当前目录查找文件名为greptest的文件
./greptest

-iname ‘filename’ 文件名匹配时不区分大小写

[root@zzd ~]# find -iname abc       //在当前目录查找文件名为abc的目录,忽略大小写
./ABC
./abc
./a/ABC
./a/Abc
./a/ABc
./a/abc
[root@zzd ~]# find ./a -iname 'ab*'     //使用通配符查找/root/a目录下以ab开头的文件
./a/ABC
./a/Abc
./a/ABc
./a/abc

-user username 根据属主来查找

[root@zzd ~]# useradd zzd       //创建一个普通用户zzd
[root@zzd ~]# tail -1 /etc/passwd
zzd:x:1001:1001::/home/zzd:/bin/bash        #UID和GID都为1001
[root@zzd ~]# chown zzd:zzd greptest        //修改文件greptest的属主和属组都为zzd
[root@zzd ~]# ll greptest
-rw-r--r--. 1 zzd zzd 62 Jul  4 22:57 greptest
[root@zzd ~]# find -user zzd        //查找属主为zzd的文件
./greptest

-group groupname 根据属组来查找

[root@zzd ~]# find -group zzd       //查找属组为zzd的文件
./greptest

-uid 根据UID进行查找,当用户被删除后文件的属主会变成此用户的UID

[root@zzd ~]# find -uid 1001        //查找属主uid为1001的文件
./greptest

-gid 根据GIDj进行查找,当用户或组被删除后文件的属组会变成此用户或组的GID

[root@zzd ~]# find -gid 1001        //查找属组gid为1001的文件
./greptest

-nouser 查找没有属主的文件,用户被删除的情况下产生的文件,只有uid没有属主

[root@zzd ~]# userdel -r zzd        //删除用户zzd
[root@zzd ~]# find -nouser          //查找只有uid没有属主的文件
./greptest

-nogroup 查找没有属组的文件,组被删除的情况下产生的文件,只有gid没有属组

[root@zzd ~]# find -nogroup         //查找只有gid没有属组的文件
./greptest

-type 根据文件类型来查找(f , d , c , l , p , s)

[root@zzd ~]# find -type f      //查找当前目录下所有文件
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./002
./qwer
./ABC
./.viminfo
./greptest
./.bashrc
./anaconda-ks.cfg
./passwd
./abc
./.lesshst
./a/ABC
./a/Abc
./a/ABc
./a/abc

-size 根据文件大小来查找,如1k,1M,+10k,+10M,-1k,-1M

[root@zzd ~]# find / -size 15M          //查找根目录下大小为15M的文件
find: ‘/proc/1465/task/1465/fd/6’: No such file or directory
find: ‘/proc/1465/task/1465/fdinfo/6’: No such file or directory
find: ‘/proc/1465/fd/5’: No such file or directory
find: ‘/proc/1465/fdinfo/5’: No such file or directory
/mnt/AppStream/Packages/bcc-0.16.0-1.el8.x86_64.rpm
/mnt/AppStream/Packages/gimp-2.8.22-15.module_el8.0.0+36+bb6a76a2.x86_64.rpm
/mnt/AppStream/Packages/golang-race-1.15.2-1.module_el8.4.0+546+c69f460d.x86_64.rpm
/mnt/AppStream/Packages/libkkc-data-0.2.7-12.el8.x86_64.rpm
/mnt/AppStream/Packages/python2-scipy-1.0.0-20.module_el8.4.0+543+a968257c.x86_64.rpm
/mnt/AppStream/Packages/python3-scipy-1.0.0-20.module_el8.3.0+389+6a62c88d.x86_64.rpm
/mnt/AppStream/Packages/texlive-lm-20180414-19.el8.noarch.rpm
/mnt/AppStream/Packages/texlive-oberdiek-20180414-19.el8.noarch.rpm
/mnt/AppStream/Packages/wireshark-cli-2.6.2-12.el8.i686.rpm

[root@zzd ~]# find /etc/ -size -1M      //查找/etc目录下大小1M及以内的文件,+1M则是大于1M的文件
/etc/crypttab
/etc/security/opasswd
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/environment
/etc/exports
/etc/motd
/etc/sysconfig/run-parts
/etc/cron.deny
/etc/kernel/install.d/20-grubby.install
/etc/kernel/install.d/90-loaderentry.install
/etc/selinux/targeted/contexts/files/file_contexts.local
/etc/selinux/targeted/contexts/files/file_contexts.subs
/etc/.pwd.lock
/etc/tuned/post_loaded_profile

-mtime 根据修改时间来进行查找(单位:天)
ctime 根据改变时间来进行查找(单位:天)
atime 根据访问时间进行查找单(位:天)
+5 五天前
-5 五天以内

[root@zzd ~]# find -mtime -5        //查找当前目录下修改时间在五天以内的文件或目录
.
./.bash_history
./002
./qwer
./ABC
./.viminfo
./greptest
./.bashrc
./anaconda-ks.cfg
./passwd
./abc
./.lesshst
./test
./test/test1
./test/test2
./a
./a/ABC
./a/Abc
./a/ABc
./a/abc

[root@zzd ~]# find -mtime +5        //查找当前目录下修改时间在五天前的文件或目录
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc

-mmin 根据修改时间来进行查找(单位:分钟)
cmin 根据改变时间来进行查找(单位:分钟)
amin 根据访问时间进行查找单(位:分钟)
+5 五分钟前
-5 五分钟以内

[root@zzd ~]# touch passwd
[root@zzd ~]# touch anaconda-ks.cfg
[root@zzd ~]# touch greptest
#对以上三个文件进行更新

[root@zzd ~]# find -mmin -5     //查找当前目录下修改时间在5分钟以内的文件货目录
./greptest
./anaconda-ks.cfg
./passwd

-perm mode 根据权限精确查找
-perm -mode 文件权限能完全包含此mode是才符合条件
-perm /mode 9位权限中有任何一位权限匹配都视为符合查找条件

[root@zzd ~]# find -perm 777        //查找当前目录下权限为777的文件或目录
./date.sh

[root@zzd ~]# find -perm -755       //查找当前目录下权限能够完全包含755的文件或目录
./test
./test/test1
./test/test2
./a
./date.sh

[root@zzd ~]# find /root/a -perm /777   //查找/root/a目录下9为权限权限中有一位符合777九位权限中一位的文件或目录
/root/a
/root/a/ABC
/root/a/Abc
/root/a/ABc
/root/a/abc

-print 显示

[root@zzd ~]# find -name greptest -print        //在当前目录查找文件名为greptest的文件,并显示
./greptest

-ls 类似于ls -l的形式显示每一个查找到的文件的详细信息

[root@zzd ~]# find -name greptest -ls       //在当前目录查找文件名为greptest的文件,并以长格式显示文件详细信息
 33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest

-delete 删除查找到的文件

[root@zzd ~]# ls
002  a  abc  ABC  anaconda-ks.cfg  date.sh  greptest  passwd  qwer  test
[root@zzd ~]# find -name 002 -delete        //在当前目录查找文件名为002的文件并删除
[root@zzd ~]# ls
a  abc  ABC  anaconda-ks.cfg  date.sh  greptest  passwd  qwer  test

-fls 路径 查找到的文件以长格式保存至指定文件

[root@zzd ~]# find -name greptest -fls /root/findfls    //在当前目录查找文件名为greptest的文件,并以长格式保存至/root/findfls文件
[root@zzd ~]# cat findfls
 33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest

-ok 命令 {} ; 对查找到的文件执行命令,每次操作需要用户确认*

[root@zzd ~]# find -name greptest -ok rm -rf {} \;      //在当前目录查找文件名为greptest的文件并执行删除命令
< rm ... ./greptest > ? n       //&#x9700;&#x8981;&#x7528;&#x6237;&#x786E;&#x8BA4;&#xFF0C;y&#x4E3A;&#x662F;&#xFF0C;n&#x4E3A;&#x5426;

-exec 命令 {} ; 对查找到的文件执行命令。不需要用户确认

[root@zzd ~]# find -name greptest -exec rm -rf {} \;        //&#x5728;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x67E5;&#x627E;&#x6587;&#x4EF6;&#x540D;&#x4E3A;greptest&#x7684;&#x6587;&#x4EF6;&#x5E76;&#x6267;&#x884C;&#x5220;&#x9664;&#x547D;&#x4EE4;
[root@zzd ~]# ls
a  abc  ABC  anaconda-ks.cfg  date.sh  findfls  passwd  qwer  test

xargs 通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可。因为有些命令不能接收过多的参数,所以需要用到xargs

[root@zzd ~]# ls /opt/
abc  Abc  ABc  ABC  anaconda-ks.cfg  date.sh  findfls  passwd  qwer
[root@zzd ~]#
[root@zzd ~]# find /opt -type f | xargs rm -rf      //&#x67E5;&#x627E;/opt&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;,&#x7136;&#x540E;&#x5220;&#x9664;
[root@zzd ~]# ls /opt/
[root@zzd ~]#

-a 必须同时满足所有条件,才符合查找标准

//&#x62E5;&#x6709;&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9ED8;&#x8BA4;&#x5C31;&#x662F;&#x540C;&#x65F6;&#x6EE1;&#x8DB3;
[root@zzd ~]# tree /opt/
/opt/
&#x251C;&#x2500;&#x2500; a
&#x2502;   &#x2514;&#x2500;&#x2500; abc
&#x2514;&#x2500;&#x2500; abc

2 directories, 1 file

[root@zzd ~]# find /opt -name abc -type f       //&#x67E5;&#x627E;/opt&#x76EE;&#x5F55;&#x4E0B;&#x7C7B;&#x578B;&#x4E3A;&#x6587;&#x4EF6;&#x4E14;&#x6587;&#x4EF6;&#x540D;&#x4E3A;abc&#x7684;&#x6587;&#x4EF6;
/opt/abc

[root@zzd ~]# find /opt -name abc -a -type f
/opt/abc

-o 只要满足其中一个条件就符合查找标准

[root@zzd ~]# find /opt -name abc -o -type f        //&#x67E5;&#x627E;/opt&#x76EE;&#x5F55;&#x4E0B;&#x540D;&#x4E3A;abc&#x6216;&#x7C7B;&#x578B;&#x4E3A;f&#x7684;&#x6587;&#x4EF6;&#x6216;&#x76EE;&#x5F55;
/opt/.bash_logout
/opt/.bash_profile
/opt/.cshrc
/opt/.tcshrc
/opt/.bash_history
/opt/.viminfo
/opt/.bashrc
/opt/.lesshst
/opt/abc
/opt/a/abc

4. 文件层级系统

FHS     //文件层级系统
/       //可以单独分区,LVM分区
/boot       //系统启动相关的文件,如内核(vmlinuz)、initrd(initramfs),\
            //以及grub(bootloader)。建议单独分区,基本分区
/dev        //设备文件。不能单独分区
    设备文件    //关联至一个设备驱动程序,进而能够与之对应硬件设备进行通信
        块设备     //随机访问,数据块(比如硬盘)
        字符设备    //也叫线性设备,线性访问,按字符为单位(比如鼠标、显示器)
        设备号     //主设备号(major)和次设备号(minor)
            主设备号标识设备类型
            次设备号标识同一类型下的不同设备
    设备文件只有元数据,没有数据
/etc        //配置文件
/home       //普通用户的家目录,每一个用户的家目录通常默认为/home/USERNAME。 \
            //建议单独分区
/root       //管理员的家目录。不该单独分区
/lib        //库文件
    静态库     //.a
    动态库     //.dll,.so(shared object)
    /lib/modules    //内核模块文件
/media      //挂载点目录,通常用来挂载移动设备
/mnt        //挂载点目录,通常用来挂载额外的临时文件系统,比如另一块硬盘
/opt        //可选目录,早期通常用来安装第三方程序
/proc       //伪文件系统,内核映射文件(伪文件系统实际上里面是没有任何内容的, \
            //开机之后才映射上去的)。不能单独分区
/sys        //伪文件系统,跟硬件设备相关的属性映射文件(伪文件系统实际上里面是没有 \
            //任何内容的,开机之后才映射上去的)。不能单独分区
/tmp        //临时文件,/var/tmp
/var        //可变化的文件,比如log、cache。存放日志信息、pid文件、lock文件,\
            //建议单独分区
/bin        //可执行文件,用户命令
/sbin       //管理命令
/usr        //shared,read-only,全局共享只读文件。提供操作系统核心功能,可以单独分区
    /usr/bin
    /usr/sbin
    /usr/lib
/usr/local      //第三方软件安装路径
    /usr/local/bin
    /usr/local/sbin
    /usr/local/lib
    /usr/local/etc
    /usr/local/man

/etc,/bin,/sbin,/lib内是系统启动就需要用到的程序,这些目录不能挂载额外的分区,\
必须位于根文件系统的分区上<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Must be on the partition of the root file system</font>*</details>

/usr/bin,/usr/sbin,/usr/lib提供操作系统核心功能,/usr可以单独分区

/usr/local/bin,/usr/local/sbin,/usr/local/lib,/usr/local/etc, \
/usr/local/man等等在/usr/local目录下的内容都是第三方软件,建议单独分区

5.重定向与管道服

默认输入设备:标准输入,stdin, 0 (键盘)
默认输出设备:标准输出,stdout, 1 (显示器)
标准错误输出:stderr, 2 (显示器)

> 覆盖

[root@zzd ~]# ls > new
[root@zzd ~]# cat new
a
abc
ABC
anaconda-ks.cfg
date.sh
findfls
new
passwd
qwer
test
[root@zzd ~]# ls abc > new      //&#x8986;&#x76D6;&#x4E86;&#x539F;&#x672C;&#x7684;&#x5185;&#x5BB9;
[root@zzd ~]# cat new
abc

>> 追加

[root@zzd ~]# cat findfls >> new    //&#x5C06;findfls&#x6587;&#x4EF6;&#x91CC;&#x7684;&#x5185;&#x5BB9;&#x8FFD;&#x52A0;&#x5230;&#x6587;&#x4EF6;new&#x91CC;
[root@zzd ~]# cat new
abc
 33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest

2> 重定向错误输出

[root@zzd ~]# asdf
-bash: asdf: command not found
[root@zzd ~]# asdf 2> 1.txt     //&#x91CD;&#x5B9A;&#x5411;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#xFF0C;&#x5C06;&#x62A5;&#x9519;&#x5199;&#x5165;1.txt&#x6587;&#x4EF6;
[root@zzd ~]# cat 1.txt
-bash: asdf: command not found

2>> 追加重定向错误输出

[root@zzd ~]# ls sadgga 2>> 1.txt       //&#x91CD;&#x5B9A;&#x5411;&#x9519;&#x8BEF;&#x8F93;&#x51FA;&#xFF0C;&#x5C06;&#x62A5;&#x9519;&#x8FFD;&#x52A0;&#x5230;1.txt&#x6587;&#x4EF6;
[root@zzd ~]# cat 1.txt
-bash: asdf: command not found
ls: cannot access 'sadgga': No such file or directory

&> 覆盖重定向标准输出或错误输出至同一个文件

[root@zzd ~]# ls passwd asdf &> 1.txt
[root@zzd ~]# cat 1.txt
ls: cannot access 'asdf': No such file or directory
passwd

&>> 追加重定向标准输出或错误输出至同一个文件

[root@zzd ~]# cat findfls asfagh &>> 1.txt
[root@zzd ~]# cat 1.txt
ls: cannot access 'asdf': No such file or directory
passwd
 33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest
cat: asfagh: No such file or directory

< 输入重定向

[root@zzd ~]# cat 1.txt
[root@zzd ~]# cat qwer
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
[root@zzd ~]# cat > 1.txt < qwer
[root@zzd ~]# cat 1.txt
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

<< 退出条件

[root@zzd ~]# cat > dame << quit        //&#x6307;&#x5411;&#x6587;&#x4EF6;dame,&#x8F93;&#x5165;,&#x5F53;&#x9047;&#x5230;quit&#x7684;&#x65F6;&#x5019;&#x9000;&#x51FA;
> 123
> 4
> qw
> as
> quit
[root@zzd ~]# cat dame
123
4
qw
as

前一个命令的输出,作为后一个命令的输入。最后一个命令会在当前shell进程子shell进程中执行
命令1 | 命令2 | 命令3 | …

[root@zzd ~]# ls
1.txt  a  abc  ABC  anaconda-ks.cfg  dame  date.sh  findfls  haha  new  passwd  qwer  test
[root@zzd ~]# ls | grep ABC     //&#x5217;&#x51FA;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x6216;&#x76EE;&#x5F55;&#xFF0C;&#x5C06;&#x7ED3;&#x679C;&#x4EA4;&#x7ED9;grep&#x8FC7;&#x6EE4;ABC
ABC

从标准输入读取数据,将数据输出到屏幕,并将数据保存到文件

[En]

Read data from standard input, output one to the screen, and save one to a file

[root@zzd ~]# echo 'Hello ZIC' | tee 1.txt
Hello ZIC
[root@zzd ~]# cat 1.txt
Hello ZIC

6.bash字符处理

FILE=/usr/local/hadoop/etc/hadoop
echo ${FILE#*/}

[root@zzd ~]# FILE=/usr/local/hadoop/etc/hadoop     //&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;FILE&#xFF0C;&#x503C;&#x4E3A;/usr/local/hadoop/etc/hadoop
[root@zzd ~]# echo $FILE        //&#x6253;&#x5370;&#x53D8;&#x91CF;FILE
/usr/local/hadoop/etc/hadoop

[root@zzd ~]# echo ${FILE#*/}
usr/local/hadoop/etc/hadoop

echo ${FILE##*/}

[root@zzd ~]# echo ${FILE##*/}
hadoop

echo ${FILE%/*}

[root@zzd ~]# echo ${FILE%/*}
/usr/local/hadoop/etc

${var:offset:number}

[root@zzd ~]# echo $FILE
/usr/local/hadoop/etc/hadoop
[root@zzd ~]# echo ${FILE:4:5}      //&#x4ECE;&#x53D8;&#x91CF;FILE&#x7684;&#x7B2C;4&#x4E2A;&#x7D22;&#x5F15;&#x5F00;&#x59CB;&#x622A;&#x53D6;5&#x4E2A;&#x5B57;&#x7B26;
/loca

${var: -lengh} 注意:冒号后面必须有一空白字符

[root@zzd ~]# echo ${FILE: -6}      //&#x622A;&#x53D6;&#x53D8;&#x91CF;FILE&#x6700;&#x53F3;&#x4FA7;&#x7684;6&#x4E2A;&#x5B57;&#x7B26;
hadoop

${var/pattern/substi} 查找var所表示的字符串中,第一次被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE/hadoop/had}
/usr/local/had/etc/hadoop

${var//pattern/substi} 查找var所表示的字符串中,所有能被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE//hadoop/had}
/usr/local/had/etc/had

${var/#pattern/substi} 查找var所表示的字符串中,行首被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# file='hello world'
[root@zzd ~]# echo $file
hello world
[root@zzd ~]# echo ${file/#hello/hi}
hi world

${var/%pattern/substi} 查找var所表示的字符串中,行尾被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE/%hadoop/had}
/usr/local/hadoop/etc/had

${var/pattern} 查找var所表示的字符串中,第一次被pattern所匹配到字符串,删除之

[root@zzd ~]# echo $FILE
/usr/local/hadoop/etc/hadoop
[root@zzd ~]# echo ${FILE/hadoop}
/usr/local//etc/hadoop

${var//pattern} 查找var所表示的字符串中,所有被pattern所匹配到字符串,删除之

[root@zzd ~]# echo ${FILE//hadoop}
/usr/local//etc/

${var/#pattern} 查找var所表示的字符串中,行首被pattern所匹配到字符串,删除之

[root@zzd ~]# echo $file
hello world
[root@zzd ~]# echo ${file/#hello}
world

${var/%pattern} 查找var所表示的字符串中,行尾被pattern所匹配到字符串,删除之

[root@zzd ~]# echo ${FILE/%hadoop}
/usr/local/hadoop/etc/

${var^^} 所有小写转换为大写

${var,,} 所有大写转换为小写

[root@zzd ~]# FILE=${FILE^^}    //&#x5C06;&#x53D8;&#x91CF;FILE&#x91CC;&#x7684;&#x5C0F;&#x5199;&#x8F6C;&#x6362;&#x4E3A;&#x5927;&#x5199;&#x5E76;&#x8D4B;&#x4E88;&#x53D8;&#x91CF;FILE
[root@zzd ~]# echo $FILE
/USR/LOCAL/HADOOP/ETC/HADOOP
[root@zzd ~]# echo ${FILE,,}        //&#x5C06;FILE&#x53D8;&#x91CF;&#x91CC;&#x7684;&#x5927;&#x5199;&#x90FD;&#x8F6C;&#x6362;&#x4E3A;&#x5C0F;&#x5199;
/usr/local/hadoop/etc/hadoop

Original: https://www.cnblogs.com/zicnotes/p/16448291.html
Author: Zic师傅
Title: Linux基础命令(三)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523191/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球