Linux Basic Command Chapter 2-Getting Better

Introduction to Linux

One, the processing of bash strings

1.1, bash character processing-wildcard

  • 1. The current position of tyy, if it is a graphical terminal, it will display /dev/pts/0, if it is a black window interface, it will display /dev/tty1
  • 2. Wildcard, intercept variable string

FILE=/usr/local/src
echo ${file#*} results in usr/local/src
echo ${ile##*/} results in src
echo $(file%/*) results in /usr/local

  • 3. String slice: ${var:offset:number}
  • 4. Take the rightmost characters of the string (from right to left) ${var: -lengh}

Note: there must be a blank character after the colon

  • 5. Find and replace
Variable extraction description
$ {var / pattern / stubst} Find the string represented by var, which is matched by pattern for the first time
${var//patterb/substi} Find the string represented by var, all strings that can be matched by pattern
  • 6. Find and delete

${/var/pattern/} Find the string that is matched by pattern for the first time in the string represented by var, and delete it

  • 7. Character case conversion

${var^^} all lowercase is replaced by uppercase,
${var,} all uppercase is replaced by lowercase
// Note: Two commas ",,"


Second, file naming rules

1. The length cannot exceed 256 characters

2. The root symbol "/" cannot be used as the file name

3. Strictly case sensitive.

4. It can contain uppercase and lowercase letters or numbers, and can contain underscores but should not start with numbers and other characters.


3. Advanced Linux Common Commands-File System Search

  • 1,locate

: Non-real-time, fuzzy matching, search is carried out based on the system-wide file database, and the search speed is fast.
: Rely on the pre-built index, the index construction is automatically run when the system is idle.

usage

Options description
-b ,–basename -Match only the base name of the path name
-c, --count – Only output the quantity found
-d, --database DBPATH – Use the database specified by DBPATH instead of the default database /var/lib/mlocate/mlocate.db
-e, --existing -Only print the entries of the current existing file
-1 -If it is 1. Then start safe mode. In safe mode, users will not see files that cannot be seen by permissions. This will initially slow down, because locate must get the file permissions data in the actual file system.
-0, --null – Separate entry with NUL on the output
-S, --statistics – Do not search for entries, print statistics about each database
-q -Quiet mode, no error message will be displayed.
-P, --nofollow, -H – Do not follow trailing symbolic links when checking for file existence
-l, --limit, -n LIMIT Limit output (or count) to LIMIT entries
-n – Display at most n outputs.
-m, --mmap – Ignored for backward compatibility
-r, --regexp REGEXP – Use basic regular expressions
–regex – Use extended regular expressions
-q, --quiet – Quiet mode, no error message will be displayed
-s, --stdio – Ignored for backward compatibility
-The - Specify the name of the data inventory.
-h, --help – Show help
-i, --ignore-case – Ignore case
-V, --version – Display version information

E.g:Insert picture description here

  • 2, updatedb, manually generate file database

: The index building process needs to traverse the entire root file system, which consumes resources.
Parameters -o (directory): ignore the default database file, use the specified slocate database file
-U directory: update the slocate database of the specified directory
-v: display the detailed process of execution .
Find an unknown file, and when it still does not exist after creation, manually update the search directory through updatedb.
Insert picture description here

  • 3. find: real-time search, high accuracy, traverse all files in the specified directory to complete the search.
  • 3.1, Syntax: find [opion] search path, search criteria, find the action to be processed
  • Command example: find / -name abc Find the corresponding file under the root. Find / -type d -name passwd

The search path // default current directory, search criteria, // default is all files in the specified path.

parameter description
-name ‘filename’ //Exactly match the file name. Support glob wildcard mechanism
-iname ‘filename’ //File name matching is not case sensitive
-regex pattern //File name matching based on regular expressions. Use pattern \ // to match the entire file path string, not just the file name
-user username //Owner to find
-group groupname //Find according to belonging group
-uid / /Search based on UID, when the user is deleted, the owner of the file will become the UID of the user
-gid //Search based on GID, when the user is deleted, the file's group will become the user's GID
-nouser //Find files without owner. Files generated when the user is deleted, only uid has no owner
-nogroup //查找没有属组的文件.组被删除的情况下产生的文件,只有gid没有属组
-type //根据文件类型来查找(f,d,c,b,l,p,s)
type=f 文件
type=d 目录
type=c 字符
-size //根据文件大小进行查找。如1k、1M,+10k、+10M,-1k、-1M, \
/+表示大于,-表示小于 [+ -]
#K、#M、#G #Unit表示(从#-1到#之间的范围大小)-#Unit表示(从0到#-1的范围大小)+#Unit表示(大于#的所有)
-mtime //修改时间
ctime //改变时间
atime /访问时间 +5 //5天前-5 //5天以内
mmin /多少分钟修改过
cmin //多少分钟改变过
amin //多少分钟访过//+5分钟前-5 //5分钟以内
-perm mode //根据权限精确查找
-perm -mode //文件权限能完全包含此mode时才符合条件
-perm /mode //9位权限中有任何一位权限匹配都视为符合查找条件

示例:root@Eryuege ~]# find /root/ -mmin -1
/root/
/root/abc
/root/abc/w
/root/abc/w/c
/root/c
/root/c/abc
/root/c/abc/w
/root/c/abc/w/c

  • 3.2//组合条件:
    -a
    -o
    -not

    例:
    !A -a !B = !(A -o B)
    |A -o !B = !(A -a B)

  • 3.3//处理动作:默认为显示到屏幕上
    -print //显示
    -ls //类似ls -l的形式显示每一个文件的详细信息
    -delete //删除查找到的文件
    -fls /path/to/somefile //查找到的所有文件的长格式信息保存至指定文件中
    -ok COMMAND {} ; //对查找到的每个文件执行COMMAND,每次操作都需要用户确认
    -exec COMMAND {} ; //对查找到的每个文件执行COMMAND,操作不需要确认

//注意:find传递查找到的文件至后面指定的命令时,查找到所有
//符合条件的文件一次性传递给后面的命令,而有些命令不能接受过多参数,
//此时命令执行可能会失败。而xargs可规避此问题。
xargs //通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可
Insert picture description here


四,文件层级系统

4.1FHS

  • //文件层级系统 多数的linu版本采用这种文件组织形式,类似于Windows操作系统中C盘的文件目录。

文件系统

文件路径 文件描述
/ //可以单独分区,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内是系统启动就需要用到的程序,这些目录不能挂载额外的分区,
必须在根文件系统的分区上
/usr/bin,/usr/sbin,/usr/lib提供操作系统核心功能,/usr可以单独分区
/usr/local/bin,/usr/local/sbin,/usr/local/lib,/usr/local/etc,
/usr/local/man等等在/usr/local目录下的内容都是第三方软件,建议单独分区

4.2重定向和管道

重定向

输出重定向 ls > haha 将ls的内容输出到haha
输入重定向ls >haha <xixi 将xixi的内容输入到哈哈。
双>>表示追加,单>表示覆盖 双<<表示当作匹 配的内容,表示开始和结束;
示例:
Insert picture description here

管道

  • 管道符 " | ":可以把一个命令的标准输出传送到另一个命令的标准输入中,连续的|意味着第一个命令的输出为第二个命令的输入,第二个命令的输入为第一个命令的输出,依次类推。

命令使用示例:
[root@Eryuege ~]# ls > hehe |cat hehe
2020-10-18
abc
ac
anaconda.log
b.tar.bz2
c
c.tar.zx
haha
hehe
initial-setup-ks.cfg
num.tar.gz
passwd
runoob.txt
test
wokr
work
xixi
[root@Eryuege ~]#
//系统设定:
默认输入设备 //标准输入,STDIN,0 (键盘)
默认输出设备 //标准输出,STDOUT,1 (显示器)
标准错误输出 //STDERR,2 (显示器)

//I/O重定向:
>:覆盖输出
>>:追加输出

2> //重定向错误输出
2>> //追加重定向错误输出
&> //覆盖重定向标准输出或错误输出至同一个文件
&>> //追加重定向标准输出或错误输出至同一个文件
< //输入重定向
<< //Here Document

Pipe // the output of the previous command as the input of the next command. The last command will be executed in the subshell process of the current shell process
//
Command 1 | Command 2 | Command 3 |…

tee //Read data from standard input, output one copy to the screen, and save one copy to file

[root@localhost ~]# echo “hello world” | tee /tmp/hello.out
hello world
[root@localhost ~]# cat /tmp/hello.out
hello world

Guess you like

Origin blog.csdn.net/LBJ19224/article/details/109135103