发现学习难度逐渐提高,真的要多练啊!node
一、文件目录管理命令
用于建立空白文件或设置文件的时间 git
参数 | 做用 |
---|---|
-a | 仅修改“读取时间”(atime) |
-m | 仅修改“修改时间”(mtime) |
-d | 同时修改atime与mtime |
实验:同时修改读取时间和修改时间后效果以下vim
[root@mingmingxingforcomputer ~]# stat anaconda-ks.cfg File: anaconda-ks.cfg Size: 1404 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 35268260 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2021-06-27 17:22:00.000000000 +0800 Modify: 2021-06-27 17:22:00.000000000 +0800 Change: 2021-06-27 17:22:56.840020967 +0800 Birth: - [root@mingmingxingforcomputer ~]# touch -d "2022-6-27 15:01" anaconda-ks.cfg [root@mingmingxingforcomputer ~]# stat anaconda-ks.cfg File: anaconda-ks.cfg Size: 1404 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 35268260 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2022-06-27 15:01:00.000000000 +0800 Modify: 2022-06-27 15:01:00.000000000 +0800 Change: 2021-06-27 17:23:24.010018406 +0800 Birth: -
修改后对比安全
原值 | 修改后 |
---|---|
Access: 2021-06-27 17:22:00.000000000 +0800 | Access: 2022-06-27 15:01:00.000000000 +0800 |
Modify: 2021-06-27 17:22:00.000000000 +0800 | Modify: 2022-06-27 15:01:00.000000000 +0800 |
建立目录bash
嵌套关系目录建立 mkdir -p(递归方式建立)markdown
实验1:递归建立目录编辑器
[root@mingmingxingforcomputer ~]# mkdir a [root@mingmingxingforcomputer ~]# ls a Desktop Downloads Music Public Videos anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates [root@mingmingxingforcomputer ~]# rm -rf a [root@mingmingxingforcomputer ~]# mkdir a/b/c mkdir: cannot create directory ‘a/b/c’: No such file or directory [root@mingmingxingforcomputer ~]# mkdir -p a/b/c [root@mingmingxingforcomputer ~]# cd a/b/c [root@mingmingxingforcomputer c]# pwd /root/a/b/c
若是不加参数-p递归建立目录,会报错ide
[root@mingmingxingforcomputer ~]# mkdir a/b/c mkdir: cannot create directory ‘a/b/c’: No such file or directory
实验2:先建立一个名为a的目录,在当前目录下执行mkdir a/b/c -p命令,该方法可行学习
[root@mingmingxingforcomputer ~]# mkdir a [root@mingmingxingforcomputer ~]# mkdir a/b/c -p [root@mingmingxingforcomputer ~]# cd a/b/c [root@mingmingxingforcomputer c]# pwd /root/a/b/c
显示目录中的文件信息 code
LS命令相关参数 | 含义 |
---|---|
-a | 所有文件 |
-l | 查看文件的属性、大小等详细信息 |
-d | 目录权限 |
-Z | 安全上下文 |
实验:查看文件详细信息,列表方式显示
[root@mingmingxingforcomputer ~]# ls -al | tail -n 3 drwxr-xr-x. 2 root root 6 Jan 4 03:40 Videos -rw-------. 1 root root 8715 Jun 27 14:20 .viminfo -rw-------. 1 root root 69 Jun 27 17:14 .Xauthority
复制文件或目录
cp [参数] 源文件名称 目标文件名称
参数 | 做用 |
---|---|
-p | 保留原始文件的属性 |
-d | 若对象为“连接文件”,则保留该“连接文件”的属性 |
-r | 递归持续复制(用于目录) |
-i | 若目标文件存在则询问是否覆盖 |
-a | 至关于-pdr(p、d、r为上述参数) |
实验:将a.txt复制给b.txt
[root@mingmingxingforcomputer ~]# echo "hello world!" > a.txt [root@mingmingxingforcomputer ~]# cat a.txt hello world! [root@mingmingxingforcomputer ~]# cp -a b.txt a.txt cp: cannot stat 'b.txt': No such file or directory [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates a.txt Desktop Downloads Music Public Videos [root@mingmingxingforcomputer ~]# cat b.txt hello world!
注意:先写源文件,再写目标文件
[root@mingmingxingforcomputer ~]# cp -a b.txt a.txt cp: cannot stat 'b.txt': No such file or directory
剪切,重命名文件
cp [参数] 源文件名称 目标文件名称
实验:重命名、移动
一、将a.txt文件更名成b.txt
[root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads Music Public Videos a.txt Documents initial-setup-ks.cfg Pictures Templates [root@mingmingxingforcomputer ~]# mv a.txt b.txt [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads Music Public Videos b.txt Documents initial-setup-ks.cfg Pictures Templates
二、将b.txt文件移到到home目录下
[root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads Music Public Videos b.txt Documents initial-setup-ks.cfg Pictures Templates [root@mingmingxingforcomputer ~]# mkdir NICE [root@mingmingxingforcomputer ~]# mv b.txt NICE/b.txt [root@mingmingxingforcomputer ~]# cd NICE [root@mingmingxingforcomputer NICE]# ls b.txt [root@mingmingxingforcomputer NICE]# cd .. [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg NICE Public Videos Desktop Downloads Music Pictures Templates
删除文件或目录
rm [参数] 文件名称
参数 | 做用 |
---|---|
-f | 强制执行 |
-i | 删除前询问 |
-r | 删除目录 |
-v | 显示过程 |
工做中千万不要使用rm -rf /*命令!!!
实验:删除文件、目录
一、删除b.txt文件
[root@mingmingxingforcomputer NICE]# ls b.txt [root@mingmingxingforcomputer NICE]# rm -f b.txt
二、删除目录NICE
[root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg NICE Public Videos Desktop Downloads Music Pictures Templates [root@mingmingxingforcomputer ~]# rm -rf NICE/ [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
复制文件中一部份内容
实验::分区表备份命令:dd if=/dev/sda of=backup bs=512 count=1
[root@mingmingxingforcomputer ~]# dd if=/dev/sda of=backup bs=512 count=1 1+0 records in 1+0 records out 512 bytes copied, 0.000116656 s, 4.4 MB/s
文件中出现backup备份文件
[root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads Music Public Videos backup Documents initial-setup-ks.cfg Pictures Templates 判别文件类型
查看文件类型
实验:查看文件类型
[root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads Music Public Videos backup Documents initial-setup-ks.cfg Pictures Templates [root@mingmingxingforcomputer ~]# file backup backup: DOS/MBR boot sector [root@mingmingxingforcomputer ~]# file anaconda-ks.cfg anaconda-ks.cfg: ASCII text
压缩、解压缩文件
经常使用压缩方式:
压缩:tar czvf/cjvf 压缩包名 文件名
解压缩:tar xzvf 压缩包名
参数 | 做用 |
---|---|
-c | 建立压缩文件 |
-x | 解开压缩文件 |
-t | 查看压缩包内有哪些文件 |
-z | 用Gzip压缩或解压 |
-j | 用bzip2压缩或解压 |
-v | 显示压缩或解压的过程 |
-f | 目标文件名 |
-p | 保留原始的权限与属性 |
-P | 使用绝对路径来压缩 |
-C | 指定解压到的目录 |
实验:压缩hello.txt文件
一、将hello.txt压缩成hello.gz
二、删除源文件hello.txt
三、将hello.gz解压缩成hello.txt
[root@mingmingxingforcomputer ~]# echo "nice!" > hello.txt [root@mingmingxingforcomputer ~]# cat hello.txt nice! [root@mingmingxingforcomputer ~]# tar czvf hello.gz hello.txt hello.txt [root@mingmingxingforcomputer ~]# rm -rf hello.txt [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads initial-setup-ks.cfg Pictures Templates backup Documents hello.gz Music Public Videos [root@mingmingxingforcomputer ~]# tar xzvf hello.gz hello.txt [root@mingmingxingforcomputer ~]# ls anaconda-ks.cfg Desktop Downloads hello.txt Music Public Videos backup Documents hello.gz initial-setup-ks.cfg Pictures Templates
输出重定向
命令 > 文件 | 将标准输出重定向到一个文件中(清空原有文件的数据) |
---|---|
命令 2> 文件 | 将错误输出重定向到一个文件中(清空原有文件的数据) |
命令 >> 文件 | 将标准输出重定向到一个文件中(追加到原有内容的后面) |
命令 2>> 文件 | 将错误输出重定向到一个文件中(追加到原有内容的后面) |
命令 >> 文件 2>&1 或 命令 &>> 文件 | 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面) |
实验1:屏幕上的文字传输到文本中
[root@mingmingxingforcomputer ~]# echo "nice" > a.txt [root@mingmingxingforcomputer ~]# cat a.txt nice
实验2:追加写入一段文字进入文本
[root@mingmingxingforcomputer ~]# echo "MMX 2021" >> a.txt [root@mingmingxingforcomputer ~]# cat a.txt nice MMX 2021
实验3:错误信息写入文本
实验中:发现错误信息直接输入会报错,须要在重定向符前面加上“2”
[root@mingmingxingforcomputer ~]# find /a -name good find: ‘/a’: No such file or directory [root@mingmingxingforcomputer ~]# find /a -name good > a.txt find: ‘/a’: No such file or directory [root@mingmingxingforcomputer ~]# find /a -name good 2> a.txt [root@mingmingxingforcomputer ~]# cat a.txt find: ‘/a’: No such file or directory
实验4:所有追加,不管是否报错
实验中,发现>>追加剧定向遇到错误信息会报错,须要在前面加上“&”
[root@mingmingxingforcomputer ~]# ls -l lx ls: cannot access 'lx': No such file or directory [root@mingmingxingforcomputer ~]# ls -l lx >> a.txt ls: cannot access 'lx': No such file or directory [root@mingmingxingforcomputer ~]# ls -l lx &>> a.txt [root@mingmingxingforcomputer ~]# cat a.txt ls: cannot access 'lx': No such file or directory
输入重定向:把文件导入到命令中,而输出重定向则是指把本来要输出到屏幕的数据信息写入到指定文件中
符号 | 做用 |
---|---|
命令 < 文件 | 将文件做为命令的标准输入 |
命令 << 分界符 | 从标准输入中读入,直到碰见分界符才中止 |
命令 < 文件1 > 文件2 | 将文件1做为命令的标准输入并将标准输出到文件2 |
实验:统计行数
[root@mingmingxingforcomputer ~]# wc -l anaconda-ks.cfg 45 anaconda-ks.cfg [root@mingmingxingforcomputer ~]# wc -l < anaconda-ks.cfg 45
简而言之:
输出重定向:命令 → 文件(用的多)
输入重定向:文件 → 命令(用得少)
把前一个命令本来要输出到屏幕的信息看成是后一个命令的标准输入
实验1:统计文本行数
[root@mingmingxingforcomputer ~]# cat anaconda-ks.cfg | wc -l 45
实验2:查看内存使用状况
[root@mingmingxingforcomputer ~]# free -h | grep Mem: | awk '{print $4}' 6.4Gi
实验3:修改用户密码
[root@mingmingxingforcomputer ~]# echo "123456" | passwd --stdin root Changing password for user root. passwd: all authentication tokens updated successfully.
实验4:多命令结合使用
[root@mingmingxingforcomputer ~]# ps aux | grep root | tail -n 3 > a.txt [root@mingmingxingforcomputer ~]# cat a.txt root 4641 0.0 0.0 57172 3900 pts/0 R+ 20:35 0:00 ps aux root 4642 0.0 0.0 12112 964 pts/0 S+ 20:35 0:00 grep --color=auto root root 4643 0.0 0.0 7320 832 pts/0 S+ 20:35 0:00 tail -n 3
通配符 | 含义 |
---|---|
* | 任意字符 |
? | 单个任意字符 |
[a-z] | 单个小写字母 |
[A-Z] | 单个大写字母 |
[a-Z] | 单个字母 |
[0-9] | 单个数字 |
[:alpha:] | 任意字母 |
[:upper:] | 任意大写字母 |
[:lower:] | 任意小写字母 |
[:digit:] | 全部数字 |
[:alnum:] | 任意字母加数字 |
[:punct:] | 标点符号 |
*实验1:通配符 “” 查看相关文件
[root@mingmingxingforcomputer ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2
实验2:通配符 “?”查看相关文件
[root@mingmingxingforcomputer ~]# ls /dev/sd? /dev/sda
实验3:通配符特殊玩法
[root@mingmingxingforcomputer ~]# echo file{1,2,3,4,5} >> a.txt [root@mingmingxingforcomputer ~]# cat a.txt file1 file2 file3 file4 file5
反斜杠(\):使反斜杠后面的一个变量变为单纯的字符。
单引号(''):转义其中全部的变量为单纯的字符串。
双引号(""):保留其中的变量属性,不进行转义处理。
反引号(``):把其中的命令执行后返回结果。
定义一个变量
[root@mingmingxingforcomputer ~]# PRICE=5
实验1:定义变量
[root@mingmingxingforcomputer ~]# echo $PRICE 5
实验2:打印"PRICE=$5"
[root@mingmingxingforcomputer ~]# echo PRICE=\$$PRICE PRICE=$5
实验3:使用反引号赋值(查看当前使用内存)
[root@mingmingxingforcomputer ~]# echo `free -h | grep Mem:|awk '{print $3}'` 763Mi
别名定义:alias 别名=命令
实验1:查看别名
[root@mingmingxingforcomputer ~]# which ls alias ls='ls --color=auto' /usr/bin/ls
实验2:定义一个别名
[root@mingmingxingforcomputer ~]# alias mmx='ll -al|tail -n 5' [root@mingmingxingforcomputer ~]# mmx -rw-r--r--. 1 root root 129 Aug 13 2018 .tcshrc drwxr-xr-x. 2 root root 6 Jan 4 03:40 Templates drwxr-xr-x. 2 root root 6 Jan 4 03:40 Videos -rw-------. 1 root root 8387 Jun 27 18:42 .viminfo -rw-------. 1 root root 69 Jun 27 17:14 .Xauthority
实验3:删除自定义别名
[root@mingmingxingforcomputer ~]# unalias mmx [root@mingmingxingforcomputer ~]# mmx bash: mmx: command not found...
实验4:查找命令文件
[root@mingmingxingforcomputer ~]# echo $BASH /bin/bash [root@mingmingxingforcomputer ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
10个重要的环境变量
变量名称 | 做用 |
---|---|
HOME | 用户的主目录(即家目录) |
SHELL | 用户在使用的Shell解释器名称 |
HISTSIZE | 输出的历史命令记录条数 |
HISTFILESIZE | 保存的历史命令记录条数 |
邮件保存路径 | |
LANG | 系统语言、语系名称 |
RANDOM | 生成一个随机数字 |
PS1 | Bash解释器的提示符 |
PATH | 定义解释器搜索用户执行命令的路径 |
EDITOR | 用户默认的文本编辑器 |
实验5:全局变量
[root@mingmingxingforcomputer ~]# WORKDIR=/home/workdir/ [root@mingmingxingforcomputer ~]# echo $WORKDIR /home/workdir/ [root@mingmingxingforcomputer ~]# cd $WORKDIR [root@mingmingxingforcomputer workdir]# pwd /home/workdir
[root@mingmingxingforcomputer workdir]# unset WORKDIR [root@mingmingxingforcomputer workdir]# echo $WORKDIR [root@mingmingxingforcomputer /]# cd $WORKDIR
其实把学习笔记放在这里面也不错!