Linux运维基础命令-文件管理类
1、basename
从文件名中去掉路径
# 去掉路径 # basename /var/log/yum.log yum.log # 去掉路径,并且去掉后缀.log # basename /var/log/yum.log .log yum # 指定需要去掉的后缀.log # basename -s .log /var/log/yum.log yum # 处理多个参数,如果不加-a只能处理一个参数 # basename -a /etc/sysconfig/network-scripts/ifcfg-eno16777984 /var/log/yum.log ifcfg-eno16777984 yum.log # 输出后不换行 # basename -z /var/log/yum.log yum.log
2、cat
打印文档到指定输出(屏幕或指定文件)
测试文档如下:
# test1.txt This is the first text. this is a TAB.
this is a space line.
语法命令如下:
# 输出行号,忽略空行 # cat -b test1.txt 1 This is the first text. 2 this is a TAB. 3 4 this is a space line. # 行尾打印$符号 # cat -E test1.txt This is the first text.$ this is a TAB.$ $ $ this is a space line.$ $ # 输出行号,不忽略空行 # cat -n test1.txt 1 This is the first text. 2 this is a TAB. 3 4 5 this is a space line. 6 # 重复空行只打印一行 # cat -s -n test1.txt 1 This is the first text. 2 this is a TAB. 3 4 5 this is a space line. 6 # TAB使用^I进行代替 # cat -T test1.txt This is the first text. ^Ithis is a TAB. this is a space line. # 相当于vET # cat -A test1.txt This is the first text.$ ^Ithis is a TAB.$ $ $ this is a space line.$ $ # 相当于vE # cat -e test1.txt This is the first text.$ this is a TAB.$ $ $ this is a space line.$ $ # 相当于vT # cat -t test1.txt This is the first text. ^Ithis is a TAB. this is a space line.
注意,如果没有跟文件名,或者文件为-,这读取标准输入的结果
其他用法:
# 将test1.txt的内容加上行号写入test2.txt # cat -n test1.txt > test2.txt # cat test2.txt 1 This is the first text. 2 this is a TAB. 3 4 5 this is a space line. 6 #将test1.txt和test2.txt都输入test3.txt # cat test1.txt test2.txt > test3.txt # cat test3.txt This is the first text. this is a TAB. this is a space line. 1 This is the first text. 2 this is a TAB. 3 4 5 this is a space line. 6
3、cd
切换目录
用法:
cd [目标目录]
4、chgrp
更改文件或目录的属组
# 更改属组信息 # ll test1.txt -rw-r--r--. 1 root root 81 11月 30 15:12 test1.txt # chgrp games test1.txt # ll test1.txt -rw-r--r--. 1 root games 81 11月 30 15:12 test1.txt # 更改属组信息,有变化时输出提示 # chgrp -c mail test1.txt changed group of "test1.txt" from games to mail # ll test1.txt -rw-r--r--. 1 root mail 81 11月 30 15:12 test1.txt # chgrp -c mail test1.txt # 不提示大部分错误消息 # chgrp -f games test1.txt # 对每个文件都做出提示 # chgrp -v mail test1.txt test2.txt changed group of "test1.txt" from games to mail changed group of "test2.txt" from root to mail # chgrp -v mail test1.txt test2.txt "test1.txt" 的所属组已保留为mail "test2.txt" 的所属组已保留为mail # 影响连接符号的引用,但不影响连接符号本身 # ln -s test1.txt te1 # ll te1 test1.txt lrwxrwxrwx. 1 root root 9 12月 1 09:43 te1 -> test1.txt -rw-r--r--. 1 root mail 81 11月 30 15:12 test1.txt # chgrp --dereference games te1 # ll te1 test1.txt lrwxrwxrwx. 1 root root 9 12月 1 09:43 te1 -> test1.txt -rw-r--r--. 1 root games 81 11月 30 15:12 test1.txt # 影响链接符号本身,不影响引用 # chgrp -h mail te1 # ll te1 test1.txt lrwxrwxrwx. 1 root mail 9 12月 1 09:43 te1 -> test1.txt -rw-r--r--. 1 root games 81 11月 30 15:12 test1.txt # 将指定目录或文件属组设置为和参考文件相同 # ll test1.txt test2.txt -rw-r--r--. 1 root mail 81 11月 30 15:12 test1.txt -rw-r--r--. 1 root root 123 11月 30 15:21 test2.txt # chgrp --reference=test2.txt test1.txt # ll test1.txt test2.txt -rw-r--r--. 1 root root 81 11月 30 15:12 test1.txt -rw-r--r--. 1 root root 123 11月 30 15:21 test2.txt # 递归处理 # chgrp -R games ../ # ll -d .. dr-xr-x---. 5 root games 4096 12月 1 09:49 .. # ll 总用量 12 lrwxrwxrwx. 1 root games 9 12月 1 09:43 te1 -> test1.txt -rw-r--r--. 1 root games 81 11月 30 15:12 test1.txt -rw-r--r--. 1 root games 123 11月 30 15:21 test2.txt -rw-r--r--. 1 root games 204 11月 30 15:23 test3.txt
5.chmod
更改文件或目录的权限
# 更改权限,和-v类似,但是只返回更改的提示 # chmod -c 755 test1.txt mode of "test1.txt" changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x) # ll test1.txt -rwxr-xr-x. 1 root games 81 12月 2 08:51 test1.txt chmod -c 755 test1.txt # ll test1.txt -rwxr-xr-x. 1 root games 81 12月 2 08:51 test1.txt # 屏蔽大部分报错信息 # chmod -f 522 test1.txt # ll test1.txt -r-x-w--w-. 1 root games 81 12月 2 08:51 test1.txt # 更改权限,返回提示信息 # chmod -v 522 test1.txt "test1.txt" 的权限模式保留为0522 (r-x-w--w-) # chmod -v 755 test1.txt mode of "test1.txt" changed from 0522 (r-x-w--w-) to 0755 (rwxr-xr-x) # 更改为参考文件或目录的权限 # chmod -v --reference test1.txt test2.txt mode of "test2.txt" changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x) # 递归更改,需要注意的是,连接文件并不会被改变权限 # chmod -v -R 664 ./ mode of "./" changed from 0755 (rwxr-xr-x) to 0664 (rw-rw-r--) mode of "./test2.txt" changed from 0755 (rwxr-xr-x) to 0664 (rw-rw-r--) mode of "./test3.txt" changed from 0644 (rw-r--r--) to 0664 (rw-rw-r--) 符号链接"./te1" 和该链接所指示的对象都未被更改 mode of "./test1.txt" changed from 0755 (rwxr-xr-x) to 0664 (rw-rw-r--) # ll -d . drw-rw-r--. 2 root games 64 12月 2 09:08 . # ll 总用量 12 lrwxrwxrwx. 1 root games 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 root games 81 12月 2 08:51 test1.txt -rw-rw-r--. 1 root games 123 12月 2 08:52 test2.txt -rw-rw-r--. 1 root games 204 12月 2 08:52 test3.txt
6、chown
变更晚间或目录的拥有者或所属群组
# 只更改所有者 # ll test1.txt -rw-rw-r--. 1 root games 81 12月 2 08:51 test1.txt # chown games test1.txt # ll test1.txt -rw-rw-r--. 1 games games 81 12月 2 08:51 test1.txt #只更改所属组 # chown :mail test1.txt # ll test1.txt -rw-rw-r--. 1 games mail 81 12月 2 08:51 test1.txt # 只报告更改的部分 # chown -c root:mail test1.txt changed ownership of "test1.txt" from games:mail to root:mail # 屏蔽大部分错误 # chown -f root:root test1.txt # ll test1.txt -rw-rw-r--. 1 root root 81 12月 2 08:51 test1.txt # 每次操作都输出信息 # chown -v mail:mail test1.txt changed ownership of "test1.txt" from root:root to mail:mail # ll test1.txt -rw-rw-r--. 1 mail mail 81 12月 2 08:51 test1.txt # chown -v mail:mail test1.txt "test1.txt" 的所有者已保留为mail:mail # 影响连接文件所连接的文件或目录(默认) # ll te1 test1.txt lrwxrwxrwx. 1 root games 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 mail mail 81 12月 2 08:51 test1.txt # chown games:games te1 # ll te1 test1.txt lrwxrwxrwx. 1 root games 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 games games 81 12月 2 08:51 test1.txt # 影响连接文件本身 # chown -h mail:mail te1 # ll te1 test1.txt lrwxrwxrwx. 1 mail mail 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 games games 81 12月 2 08:51 test1.txt # 设置为和参考目录相同的拥有者和所属组 ll test1.txt test2.txt -rw-rw-r--. 1 games games 81 12月 2 08:51 test1.txt -rw-rw-r--. 1 root games 123 12月 2 08:52 test2.txt # chown --reference=test2.txt test1.txt # ll test1.txt test2.txt -rw-rw-r--. 1 root games 81 12月 2 08:51 test1.txt -rw-rw-r--. 1 root games 123 12月 2 08:52 test2.txt # 所有者和属组匹配时才进行更改 # chown mail:mail --from=root:root test1.txt # ll test1.txt -rw-rw-r--. 1 root games 81 12月 2 08:51 test1.txt # chown mail:mail --from=root:games test1.txt # ll test1.txt -rw-rw-r--. 1 mail mail 81 12月 2 08:51 test1.txt # 递归更改 # ll . 总用量 12 lrwxrwxrwx. 1 mail mail 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 mail mail 81 12月 2 08:51 test1.txt -rw-rw-r--. 1 root games 123 12月 2 08:52 test2.txt -rw-rw-r--. 1 root games 204 12月 2 08:52 test3.txt # ll -d . drw-rw-r--. 2 root games 64 12月 2 09:08 . # chown -R root:root . # ll -d . drw-rw-r--. 2 root root 64 12月 2 09:08 . # ll . 总用量 12 lrwxrwxrwx. 1 root root 9 12月 2 08:47 te1 -> test1.txt -rw-rw-r--. 1 root root 81 12月 2 08:51 test1.txt -rw-rw-r--. 1 root root 123 12月 2 08:52 test2.txt -rw-rw-r--. 1 root root 204 12月 2 08:52 test3.txt
7、cp
复制文件或目录。指定多个文件或目录时,最后一个参数必须是目录,之前所有的内容都会复制到最后参数的目录。
# 和-dR相同 # mkdir cpdir # ll test1.txt -rw-rw-r--. 1 root root 81 12月 2 08:51 test1.txt # cp test1.txt cpdir/cptest1.txt # ll cpdir/cptest1.txt -rw-r--r--. 1 root root 81 12月 6 09:09 cpdir/cptest1.txt # cp -a test1.txt cpdir/ # ll cpdir/test1.txt -rw-rw-r--. 1 root root 81 12月 2 08:51 cpdir/test1.txt # 只复制属性,不复制数据 # cp --attributes-only test1.txt cpdir/cptest2.txt # ll cpdir/cptest2.txt -rw-r--r--. 1 root root 0 12月 6 09:11 cpdir/cptest2.txt # cat cpdir/cptest2.txt
8、cut
显示每行从开头算起num1到num2的文字。
# 事例文件 # cat example.txt test use this is a test file. # 获得从0到12的字符,会进行提示这条命令有误 # cut -c0-12 example.txt cut: 序号从1 开始计数 Try 'cut --help' for more information. # 更改为从1开始可以正常读取 [root@localhost ~]# cut -c1-12 example.txt test use this is a te # 也可以从只获得中间的部分 [root@localhost ~]# cut -c5-12 example.txt use is a te # 超过之后会显示至末尾 [root@localhost ~]# cut -c5-20 example.txt use is a test file.

浙公网安备 33010602011771号