Disk and file management commands
System catalog introduction
/: root directory, generally only directories are stored in the root directory, not files, /etc, /bin, /dev, /lib, /sbin should be placed in the same partition as the root directory /bin(binary): /usr/
bin : Directory of executable binary files, such as common commands ls, tar, mv, cat, etc.
/boot: Place some files used when the linux system starts. /boot/vmlinuz is the kernel file of linux, and /boot/grub. It is recommended to partition separately, the size of the partition is 100M
/dev: store the device files under the linux system, access a file in this directory, which is equivalent to accessing a certain device, the common way is to mount the CD-ROM drive mount /dev/cdrom /mnt.
/etc: The directory where system configuration files are stored. It is not recommended to store executable files in this directory. Important configuration files include /etc/inittab, /etc/fstab, /etc/init.d, /etc/X11, /etc Remember to back up /sysconfig, /etc/xinetd.d before modifying configuration files. Note: /etc/X11 stores settings related to x windows.
/home: The system default (ordinary) user home directory. When adding a new user account, the user's home directory is stored in this directory, indicating the current user's home directory, and test indicates the user test's home directory. It is recommended to partition separately and set a larger disk space to facilitate users to store data
/lib: /usr/lib:/usr/local/lib: The directory of the function library used by the system. During the execution of the program, some additional functions need to be called The parameter needs the assistance of the function library, and the more important directory is /lib/modules.
/mnt: /media:The default mount point of the device, usually the CD is mounted under /mnt/cdrom, it is not necessarily, you can choose any location to mount.
/opt: The directory where additional software is installed for the host. For example: the Fedora community development software used by FC4, if you want to install the new KDE desktop software by yourself, you can install the software in this directory. In the previous Linux system, it was customary to place it in the /usr/local directory
/proc: the data in this directory is in the memory, such as the system core, external devices, and network status. Since the data is stored in the memory, it does not occupy disk space , the more important directory process processes include /proc/cpuinfo, /proc/interrupts, /proc/dma, /proc/ioports, /proc/net/*, etc. /root: The home (root) directory of the system administrator root, the
system The first boot partition is /, so it is best to place /root and / under one partition.
/sbin: /usr/sbin:/usr/local/sbin: Place the executable commands used by the system administrator root, such as fdisk, shutdown, mount, etc. The difference from /bin is that these directories are commands for the system administrator root, and general users can only "view" but cannot set and use them.
/tmp: The directory where general users or programs that are being executed temporarily store files. Anyone can access it. Important data cannot be placed in this directory.
/srv: The data directory that needs to be accessed after the service is started, such as the webpage data that the www service needs to access Stored in /srv/www
/usr:Application storage directory, /usr/bin stores application programs, /usr/share stores shared data, and /usr/lib stores some function library files that cannot be run directly, but are necessary for many programs to run. /usr/local: store software upgrade packages. /usr/share/doc: The directory where the system description files are stored. /usr/share/man: The directory where the program description file is stored. When using man ls, the content of /usr/share/man/man1/ls.1.gz will be queried. It is recommended to partition separately and set a larger disk space /
var: Place the system Files that change frequently during execution, such as log files that change at any time variable /var/log, /var/log/message: directory for storing all login files, /var/spool/mail: directory for storing mail, /var/run : Program or service start
command format
Command [-option] [parameter]
such as: ls -la /usr
Command Category and Help
Internal command: a part of the shell parser (cd pwd), the execution speed will be faster
External command: a file program (ls mkdir) independent of the shell parser, the execution speed will be slow (every time you run an external command, shell parsing is required Load the parser first, then execute)
Internal command: help + command (help cd)
External command: man (manual help manual) + command (man ls)
to view the help document of a command. If the document is long, press the enter key to read it line by line , space (space) key to read the letter q (quit) page by page to exit the help document
Common commands
- view directory contents
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/ 相对路径显示 选项可以随意加
- switch directory
cd Change the shell working directory 改变工作目录
具体使用:
cd /etc/sysconfig/network-scripts/ 切换到指定目录
cd ../ 切换上级目录
cd ../../ 切换到上两级目录
cd 什么都参数都不加 切换到当前用户的根目录
cd /etc/sysconfig/network-scripts/
cd ~ 和上面什么参数都不加一样 切换到当前用户的根目录
- Create a directory
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 正确写法
- Create a file
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
- view file content
cat
cat: concatenate files and print on the standard output Concatenate files and print to standard output, displaying all the contents of a file
简单练习:
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 退出
可以使用 /关键字 进行搜索 高亮关键字
- copy files
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/
- View current working directory
pwd print name of current/working directory 打印当前工作目录的名称
具体用法:
pwd 回车 ,默认显示当前工作目录的位置
cd /etc/sysconfig/network-scripts/
pwd
cd ../
pwd
cd ../../
pwd
- Move files or rename
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
- delete empty directory
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 无法删除
- Delete Files
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
- file statistics
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 a file or directory
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 | 管道符 连接多个命令的 通常后一个命令把前一个命令的执行结果作为参数
- filter file content
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的行并打印,并统计行数
- Create shortcuts (create soft and hard links)
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 删除源文件对连接文件没有任何影响
- Compress and decompress the file
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/
这两个命令都无法直接对目录进行压缩
- Compress and decompress directory/file View compressed package
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/
Roles, Privileges and Commands
Four roles:
Bit 1: file type (d directory, - ordinary file, l link file)
"-" means ordinary file;
"d" means directory;
"l" means link file;
"p" means management file;
"b" means block Device file;
"c" means a character device file;
"s" means a socket file;
2nd-4th digits: user rights, use u (user) to represent
the 5th-7th digits: group rights, use g (group ) to represent
the 8th to 10th digits: other user permissions, represented by o (other)
The 2nd to 10th digits: to represent all permissions, represented by a (all)
Three basic permissions:
r read permission (read)
w write permission (write)
x execute permission (execute)
chmod modify file permission command (change mode)
parameters: -R The following files and subdirectories do the same permission operation (Recursive 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```
Three-digit binary number authorization
Use 3-digit binary to identify decimal 0-8
Decimal three-digit binary
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
Finished flowering, see you Nora