Linux运维学习

Linux——文件和目录操作

2023-12-18  本文已影响0人  So_ProbuING

pwd:显示当前所在位置

cd:切换目录

tree:以树形结构显示目录下的内容

mkdir 创建目录

mkdir [选项] [目录]

[root@linuxprobe probuing]# mkdir -pv ./{dir1_1,dir1_2}/{dir2_1,dir2_2}
mkdir: created directory './dir1_1'
mkdir: created directory './dir1_1/dir2_1'
mkdir: created directory './dir1_1/dir2_2'
mkdir: created directory './dir1_2'
mkdir: created directory './dir1_2/dir2_1'
mkdir: created directory './dir1_2/dir2_2'
[root@linuxprobe probuing]# tree -d
.
|-- dir1_1
|   |-- dir2_1
|   `-- dir2_2
|-- dir1_2
|   |-- dir2_1
|   `-- dir2_2
`-- test

touch:创建空文件或改变文件的时间属性

touch 选项 文件

ls 显示目录下的内容及相关属性信息

ls 选项 文件或目录

cp:复制文件或目录

cp 选项 源文件 目标文件

mv:移动或重命名文件

mv 选项 源文件 目标文件

rm 删除文件或目录

rm 选项 文件或目录

rmdir 删除空目录

rmdir命令用于删除空目录,当目录不为空时,命令不起作用

rmdir 选项 目录

ln:硬链接与软链接

ln 创建文件间的链接,包括硬链接和软链接

ln 选项 源文件或目录 目标文件或目录

硬链接

硬链接是指通过索引节点来进行链接。多个文件名指向同一个索引节点时正常且允许的,这种情况下的文件就是硬链接。硬链接文件相当于文件的另外一个入口

# 创建硬链接并查看硬链接文件内容
[root@linuxprobe lntest]# ln lnfile.txt lnfile_hard_link
[root@linuxprobe lntest]# cat lnfile_hard_link 
123
#删除源文件
[root@linuxprobe lntest]# rm -rf lnfile.txt 
# 查看硬链接文件 硬链接文件正常
[root@linuxprobe lntest]# cat lnfile_hard_link 
123
# 重新链接文件
[root@linuxprobe lntest]# ln lnfile_hard_link a.txt
# 查看源文件
[root@linuxprobe lntest]# cat a.txt 
123


软链接

ln -s 源文件 目标文件

readlink 查看符号链接文件的内容

readlink能够帮助我们查看符号链接文件的内容

readlink [选项] [文件]

find查找目录下的文件

find [选项]  [路径] [操作语句]

用-name指定关键字查找

[root@linuxprobe lntest]# find /var/log/ -name '*.log'

xargs 将标准输入转换成命令行参数

xargs命令是向其他命令传递命令行参数的一个过滤器,能够将管道或者标准输入传递的数据转换成xargs命令后跟随的命令的命令行参数

xargs [选项]
[root@linuxprobe lntest]# cat test.txt 
123\n234\n345\n456
321
432
543
   654    789
[root@linuxprobe lntest]# xargs < test.txt 
123n234n345n456 321 432 543 654 78
[root@linuxprobe lntest]# xargs -n 3 < test.txt 
123n234n345n456 321 432
543 654 789

结合find使用xargs的案例

[root@linuxprobe tmp]# touch hello\ everyone.txt

rename 重命名文件

rename 通过字符串替换的方式批量修改文件名
语法格式:

rename from to file

basename 显示文件名或目录名

basename命令用于显示去除路径和文件后缀部分的文件名或目录
语法格式

basename name suffix
[root@linuxprobe ~]# basename /data/dir/file1.txt 
file1.tx

dirname 显示文件或目录路径

[root@linuxprobe ~]# dirname /data/dir/file1.txt 
/data/dir
[root@linuxprobe ~]# cd /data/dir/
[root@linuxprobe dir]# ls
file1.txt
[root@linuxprobe dir]# dirname file1.txt 
.

chattr 改变文件的扩展属性

chattr用于改变文件的扩展属性,与chmod这个命令相比,chmod只是改变文件的读、写、执行,更底层的属性控制是由chattr来改变的

chattr [选项] [模式] [<文件或目录>]
[root@linuxprobe dir]# chattr +a test  添加追加属性
[root@linuxprobe dir]# lsattr test  
-----a-------------- test
[root@linuxprobe dir]# rm -rf test  root用户也无法删除,只能追加
rm: cannot remove 'test': Operation not permitted
[root@linuxprobe dir]# echo test >> test 
[root@linuxprobe dir]# cat test 
test
[root@linuxprobe dir]# echo 222 > test  无法清空文件
-bash: test: Operation not permitted
[root@linuxprobe dir]# chattr +i test 
[root@linuxprobe dir]# lsattr test 
----i--------------- test
[root@linuxprobe dir]# vim test

lsattr 查看文件扩展属性

lsattr 选项 文件或目录

file显示文件的类型

file显示文件的类型

[root@linuxprobe data]# file dir/file1.txt 
dir/file1.txt: empty
[root@linuxprobe data]# file dir/test 
dir/test: ASCII text

md5sum 计算和校验文件的MD5值

md5sum命令用于计算和校验文件的MD5值。

md5sum 选项 文件
[root@linuxprobe data]# md5sum dir/test
d8e8fca2dc0f896fd7cb4cb0031ba249  dir/test

chown 改变文件或目录的用户和用户组

chown [选项] [用户:用户组] [<文件或目录>]
[root@linuxprobe dir]# ll
total 4
-rw-r--r--. 1 root root 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root root 5 Dec 19 09:00 test
[root@linuxprobe dir]# chown linuxcool:linuxcool file1.txt 
[root@linuxprobe dir]# ll
total 4
-rw-r--r--. 1 linuxcool linuxcool 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root      root      5 Dec 19 09:00 tes

chmod改变文件或目录权限

chmod命令是用来改变文件或目录权限的命令,只有文件的属主和超级用户root才能够执行该命令

chmod [选项] [模式] [<文件或目录>]
权限位 全称 含义 对应数字
r read 可读 4
w write 可写 2
x execute 可执行 1
- 没有任何权限 0
备注 一些特殊权限位:t、T、s、S、X、x
用户类型 其他用户:o 所有:a,等效于u、g、o的总和
操作字符 +:加入 -:减去 =:设置
[root@linuxprobe dir]# ll
total 4
----------. 1 linuxcool linuxcool 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root      root      5 Dec 19 09:00 test
[root@linuxprobe dir]# chmod u+x file1.txt 
[root@linuxprobe dir]# ll
total 4
---x------. 1 linuxcool linuxcool 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root      root      5 Dec 19 09:00 test
[root@linuxprobe dir]# chmod g+w file1.txt 
[root@linuxprobe dir]# ll
total 4
---x-w----. 1 linuxcool linuxcool 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root      root      5 Dec 19 09:00 test
[root@linuxprobe dir]# chmod o+r file1.txt 
[root@linuxprobe dir]# ll
total 4
---x-w-r--. 1 linuxcool linuxcool 0 Dec 19 08:51 file1.txt
-rw-r--r--. 1 root      root      5 Dec 19 09:00 test

Linux读、写、执行权限说明

普通文件的权限说明

  1. 文件本身要能够执行
  2. 普通用户必须还要有r权限才能够执行,无r就不能执行
  3. root没有r权限 只要有x权限就可以执行
  4. root用户位没有执行权限,但只要其他权限位还有x权限,就能执行

Linux目录的读、写、执行权限说明

chgrp 更改文件用户组

chgrp只能更改文件的用户组,功能已经被chown取代

umask 显示或设置权限掩码

umask是通过八进制的数值来定义用户创建文件或目录的默认权限

umask [参数] [模式]

通过umask计算文件目录权限

上一篇 下一篇

猜你喜欢

热点阅读