天天看点

Linux学习笔记(第三课)

文件目录管理命令

-----------------------------------------------------------------------------------------

touch命令用于创建空白文件或设置文件的时间,语法格式为:“touch [参数] 文件名称

[root@localhost mnt]# ls

[root@localhost mnt]# touch bing

bing

# vim linux 使用vim编辑一个不存在的文件,修改保存后,该会自动建立

[root@localhost mnt]# vim linux

bing  linux

--------------------------------------------------------------------------------------------------------------

mkdir命令用于创建空白的目录,英文全称为:“make directory”,语法格式为:“mkdir [参数] 目录名称”

[root@localhost mnt]# mkdir linux-b

bing  linux  linux-b

mkdir -p /mnt/linux-c/bing/geely/7dct  创建递增层次的文件

[root@localhost mnt]# mkdir -p /mnt/linux-c/bing/geely/7dct

bing  linux  linux-b  linux-c

[root@localhost mnt]# cd /mnt/linux-c/bing/geely/7dct

[root@localhost 7dct]# pwd

/mnt/linux-c/bing/geely/7dct

--------------------------------------------------------------------------------

cp命令用于复制文件或目录,英文全称为:“copy”,语法格式为:“cp [参数] 源文件名称 目标文件名称”

语法: cp 源… 目的

复制目录时,多个-r 选项:

# cp -r newdir/ /tmp/ 目标目录存在,则将源目录放在该目录下

# cp -r newdir /tmp/olddir 目标目录不存在,类似复制过去后重命名

# cp -r /run/media/admin/RHEL-7.0Server.x86_64/. /mnt/redhat7/  复制/run/media/admin/RHEL-7.0Server.x86_64/下所有文件到/mnt/redhat7/下

[root@localhost mnt]# cp bing bing1

bing  bing1  linux  linux-b  linux-c

[root@localhost mnt]# cp -r linux-b /mnt/linux-c

[root@localhost mnt]# cd linux-c

[root@localhost linux-c]# ls

bing  linux-b

mv命令用于剪切或重命名文件,英文全称为:“move”,语法格式为:“mv [参数] 源文件名称 目标文件名称”

[root@localhost mnt]# mv bing1 bing2

bing  bing2  linux  linux-b  linux-c

[root@localhost mnt]# mv bing2 /mnt/linux-b

[root@localhost mnt]# cd linux-b

[root@localhost linux-b]# ls

bing2

rm命令用于删除文件或目录,英文全称为:“remove”,语法格式为:“rm [参数] 文件名称”

rm -f 强制删除文件

rm -rf 强制删除目录文件

[root@localhost mnt]# rm linux

rm: remove regular file 'linux'? y

bing  linux-b  linux-c

[root@localhost mnt]# rm -f bing

linux-b  linux-c

[root@localhost mnt]# rm -rf linux-b

linux-c

dd命令用于按照指定大小的数据块个数来复制文件或转换文件

if  input file  输入文件

of output file 输出文件

bs 设置每个“块”的大小

count 设置要复制“块”的个数

语法格式为:“dd if=参数值 of=参数值 count=参数值 bs=参数值”

file命令用于查看文件的类型,语法格式为:“file 文件名称”

[root@localhost mnt]# file linux-c/

linux-c/: directory

(考试命令tar)

tar命令用于对文件进行打包压缩或解压,语法格式为:“tar 参数 文件名称

[root@localhost mnt]# tar czvf etc.tar.gz /etc

etc.tar.gz  linux-c

z  ---tar.gz

j  ---tar.bz2

tar czvf 压缩包.tar.gz

tar xjvf 压缩包.tar.bz2

参数 作用

-c 创建压缩文件

-x 解开压缩文件

-t 查看压缩包内有哪些文件

-z 用Gzip压缩或解压

-j 用bzip2压缩或解压

-v 显示压缩或解压的过程

-f 目标文件名

-p 保留原始的权限与属性

-P 使用绝对路径来压缩

-C 指定解压到的目录

grep命令用于在文本中执行关键词搜索,并显示匹配的结果,格式为“grep [选项] [文件]”

[root@localhost mnt]# grep /sbin/nologin /etc/passwd

bin:x:1:1:bin:/bin:/sbin/nologin

........

[root@localhost mnt]# grep -c /sbin/nologin /etc/passwd

48

-b 将可执行文件(binary)当作文本文件(text)来搜索

-c 仅显示找到的行数

-i 忽略大小写

-n 显示行号

-v 反向选择——仅列出没有“关键词”的行。

find命令用于按照指定条件来查找文件,格式为“find [查找路径] 寻找条件 操作”

[root@localhost mnt]# find /etc -name "host*" -print

/etc/opa/hosts

/etc/host.conf

/etc/hosts

/etc/hosts.allow

/etc/hosts.deny

/etc/avahi/hosts

/etc/hostname

---------------------------------------------------

重定向符(<、<

输入重定向(文件到命令)

命令 < 文件 将文件作为命令的标准输入

命令 << 分界符 从标准输入中读入,直到遇见分界符才停止

-------------------------------

输出重定向(命令到文件)

符号 作用

命令 > 文件 将标准输出重定向到一个文件中(清空写入 )

命令 2> 文件 将错误输出重定向到一个文件中(追加写入)

命令 >> 文件 将标准输出重定向到一个文件中(清空写入 )

命令 2>> 文件 将错误输出重定向到一个文件中(追加写入)

命令 &> 文件 将标准输出与错误输出共同写入到文件中(清空写入 )

命令 &>> 文件 将标准输出与错误输出共同写入到文件中(追加写入 )

[root@localhost mnt]# ls -l > test

[root@localhost mnt]# cat test

total 6548

-rw-r--r--. 1 root root 6701472 Apr 10 12:49 etc.tar.gz

drwxr-xr-x. 4 root root      33 Apr 10 12:31 linux-c

-rw-r--r--. 1 root root       0 Apr 10 13:12 test

[root@localhost mnt]# ls -l bing > test

ls: cannot access 'bing': No such file or directory

[root@localhost mnt]# ls -l bing 2> test

[root@localhost mnt]# ls -l  2> test

-rw-r--r--. 1 root root       0 Apr 10 13:14 test

[root@localhost mnt]#

[root@localhost mnt]# ls -l  &>> test

[root@localhost mnt]# ls -l geely  &>> test

ls: cannot access 'geely': No such file or directory

total 6552

-rw-r--r--. 1 root root      53 Apr 10 13:18 test

------------------------------------------------------------

管道符

grep /sbin/nologin /etc/passwd | wc -l

grep搜索命令的输出值传递给wc统计命令,即把原本要输出到屏幕的用户信息列表再交给wc命令作进一步的加工

[root@localhost mnt]# grep /sbin/nologin /etc/passwd | wc -l

把管道符和passwd命令的--stdin参数相结合,可以用一条命令来完成密码重置操作

echo "root" | passwd --stdin root

[root@localhost mnt]# echo "root" | passwd --stdin root

Changing password for user root.

passwd: all authentication tokens updated successfully.

----------------------------------------------------------------

命令行的通配符

[root@localhost ~]# ls -l /dev/sda

brw-rw----. 1 root disk 8, 0 Apr 10 12:24 /dev/sda

[root@localhost ~]# ls -l /dev/sda1

brw-rw----. 1 root disk 8, 1 Apr 10 12:24 /dev/sda1

[root@localhost ~]# ls -l /dev/sda2

brw-rw----. 1 root disk 8, 2 Apr 10 12:24 /dev/sda2

[root@localhost ~]# ls -l /dev/sda3

ls: cannot access '/dev/sda3': No such file or directory

[root@localhost ~]#

[root@localhost ~]# ls -l /dev/sd*

[root@linuxprobe ~]# ls -l /dev/sda?

brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1

brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2

[root@localhost ~]# ls -l /dev/sd?

[root@localhost ~]# ls -l /dev/sd??

通配符 含义

* 任意字符

? 单个任意字符

[a-z] 单个小写字母

[A-Z] 单个大写字母

[a-Z] 单个字母

[0-9] 单个数字

[:alpha:] 任意字母

[:upper:] 任意大写字母

[:lower:] 任意小写字母

[:digit:] 所有数字

[:alnum:] 任意字母加数字

[:punct:] 标点符号

----------------------------------------------------------------------

转义字符

单个转义--反斜杠(\):使反斜杠后面的一个变量变为单纯的字符。

全部转义--单引号(''):转义其中所有的变量为单纯的字符串。

有空格加--双引号(""):保留其中的变量属性,不进行转义处理。

执行命令--反引号(``):把其中的命令执行后返回结果。

[root@localhost ~]# PRICE=5

[root@localhost ~]# echo "Price is $PRICE"

Price is 5

[root@localhost ~]# echo "Price is \$$PRICE"

Price is $5

[root@localhost ~]# echo `uname -a`

Linux localhost.localdomain 4.18.0-80.el8.x86_64 #1 SMP Wed Mar 13 12:02:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux