大家好,欢迎来到IT知识分享网。
目录
一、基础命令
cp :复制文件和文件夹的命令
(复制后,自动粘贴)
查看cp命令的用法
[root@lb1 china3]# cp --help 查看cp的使用方法 [root@lb1 china3]# man cp 查看cp命令的手册
1.复制单个文件
文件存放到文件夹(目录)
可用相对路径,也可用绝对路径
[root@lb1 china3]# ls boot hosts hunan liyili.txt passwd [root@lb1 china3]# cp liyili.txt hunan #相对路径 [root@lb1 china3]# ls hunan liyili.txt [root@lb1 china3]# cp /etc/passwd /china3/hunan/ #绝对路径 [root@lb1 china3]# cp /etc/hostname . # .代表当前目录 [root@lb1 china3]# ls boot hostname hosts hunan liyili.txt passwd
1.1复制粘贴重命名一步到位
如果复制的目的地文件或文件夹不存在的话,就是重命名
[root@lb1 china3]# cp liyili.txt hunan/lyl.txt #将当前目录下的liyili.txt复制到hunan文件夹里改名为lyl.txt,因为hunan文件夹里面本来没有lyl.txt,所以为重命名 [root@lb1 china3]# cp passwd sc-pwd #将passwd文件重命名为sc-pwd [root@lb1 china3]# ls boot hostname hosts hunan liyili.txt passwd sc-pwd [root@lb1 china3]# cp hosts sc-hosts [root@lb1 china3]# mkdir /china4 [root@lb1 china3]# cd /china4 [root@lb1 china4]# pwd /china4 [root@lb1 china4]# ls [root@lb1 china4]# cp /etc/ssh/sshd_config . [root@lb1 china4]# ls sshd_config [root@lb1 china4]# cp /etc/hostname sc-name [root@lb1 china4]# ls sc-name sshd_config
2.复制多个文件
前面的都是源文件,最后一个为目的地
[root@lb1 china4]# cp dengchao1.txt dengchao2.txt dengchao3.txt deng #将文件dengchao1.txt dengchao2.txt dengchao3.txt复制到deng文件夹 [root@lb1 china4]# cp dengchao{1..9}.txt deng [root@lb1 china4]# cp dengchao?.txt deng [root@lb1 china4]# cp dengchao*.txt dengchao
3.复制单个文件夹(-r选项)
[root@lb1 china4]# man cp -R, -r, --recursive 递归复制--》每一层子文件夹里的都复制 --》祖祖辈辈 copy directories recursively [root@lb1 china4]# cp dengchao yueyang cp: 略过目录"dengchao" #报错 [root@lb1 china4]# cp -r dengchao yueyang [root@lb1 china4]# cp dengchao yueyang/dc -r #复制粘贴重命名
4.复制多个文件夹或者文件
[root@lb1 china4]# cp deng dengchao yueyang/ sc-name hunan -r #前面为多个源文件和文件夹,最后的为目的地 [root@lb1 china4]# cp dengchao/* hubei -r #复制dengchao文件夹里的所有的文件或者文件夹 到hubei目录下,但是不复制dengchao文件夹
5.复制如何直接覆盖,不给予提醒
[root@lb1 jindafu]# man cp -i, --interactive #交互式--》一问一答 prompt before overwrite (overrides a previous -n option) [root@lb1 jindafu]# alias #系统里已经定义了别名 cp='cp -i' alias cp='cp -i' [root@lb1 jindafu]# cp /etc/passwd . cp:是否覆盖"./passwd"? y [root@lb1 jindafu]# #不想看到交互式的提醒,本质上就是不使用 -i 选项
方法1.取消别名
[root@lb1 jindafu]# unalias cp [root@lb1 jindafu]# cp /etc/passwd . [root@lb1 jindafu]# cp /etc/passwd . [root@lb1 jindafu]# cp /etc/passwd .
方法2.使用cp命令的绝对路径 –》推荐
[root@lb1 jindafu]# which cp /usr/bin/cp [root@lb1 wuxia]# /usr/bin/cp /etc/passwd . [root@lb1 jindafu]# alias cp='cp -i' [root@lb1 jindafu]# which cp alias cp='cp -i' /usr/bin/cp
为什么使用绝对路径就可以绕过别名?
因为shell解释器在进行命令的解析的过程中绕过别名,因为我们没有使用别名,不进行别名的解析
linux里不要被后缀名迷惑了,可以使用file命令查看文件的类型
empty 空文件
directory 目录=文件夹
symbolic link 符号链接=快捷方式
[root@localhost lianxi]# touch weihong.txt [root@localhost lianxi]# file weihong.txt weihong.txt: empty [root@localhost lianxi]# touch weihong.jpg [root@localhost lianxi]# file weihong.jpg weihong.jpg: empty [root@localhost lianxi]# touch weihong.mp3 [root@localhost lianxi]# file weihong.mp3 weihong.mp3: empty [root@localhost lianxi]# touch weihong.mp4 [root@localhost lianxi]# file weihong.mp4 weihong.mp4: empty
[root@lb1 wuxia]# file taishan taishan: directory #目录(文件夹) [root@lb1 wuxia]# file /etc/passwd /etc/passwd: ASCII text
du:统计目录及文件的空间占用情况
du -sh:统计文件的总大小,并以人类看得懂的格式显示
du - estimate file space usage Summarize disk usage of each FILE, recursively for directories. -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -s, --summarize display only a total for each argument [root@lb1 /]# du -sh /usr 1.5G /usr
mv:移动或者重命名文件或文件夹
1.当后面接的文件存在就是移动
可移动单个文件,也移动多个文件
[root@lb1 china5]# mkdir hunan hubei [root@lb1 china5]# ls hubei hunan [root@lb1 china5]# touch shenjiemi lijunlin [root@lb1 china5]# ls hubei hunan lijunlin shenjiemi [root@lb1 china5]# mv shenjiemi hunan #shenjiemi为源文件 hunan为目的地 [root@lb1 china5]# mv lijunlin wang*.txt hunan
2.当后面接的文件不存在就是重命名
[root@lb1 china5]# touch liyongqian [root@lb1 china5]# ls hubei liyongqian beijing [root@lb1 china5]# mv liyongqian liwenqian #将liyongqian重命名为liwenqian [root@lb1 china5]# mv hubei e #将hubei重命名为e [root@lb1 china5]# mv e liwenqian beijing #将e liwenqian 移动到beijing
$1 是位置变量 第1个位置变量
–》作用:就是往脚本里传递参数
[root@lb1 china5]# vim post_var.sh [root@lb1 china5]# cat post_var.sh #!/bin/bash echo "第1个位置变量是 $1" echo "第2个位置变量是 $2" echo "第3个位置变量是 $3" [root@lb1 china5]# bash post_var.sh siyuxiang xiaowei yaolifan #执行脚本往里面传递3个参数 第1个位置变量是 siyuxiang 第2个位置变量是 xiaowei 第3个位置变量是 yaolifan
PATH变量: 定义了linux存放命令的目录
[root@lb1 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sclilin99/sbin:/root/bin [root@lb1 ~]# which mkdir /usr/bin/mkdir [root@lb1 ~]# env 使用env查看PATH变量的定义 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sclilin99/sbin:/root/bin [root@lb1 ~]# cd /china5 [root@lb1 china5]# ls mv_back.sh post_var.sh [root@lb1 china5]# cp mv_back.sh /usr/bin/ #复制mv_back.sh到PATH变量的目录下,让mv_back.sh在任意路径下都能直接使用
/usr --》存放linux下的安装了的程序的目录---》c:\program files unix system resource --》linux 山寨的unix --》linux is very like unix bin -->binary 二进制 linux系统里的绝大多数的命令,都是使用c语言编写--》编译二进制文件 /usr/bin 目录存放了我们很多的基本的linux命令
自己编写回收站功能的脚本 mv_back.sh
1)临时存放文件功能
1.编写脚本mv_back.sh
[root@lb1 china5]# vim mv_back.sh #!/bin/bash #新建回收站目录 mkdir -p /backup #剪切功能 mv $1 /backup
2.授予可执行权限
chmod +x mv_back.sh
3.将脚本复制到PATH变量对应的某一个文件夹
cp mv_back.sh /usr/bin
4.定义别名rm等于mv_back.sh
alias rm='mv_back.sh'
5.直接使用
rm /china2
2)记录当时文件的路径,方便恢复
–》记录当时的文件的路径到一个文件里,在恢复的时候查询使用
重定向
输出重定向 > : 将在屏幕上的输出写到文件里 –》改变了内容的输出方向,将本来往屏幕输出的内容,重新改变方向写到文件里
如果后面接的文件不存在会帮助新建,如果存在就会覆盖原来的内容
追加输出重定向 >> : 将在屏幕上的输出写到文件里
如果后面接的文件不存在会帮助新建,如果存在就不会覆盖原来的内容,只是在文件的末尾追加内容
清空文件内容: >sc.txt
[root@lb1 china5]# echo [root@lb1 china5]# echo >sc.txt [root@lb1 china5]# ls mv_back.sh post_var.sh sc.txt [root@lb1 china5]# echo hunan changsha >sc.txt [root@lb1 china5]# cat sc.txt hunan changsha [root@lb1 china5]# echo hunan changsha >>sc.txt [root@lb1 china5]# echo >>sc.txt [root@lb1 china5]# cat sc.txt hunan changsha hunan changsha [root@lb1 china5]# >sc.txt #清空文件sc.txt里的内容,不删除文件 [root@lb1 china5]# cat mv_back.sh 更新脚本增加记录删除文件的路径的功能 #!/bin/bash #新建回收站目录 mkdir -p /backup #剪切功能 mv $1 /backup #保存位置变量--》文件或者文件夹的路径 echo $1 >>/backup/filepath.txt [root@lb1 china5]# cp mv_back.sh /usr/bin/ 复制新的脚本到/usr/bin目录下 cp:是否覆盖"/usr/bin/mv_back.sh"? y [root@lb1 backup]# cat filepath.txt |grep yunnan #查找包含yunnan的行 /china5/yunnan/
cat :查看文件里的内容
[root@lb1 china5]# man cat cat - concatenate files and print on the standard output 1.concatenate 拼接多个文件 2.输出文件里的内容到屏幕 standard output 标准输出 输出到屏幕 standard input 标准输入 从键盘输入 -n, --number number all output lines
[root@lb1 china5]# cat -n yangyongjie.txt 1 yangyongjie 2 yiyang 3 pangge 4 [root@lb1 china5]# cat sc.txt yangyongjie.txt mv_back.sh >3.txt #将多个文件的内容按照顺序读取(拼接)输出到屏幕 ,然后使用重定向保存到文件3.txt里 [root@lb1 china5]# cat 3.txt xiaowei yangyongjie yiyang pangge #!/bin/bash #新建回收站目录 mkdir -p /backup #剪切功能 mv $1 /backup #保存位置变量--》文件或者文件夹的路径 echo $1 >>/backup/filepath.txt #nl 给输出的内容编号 #nl - number lines of files #nl = cat -n [root@lb1 china5]# nl yangyongjie.txt 1 yangyongjie 2 yiyang 3 pangge
[root@lb1 china5]# vim bigfile.sh [root@lb1 china5]# cat bigfile.sh #!/bin/bash for i in {1..10000} do echo "I am learning linux in sanchuang sc$i" >>/china5/sc.txt done [root@lb1 china5]# bash bigfile.sh #执行脚本 [root@lb1 china5]# cat /china5/sc.txt #查看创建的文件 # 改进版 [root@lb1 china5]# cat bigfile.sh #!/bin/bash mkdir -p /china99 /china99/sc99.txt for i in {1..10000} do echo "I am learning linux in sanchuang sc$i" >>/china99/sc99.txt done [root@lb1 china5]# bash bigfile.sh [root@lb1 china5]# cd /china99 [root@lb1 china99]# ls sc99.txt [root@lb1 china99]# cat sc99.txt [root@lb1 china99]# ls -lh 总用量 392K -rw-r--r-- 1 root root 390K 3月 20 16:46 sc99.txt [root@lb1 china99]# du -sh sc99.txt 392K sc99.txt
head :默认查看文件的前10行
-number
-n number
[root@lb1 china99]# head -n 5 sc99.txt I am learning linux in sanchuang sc1 I am learning linux in sanchuang sc2 I am learning linux in sanchuang sc3 I am learning linux in sanchuang sc4 I am learning linux in sanchuang sc5 [root@lb1 china99]# head -5 sc99.txt I am learning linux in sanchuang sc1 I am learning linux in sanchuang sc2 I am learning linux in sanchuang sc3 I am learning linux in sanchuang sc4 I am learning linux in sanchuang sc5
tail:默认查看文件的尾10行
-number
-n number
[root@lb1 china99]# tail -5 sc99.txt I am learning linux in sanchuang sc9996 I am learning linux in sanchuang sc9997 I am learning linux in sanchuang sc9998 I am learning linux in sanchuang sc9999 I am learning linux in sanchuang sc10000 [root@lb1 china99]# tail -n 5 sc99.txt I am learning linux in sanchuang sc9996 I am learning linux in sanchuang sc9997 I am learning linux in sanchuang sc9998 I am learning linux in sanchuang sc9999 I am learning linux in sanchuang sc10000
显示5~20行
[root@lb1 china99]# head -20 sc99.txt|tail -16
显示第8行
[root@lb1 china99]# head -8 sc99.txt|tail -1 I am learning linux in sanchuang sc8
显示第100行
[root@lb1 china99]# head -100 sc99.txt|tail -1 I am learning linux in sanchuang sc100
more命令
用途:全屏方式分页显示文件内容
交互操作方法:
按Enter键向下逐行滚动
按空格键向下翻一屏、按b键向上翻一屏 back
按q键退出 quit
会自动退出
less命令
sed 可以实现查找功能
显示不连续的行
[root@lb1 china99]# sed -n '1p;3p;6p;9p' sc99.txt I am learning linux in sanchuang sc1 I am learning linux in sanchuang sc3 I am learning linux in sanchuang sc6 I am learning linux in sanchuang sc9
-n 不符合要求的行,不显示
1p 打印第1行 print
; 命令连接符号,先执行前面的命令,然后再执行后面的命令 –》同时在sed里执行多条命令时候,需要接;
显示连续的行
[root@lb1 china99]# sed -n '5,12p' sc99.txt I am learning linux in sanchuang sc5 I am learning linux in sanchuang sc6 I am learning linux in sanchuang sc7 I am learning linux in sanchuang sc8 I am learning linux in sanchuang sc9 I am learning linux in sanchuang sc10 I am learning linux in sanchuang sc11 I am learning linux in sanchuang sc12
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/99882.html
