mv 可以实现2个功能:
1.移动文件 —》将文件夹或者文件从一个地方移动到另外一个地方 --》剪切
后面接的文件夹如果存在,就是移动
2.重命名文件
后面接的文件夹如果不存在,就是重命名
[root@localhost lianxi]# ls
guangdong guangxi hunan
[root@localhost lianxi]# mv guangdong hunan 移动文件夹
源 目的地
[root@localhost lianxi]# ls
guangxi hunan
[root@localhost lianxi]# tree
.
├── guangxi
└── hunan
└── guangdong
3 directories, 0 files
[root@localhost lianxi]#
[root@localhost lianxi]# mv zhang.txt hunan 移动文件
[root@localhost lianxi]# ls
guangxi hunan
[root@localhost lianxi]# mv guangxi hunan
[root@localhost lianxi]# ls
hunan
[root@localhost lianxi]# tree
.
└── hunan
├── guangdong
├── guangxi
└── zhang.txt
3 directories, 1 file
[root@localhost lianxi]#
[root@localhost lianxi]# mv hunan xiang 将hunan改名为xiang
[root@localhost lianxi]# ls
xiang
[root@localhost lianxi]# tree
.
└── xiang
├── guangdong
├── guangxi
└── zhang.txt
3 directories, 1 file
[root@localhost lianxi]#
1.移动单个文件和文件夹
2.移动多个文件和文件夹
[root@localhost lianxi]# touch chenyamin{1..10}
[root@localhost lianxi]# ls
chenyamin1 chenyamin10 chenyamin2 chenyamin3 chenyamin4 chenyamin5 chenyamin6 chenyamin7 chenyamin8 chenyamin9 xiang
[root@localhost lianxi]# mv chenyamin* xiang 将所有chenyamin开头的文件移动到xiang目录里
[root@localhost lianxi]# ls
xiang
[root@localhost lianxi]# tree xiang
xiang
├── chenyamin1
├── chenyamin10
├── chenyamin2
├── chenyamin3
├── chenyamin4
├── chenyamin5
├── chenyamin6
├── chenyamin7
├── chenyamin8
├── chenyamin9
├── guangdong
├── guangxi
└── zhang.txt
2 directories, 11 files
[root@localhost lianxi]#
[root@localhost lianxi]# mkdir henan hebei
[root@localhost lianxi]# ls
hebei henan xiang
[root@localhost lianxi]# mv henan hebei xiang 将henan和hebei都移动到xiang文件夹里
[root@localhost lianxi]# ls
xiang
[root@localhost lianxi]#
mv 也是非常危险的命令,因为背后把文件挪走了,导致原来的位置没有文件
移动粘贴重命名
[root@localhost lianxi]# mv shanxi xiang/jin 将shanxi移动到xiang目录下改名为jin
练习5
1.在根目录下新建hunantv目录
2.然后在hunantv目录里新建hejiong、wanghan、xiena、haiquan
3.在haiquan目录下新建singer目录,singer目录下,新建yangzongwei、qiqin、linzhixuan空文件
4.将/etc/passwd文件复制到wanghan目录下
5.将xiena目录改名为xienana
6.复制xienana目录到haiquan目录下
7.将singer目录下的所有文件复制到hejiong目录下
[root@localhost singer]# cp * /hunantv2/hejiong/
8.将yangzongwei空文件复制到wanghan目录下改名为taiwan-zongwei
[root@localhost singer]# cp yangzongwei /hunantv2/wanghan/taiwan-zongwei
9.移动hejiong目录到haiquan目录下
10.删除所有的xienana目录
11.查看hunantv的目录结构,查看hunantv的文件类型
cat
[root@sanchuang hunantv]# cd /lianxi
[root@sanchuang lianxi]# cp /etc/hosts .
[root@sanchuang lianxi]# cat hosts -n -n选项可以查看文件的行号
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@sanchuang lianxi]#
cat 可以拼接查看多个文件的内容 但不会改变文件本身的内容
[root@localhost lianxi]# cat hosts passwd >chen.txt
[root@localhost lianxi]# ls
chen.txt hosts passwd xiang
[root@localhost lianxi]# cat chen.txt
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
重定向:可以起到保存文本内容的作用
> 输出重定向:作用将本来在屏幕上输出的内容,转移(重定向)存储到文件里
linux 系统里默认的输出方向是屏幕 标准的输出方向 --》改变了它的输出方向 --》重定向:重新定义了输出的方向
>> 追加输出重定向: 作用将本来在屏幕上输出的内容,转移(重定向)存储到文件里,不覆盖原来的内容,只是在文件的末尾追加
如果文件存在,就在末尾追加,如果文件不存在, 就新建
[root@sanchuang lianxi]# echo 12345
12345
[root@sanchuang lianxi]# echo 12345 >sc.txt
[root@sanchuang lianxi]# ls
12-15 access.log email.txt hunan ip.txt python-test sc.txt
a.bash changsha.txt hosts import-test passwd sc
[root@sanchuang lianxi]# cat sc.txt
12345
[root@sanchuang lianxi]# echo zhangshaowei >sc.txt
[root@sanchuang lianxi]# cat sc.txt
zhangshaowei
[root@sanchuang lianxi]# echo songzhiqiang >>sc.txt
[root@sanchuang lianxi]# echo liuhuichao >>sc.txt
[root@sanchuang lianxi]# cat sc.txt
zhangshaowei
songzhiqiang
liuhuichao
[root@sanchuang lianxi]#
here document 生成指定内容的文档 —》用在编写shell脚本
<< 追加输入重定向
EOF 输入的结束标志 end of file
[root@localhost lianxi]# cat >songzhiqiang.txt <SONG ZHI QIANG > 东北 吉林 长白山 > changsha > 男 > 22 > EOF [root@localhost lianxi]# ls chen.txt hosts new_passwd passwd passwd2 passwd3 sc.txt songzhiqiang.txt wang.txt xiang zql [root@localhost lianxi]# cat songzhiqiang.txt SONG ZHI QIANG 东北 吉林 长白山 changsha 男 22 [root@localhost lianxi]# [root@localhost lianxi]# cat >huangzhiqiang.txt < huang zhi qiang > 湖南 永州 祁阳 > changsha > linux > END > EOF > end [root@localhost lianxi]# [root@localhost lianxi]# cat huangzhiqiang.txt huang zhi qiang 湖南 永州 祁阳 changsha linux END EOF [root@localhost lianxi]# vim hejin.sh #!/bin/bash #产生一个文件/lianxi/hejin.txt ,里面的内容如下: cat >/lianxi/hejin.txt < tac 逆序输出 从最后一行倒着显示文件的全部内容 [root@localhost lianxi]# tac songzhiqiang.txt 22 男 changsha 东北 吉林 长白山 SONG ZHI QIANG [root@localhost lianxi]# [root@localhost lianxi]# tac songzhiqiang.txt >song.txt [root@localhost lianxi]# cat song.txt 22 男 changsha 东北 吉林 长白山 SONG ZHI QIANG [root@localhost lianxi]#分页显示: more
less[root@localhost lianxi]# more /etc/ssh/sshd_config 1. 回车 :一次显示一行 2. 空格 :一次显示一页 --》下一页 3. b :上一页 back 4. q 退出 quit如何产生一个1亿行的文本文件? welcome to sanchuang we are study cat and more 分析: 使用什么方法生成一个文件sc.txt --> 重定向 >> 1亿行 --》for 需要多少时间? 问题: 到底打开一个大的文本文件,使用cat命令好,还是使用more命令好? cat命令消耗内存比较多,more消耗内存比较少 cat查看大文件的时候,消耗的cpu并不是特别多,消耗的内存也不是特别多,内存的消耗确实是慢慢的增多,时间久了会导致消耗比较多的内存 [root@localhost lianxi]# [root@localhost lianxi]# vim bigfile.sh #!/bin/bash #产生1亿行的内容 for i in {1..100000000} do echo "$i welcome to sanchuang ,we are now learning cat and more , we want to know what their diffrence " >>linux.txt done [root@localhost lianxi]# time bash bigfile.sh [root@localhost lianxi]# time bash bigfile.sh 已杀死 real 1m32.047s user 0m16.528s sys 0m42.085s [root@localhost lianxi]# 为什么我们的程序在运行的时候被杀死? 1.占内存太多 [root@localhost lianxi]# cat bigfile.sh #!/bin/bash #产生100万行的内容 for i in {1..1000000} do echo "$i welcome to sanchuang ,we are now learning cat and more , we want to know what their diffrence " >>linux.txt done [root@localhost lianxi]# [root@localhost lianxi]# du -sh linux.txt 128M linux.txt 重定向,快速生成一个大文件 [root@localhost lianxi]# cat linux.txt >>zhangshaowei.txt [root@localhost lianxi]# cat linux.txt >>zhangshaowei.txt [root@localhost lianxi]# cat linux.txt >>zhangshaowei.txt [root@localhost lianxi]# cat linux.txt >>zhangshaowei.txt [root@localhost lianxi]# cat linux.txt >>zhangshaowei.txt [root@localhost lianxi]# du -sh zhangshaowei.txt 2.0G zhangshaowei.txt [root@localhost lianxi]# [root@localhost lianxi]# free -m 查看内存的大小 total used free shared buff/cache available Mem: 1828 177 70 1 1580 1491 Swap: 2047 98 1949 [root@localhost lianxi]# cpu和内存的资源消耗 --》判断这台机器是否繁忙--》卡 top :动态的显示cpu、内存和进程的信息 按q退出 [root@localhost lianxi]# top 查看系统资源的消耗情况 按q退出 清除内存里的缓存里的数据 echo 3 > /proc/sys/vm/drop_caches [root@localhost lianxi]# du -sh zhangshaowei.txt 3.4G zhangshaowei.txt [root@localhost lianxi]# 文件有3.4G,而我们的内存只有2G,为什么cat命令在查看这个文件的时候,没有把内存全部消耗完? cat查看大文件的时候,消耗的cpu并不是特别多,消耗的内存也不是特别多,内存的消耗确实是慢慢的增多,时间久了会导致消耗比较多的内存 [root@localhost lianxi]# free -m 显示内存的大小 total used free shared buff/cache available Mem: 1828 212 85 2 1531 1459 Swap: 2047 86 1961 [root@localhost lianxi]# total 总的大小 used 使用了的大小 free 真正没有使用的空间 --》剩余的空间 shared 共享的内存空间: 大家都可以使用的空间 buff/cache 缓存的空间: 大家都可以去使用的空间 --》临时用来存放数据 available 可用的空间: free + buff/cache里的可用空间 严格上只看free 是否还有空间 =============== OOM out of memory 内存溢出 当一个程序需要的内存空间超过我们的内存的大小的时候,内核会杀死这个程序,导致程序运行终止 注意:不要使用vim打开大文件,会消耗非常多的内存和cpu,会导致死机



