磁盘和文件管理命令
系统目录介绍
/: 根目录,一般根目录下只存放目录,不要存放文件,/etc、/bin、/dev、/lib、/sbin应该和根目录放置在一个分区中
/bin(binary): /usr/bin: 可执行二进制文件的目录,如常用的命令ls、tar、mv、cat等。
/boot: 放置linux系统启动时用到的一些文件。/boot/vmlinuz为linux的内核文件,以及/boot/grub。建议单独分区,分区大小100M即可
/dev: 存放linux系统下的设备文件,访问该目录下某个文件,相当于访问某个设备,常用的是挂载光驱mount /dev/cdrom /mnt。
/etc: 系统配置文件存放的目录,不建议在此目录下存放可执行文件,重要的配置文件有/etc/inittab、/etc/fstab、/etc/init.d、/etc/X11、/etc/sysconfig、/etc/xinetd.d修改配置文件之前记得备份。注:/etc/X11存放与x windows有关的设置。
/home: 系统默认的(普通)用户家目录,新增用户账号时,用户的家目录都存放在此目录下,表示当前用户的家目录,test表示用户test的家目录。建议单独分区,并设置较大的磁盘空间,方便用户存放数据
/lib: /usr/lib:/usr/local/lib: 系统使用的函数库的目录,程序在执行过程中,需要调用一些额外的参数时需要函数库的协助,比较重要的目录为/lib/modules。
/mnt: /media: 设备默认挂载点,通常光盘挂载于/mnt/cdrom下,也不一定,可以选择任意位置进行挂载.
/opt: 给主机额外安装软件所摆放的目录。如:FC4使用的Fedora 社群开发软件,如果想要自行安装新的KDE 桌面软件,可以将该软件安装在该目录下。以前的 Linux 系统中,习惯放置在 /usr/local 目录下
/proc: 此目录的数据都在内存中,如系统核心,外部设备,网络状态,由于数据都存放于内存中,所以不占用磁盘空间,比较重要的目录 process 进程有/proc/cpuinfo、/proc/interrupts、/proc/dma、/proc/ioports、/proc/net/*等
/root: 系统管理员root的家(根)目录,系统第一个启动的分区为/,所以最好将/root和/放置在一个分区下。
/sbin: /usr/sbin:/usr/local/sbin: 放置系统管理员root使用的可执行命令,如fdisk、shutdown、mount等。与/bin不同的是,这几个目录是给系统管理员root使用的命令,一般用户只能"查看"而不能设置和使用。
/tmp: 一般用户或正在执行的程序临时存放文件的目录,任何人都可以访问,重要数据不可放置在此目录下
/srv: 服务启动之后需要访问的数据目录,如www服务需要访问的网页数据存放在/srv/www内
/usr: 应用程序存放目录,/usr/bin 存放应用程序, /usr/share 存放共享数据,/usr/lib 存放不能直接运行的,却是许多程序运行所必需的一些函数库文件。/usr/local:存放软件升级包。/usr/share/doc: 系统说明文件存放目录。/usr/share/man: 程序说明文件存放目录,使用 man ls时会查询/usr/share/man/man1/ls.1.gz的内容建议单独分区,设置较大的磁盘空间
/var: 放置系统执行过程中经常变化的文件,如随时更改的日志文件variable /var/log,/var/log/message: 所有的登录文件存放目录,/var/spool/mail: 邮件存放的目录,/var/run: 程序或服务启动
命令格式
命令 【-选项】 【 参数】
如:ls -la /usr
命令分类和帮助
内部命令:属于Shell解析器的一部分(cd pwd),执行速度会快
外部命令:独立于Shell解析器之外的文件程序(ls mkdir),执行速度会慢(每次运行外部命令,需要shell解析器先加载解析,再执行)
内部命令:help + 命令(help cd)
外部命令:man(manual 帮助手册) + 命令(man ls)
查看一个命令的帮助文档,如果文档比较长,enter键一行一行看,space(空格)键 一页一页看 字母q(quit) 退出帮助文档
常用命令
- 查看目录内容
ls list directory contents 列出目录内容
选项:
-a all do not ignore entries starting with .
在linux系统中以.开头的文件或者目录是隐藏文件 -a 显示所有内容,包括隐藏文件
-l use a long listing format 使用长的列表格式显示目录或者详情
-t sort by modification time, newest first 按照更新时间排序 越新的越在前面倒叙排序文件内容
简单练习:
touch .a 创建.a文件
touch .b 创建.b文件
mkdir .aaa 创建.aaa目录
mkdir .bbb
ls 查看不到.开头目录文件
ls -a 显示所有的
ls -l 显示内容详细信息
ll 是ls -l 的简化写法
ls -a -l 多个选项一起使用 显示所有内容的详情
ls -al 多个选项可以合并使用
ls -la
ls
ls -t 发现显示文件顺序不同
ls -a -l -t 发现确实按照更新时间倒叙排序
ls -alt
ls -lat
ls -alt / 显示根目录所有内容详情,倒叙排序
ls -lta /etc/
ls -la /etc/sysconfig/network-scripts/
ls -l ../ 相对目录 ../上级目录
cd /etc/sysconfig/network-scripts/ 进入该目录
ls ../../../root/ 相对路径显示 选项可以随意加
- 切换目录
cd Change the shell working directory 改变工作目录
具体使用:
cd /etc/sysconfig/network-scripts/ 切换到指定目录
cd ../ 切换上级目录
cd ../../ 切换到上两级目录
cd 什么都参数都不加 切换到当前用户的根目录
cd /etc/sysconfig/network-scripts/
cd ~ 和上面什么参数都不加一样 切换到当前用户的根目录
- 创建目录
mkdir make directories 创建单个/多个目录
选项:
-p parents no error if existing, make parent directories as needed
一次创建多级目录,使用-p会按照需要创建父目录,不会让创建错误
具体用法:
mkdir spring 创建单个目录
mkdir mybatis springboot 创建多个目录
mkdir spring/ioc spring/aop spring/mvc 一次在某一个目录下创建多个目录
ls spring
mkdir ../var/aaa ../var/bbb 相对路径创建多个目录
ls /var/
mkdir aa/bb/cc/dd 错误的 因为创建dd时,cc不存在。。。。
mkdir -p aa/bb/cc/dd 正确写法
- 创建文件
touch change file timestamps 改变文件时间戳
A FILE argument that does not exist is created empty
当touch命令后根的参数文件不存在时,会创建一个空文件
具体用法:
ll 查看a.txt的创建时间
touch a.txt 改变a.txt的文件更新时间
ll 发现a.txt的更新时间编程当前时间
touch b.txt b.txt不存在就会创建一个空的b.txt
ll 发现b.txt被创建,内容为0
- 查看文件内容
cat
cat: concatenate files and print on the standard output 串联文件并打印到标准输出,显示一个文件的全部内容
简单练习:
touch a.txt
touch b.txt
touch c.txt
echo 'hello' 正常显示标准输出 echo= display a line of text
echo 'hello' > a.txt > 写入 把hello内容写入到a.txt
echo 'world' > b.txt
echo 'bye' > b.txt
ll
cat a.txt b.txt c.txt 串联3个文件,并把内容打印
cat a.txt
cat b.txt
touch tomcat.log 创建一个tomcat.log文件
echo 'abc' > tomcat.log 写入abc
cat tomcat.log
echo '123' > tomcat.log 写入123
cat tomcat.log 发现写入会覆盖原来内容
date 显示当前时间
date >> tomcat.log >>追加 不会覆盖原来内容,在原来内容的下一行进行追加
cat tomcat.log
date >> tomcat.log 多次执行 发现都是追加
cat tomcat.log 当我们执行多次后,文件不再一页 cat的弊端就显示出来,只能看到文件的最后一页内容
head:
output the first part of files 显示文件的开始部分选项:
-n lines print the first K lines instead of the first 10;---如果指定了n的值为k 打印 前K 代替前10行
具体用法:
head tomcat.log 默认显示文件的前10行
head -5 tomcat.log 显示前5行
head -2 tomcat.log
head -20 tomcat.log
tail:
output the last part of files 输出文件的末尾部分
选项:
-n 显示文件尾部指定行
-f follow output appended data as the file grows --文件增长时显示追加数据
-F 和-f 只不过带retry(重试)功能
具体用法:
tail tomcat.log 默认显示后10行
tail -2 tomcat.log
tail -20 tomcat.log
tail -f tomcat.log 查看tomcat.log追加部分 会阻塞不动,另外启动一个会话窗口,在里面执行
echo 'appened111' >> tomcat.log
echo 'appened222' >> tomcat.log
date>>tomcat.log 多次执行 观察原窗口的变化
tail -F tomcat.log 带重试功能,在web项目中都会记录日志,通常记录日志时,如果日志是按照日志每天生成的, 当前的文件是tomcat.out 今天过完把tomcat.log变为tomcat.2022-08-12.log文件,最新的仍然是tomcat.out 继续追踪最新文件
ctrl+c 退出
more:
file perusal filter for crt viewing 分页显示数据
具体用法:
more settings.xml
enter 键 一行一行显示
space 空格键 向下翻页
ctrl+f 向下翻页
ctrl+b 向上翻页
q 退出
less:
opposite of more 分页显示数据
具体用法:
less settings.xml
enter 键 一行一行显示
space 空格键 向下翻页
ctrl+f 向下翻页
ctrl+b 向上翻页
q 退出
可以使用 /关键字 进行搜索 高亮关键字
- 复制文件
cp opy files and directories 复制和目录 相当于windows下的ctrl+c + ctrl+v
选项:
-r recursive 递归 copy directories recursively 递归复制目录
具体用法:
ls
cp a.txt spring 复制a.txt到spring目录
ls
ls spring 发现存在了
ls /tmp
cp /root/a.txt /tmp 复制a.txt到/tmp目录下
ls /tmp
cp ../etc/sysconfig/network-scripts/ifcfg-ens33 /tmp/ 相对路径复制
ls /tmp
cat /tmp/ifcfg-ens33
mkdir frames 创建一个框架目录
cp spring frames/ cp 不可以直接复制目录
mkdir spring/aop/aspect
mkdir spring/aop/pointcut
cp -r spring frames/ 复制spring目录到frames目录下
ls frames/spring/
ls frames/spring/aop/
- 查看当前工作目录
pwd print name of current/working directory 打印当前工作目录的名称
具体用法:
pwd 回车 ,默认显示当前工作目录的位置
cd /etc/sysconfig/network-scripts/
pwd
cd ../
pwd
cd ../../
pwd
- 移动文件或者重命名
mv move (rename) files 移动/重命名 文件 相当于windows中的ctrl+x ctrl+v
具体用法:
ls
ls spring
mv b.txt spring 移动b.txt 文件到 目录中
ls 发现b.txt没有了
ls spring 发现b.txt
ls /var/log/
mv /root/c.txt /var/log/ 移动c.txt到 /var/log/
ls 发现c.txt没有了
ls /var/log/ 发现c.txt
ls
mv springboot/ frames/ 移动目录到另外一个目录中
ls
ls /frames 发现springboot目录
ls
mv a.txt aaa.txt 把a.txt重命名为aaa.txt aaa.txt目录不存在
ls
mv frames/ frameset 把目录frames重命名为frameset
ls
- 删除空目录
rmdir remove empty directories 删除空目录
具体用法:
ls
rmdir mybatis/ 直接删除空目录mybatis
ls
rmdir frameset/ 不能删除非空目录
ls
mkdir bb cc dd
ls
rmdir bb cc dd 删除多个空目录
ls
mkdir -p bb/cc/dd
rmdir bb 无法删除
- 删除文件
rm remove files or directories 移除为你教案或者目录
选项:
-r recursive 递归的 remove directories and their contents recursively
递归删除目录及目录下所有内容
-f force 强制的 ignore nonexistent files and arguments, never prompt
不带提示信息,直接删除
具体用法:
ls
rm 中文.txt 删除 中文.txt 有提示信息 需要确认是否删除 y/n
ls
rm -f aaa.txt 直接删除aaa.txt不带提示
ls
mkdir -p bb/cc/dd
rm -f bb 无法删除目录
rm bb 无法删除目录
ls
rm -rf bb 不带提示递归删除目录bb 及其下的所有内容
ls
无论是文件或者目录,都可以使用 rm -rf 强制删除
touch frameset/a frameset/b frameset/c.log 在frameset下创建3个文件
ls frameset 有目录也有文件
rm -rf frameset/* * 为通配符,通配所有内容
ls frameset
ls
rm -rf aa spring/ settings.xml 移动多个文件和目录
ls
- 文件统计
wc (word count) print newline, word, and byte counts for each file 打印每个文件行数,单词数和字节数
选项:
-l print the newline counts 打印行数
-c print the byte counts 打印字节数
-m print the character counts 打印字符数(utf-8时一个中文1个字符 3个字节)
-w print the word counts 打印单词数
具体用法:
touch a.log
echo 'hello'>>a.log
wc a.log 结果为1 1 6 a.log 行数 单词数(以换行或者空格分割) 字节数(一个空格和换行都按照1个字节计算 1个中文汉字占3个字节utf-8) 文件名字
echo '中国'>>a.log
cat a.log
wc a.log 2 2 13 2行 2单词 13字节
echo 'hello AAA'>>a.log
cat a.log
wc a.log 3 4 23 3行 4单词 23 字节
wc -l a.log 打印行数
wc -c a.log 打印字节数
wc -w a.log 打印单词数
wc -m a.log 打印字符数 (一个中文占3个字符)
- 查找文件或者目录
find search for files in a directory hierarchy 按照目录层次查询文件
具体用法:
find -name '*t*' 列出当前目录下所有名称含有t的文件或者目录
find /etc -name '*audit*' 查找/etc下 所有名称含有audit的文件或者目录
find /etc -name '*t*'
find /etc -name '*audit*'
find /etc -name '*audit*' | wc -l 查找/etc下 所有名称含有audit的文件或者目录 并统计行数
find /etc -name '*t*' | wc -l | 管道符 连接多个命令的 通常后一个命令把前一个命令的执行结果作为参数
- 筛选文件内容
grep(global regular expression print) print lines matching a pattern 全局按照正则表达式查找匹配内容,并把查找结果打印到标准数据
grep(缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。(百度百科)
具体用法:
cat tomcat.log 查看所有内容
grep 22: tomcat.log 查找含有22:的所有行,并打印
grep This settings.xml
grep a settings.xml
grep a settings.xml | wc -l 从 settings.xml文件中查找含有a的行,并统计行数
find /etc -name '*t*' | grep pos 从find的查找结果查找含有pos的行并打印
find /etc -name '*t*' | grep pos | wc -l 从find的查找结果查找含有pos的行并打印,并统计行数
- 创建快捷方式(创建软硬连接)
ln make links between files 在文件之间创建连接
选项:
-s make symbolic links instead of hard links 为文件或者目录创建象征性的连接 软链接 (相当于windows下的快捷方式)
-d allow the superuser to attempt to hard link directories
硬链接 给源文件创建一个副本文件,当源文件删除,对副本文件没有任何影响
具体用法:
mkdir -p /tmp/aaa/bbb/ccc
ln -s /tmp/aaa/ aaa.lnk 为/tmp下aaa目录创建软连接到当前用户主目录下
ls -l 发现aaa.lnk连接到/tmp/aaa
ls aaa.lnk/ 测试是否可以使用 直接看到bbb
rm -rf /tmp/aaa 删除源目录
ll 发现aaa.lnk变红色 不可用了
mkdir -p /tmp/aaa/bbb/ccc 再次创建目录
ll 发现又可以使用
ls aaa.lnk 查看内容
cat a.log
ln -d a.log a.log.lnk 为a.log创建硬连接a.log.lnk
cat a.log.lnk 直接查看内容
rm -rf a.log 删除源文件
cat a.log.lnk 删除源文件对连接文件没有任何影响
- 压缩解压文件
gzip:
compress or expand files
选项:
-d Decompress 解压缩
具体用法:
gzip tomcat.log 压缩tomcat.log文件 产生一个tomcat.log.gz 源文件消失
gzip -d tomcat.log.gz 解压 压缩包消失,源文件出现
bzip2:
最小化安装,只安装了gzip 没有安装bzip2
在线安装bzip2 压缩解压方法
ping www.baidu.com 看网络是否通畅
yum -y install bzip2 自动分析依赖,在线安装软件bzip2
a block-sorting file compressor,a block-sorting file compressor 压缩解压文件
选项:
-d decompress 解压缩
具体操作:
ls
bzip2 tomcat.log 压缩tomcat.log 产生tomcat.log.bz2 源文件消失
ls
bzip2 -d tomcat.log.bz2 解压 压缩包消失 源文件出现
ls
gzip frameset/
bzip2 frameset/
这两个命令都无法直接对目录进行压缩
- 压缩解压目录/文件 查看压缩包
tar:
GNU `tar' saves many files together into a single tape or disk archive,
and can restore individual files from the archive
把许多文件保存到一起到一个单独磁带或者磁盘档案上,可以从档案中恢复文件,和windows下的压缩解压是一个意思
选项:
-c create 创建 create a new archive 创建压缩
-t list 列表 list the contents of an archive 查看压缩包
-x extract 提取 extract files from an archive 从压缩包提取文件 解压缩
上面3个选项中 都不可以共同使用
-z gzip filter the archive through gzip 通过gzip方式进行过滤(压缩,查看压缩包,解压)
-j bzip2 filter the archive through bzip2 通过bzip2方式进行过滤(压缩,查看压缩包,解压)
上面2个选项不能共同使用(不能压缩,查看或者解压过程中换方式)
-v verbose verbosely list files processed 显示文件详情 可选的,不要也可以
-f file use archive file or device ARCHIVE 文档名称
-C change to directory DIR 改变目录 指定解压文件位置
具体用法:既可以对文件进行操作 ,也可以对目录进行操作(本质tar 打包,打包可以打包目录,在打包过程使用gzip,bzip2进行压缩或者解压)
ls
mkdir frameset
mkdir frameset/spring frameset/springboot frameset/mybatis frameset/hibernate
mkdir frameset/spring/ioc frameset/spring/aop frameset/spring/mvc
touch frameset/spring/ioc/a.log
touch frameset/spring/ioc/b.log
touch frameset/spring/aop/aa.log
touch frameset/spring/aop/bb.log
tar -czvf frameset.tar.gz frameset/ 压缩文件夹frameset,压缩之后的名称为frameset.tar.gz 因为带了v 所以会显示压缩过程
ls 发现和windows下一样,压缩后,源目录依然存在,产生frameset.tar.gz文件
rm -rf frameset.tar.gz 删除,再压缩,演示不带v
tar -czf frameset.tar.gz frameset 依然压缩,不再显示详细信息
ls
tar -tzvf frameset.tar.gz 查看压缩包(带v显示详情)
tar -tjvf frameset.tar.gz 错误 压缩文件使用的gzip方式压缩的,不能使用bzip2的方式进行查看
tar -tzvf frameset.tar.gz 查看压缩包(不带v不显示详情)
ls
rm -rf frameset 删除源目录
tar -xjvf frameset.tar.gz 解压错误 压缩文件使用的gzip方式压缩的,不能使用bzip2的方式进行解压
tar -xzvf frameset.tar.gz 解压 没有-C 默认解压到当前目录
ls
ls /tmp
tar -xzvf frameset.tar.gz -C /tmp/ 解压到指定目录
ls /tmp
ls /tmp/frameset
tar -cjvf frameset.tar.bz2 frameset 使用bzip2方式进行压缩
ls
tar -tjvf frameset.tar.bz2 查看压缩包
rm -rf frameset 删除源目录
ls 查看
tar -xjvf frameset.tar.bz2 解压到当前目录
ls
ls frameset
ls /var
tar -xjvf frameset.tar.bz2 -C /var/ 解压到指定的/var目录
ls /var/
ls /var/frameset/
角色,权限和命令
四种角色:
第1位:文件类型(d 目录,- 普通文件,l 链接文件)
“-”表示普通文件;
“d”表示目录;
“l”表示链接文件;
“p”表示管理文件;
“b”表示块设备文件;
“c”表示字符设备文件;
“s”表示套接字文件;
第2-4位:所属用户权限,用u(user)表示
第5-7位:所属组权限,用g(group)表示
第8-10位:其他用户权限,用o(other)表示
第2-10位:表示所有的权限,用a(all)表示
三种基本权限:
r 读权限(read)
w 写权限(write)
x 执行权限 (execute)
chmod修改文件权限命令(change mode)
参数:-R 下面的文件和子目录做相同权限操作(Recursive递归的)
例如:
chmod u[go/a]+/-x[rw] a.log
touch a.log
[root@basic ~]# chmod u+x a.log
[root@basic ~]# ll a.log
-rwxr--r--. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod g+wx a.log
[root@basic ~]# ll a.log
-rwxrwxr--. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod o+wx a.log
[root@basic ~]# ll a.log
-rwxrwxrwx. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod a-rwx a.log
[root@basic ~]# ll a.log
----------. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod ug+rw a.log
[root@basic ~]# ll a.log
-rw-rw----. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod aug-rw a.log
[root@basic ~]# ll a.log
----------. 1 root root 16 8月 12 16:43 a.log```
三位二进制数授权
使用3位2进制标识十进制的0-8
十进制 三位2进制
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
8 1000
chmod 7 7 7 a.log
111 111 111
2 3 4
010 011 100
-w- -wx r–
5 6 7
101 110 111
r-x rw- rwx
例如:
[root@basic ~]# chmod 777 a.log
[root@basic ~]# ll a.log
-rwxrwxrwx. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod 000 a.log
[root@basic ~]# ll a.log
----------. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod 234 a.log
[root@basic ~]# ll a.log
--w--wxr--. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod 238 a.log
chmod: 无效模式:"238"
Try 'chmod --help' for more information.
[root@basic ~]# chmod 777 a.log
[root@basic ~]# ll a.log
-rwxrwxrwx. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod 234 a.log
[root@basic ~]# ll a.log
--w--wxr--. 1 root root 16 8月 12 16:43 a.log
[root@basic ~]# chmod 567 a.log
[root@basic ~]# ll a.log
完结撒花,see you娜拉