文章目录
- 文件操作命令
文件操作命令
touch:新建空文件
1.如果文件不存在则新建空文件
2.如果文件存在则更新文件的创建时间
[root@scchen lianxi]# touch weihong.txt
[root@scchen lianxi]# date
2022年 03月 06日 星期日 15:06:49 CST
[root@scchen lianxi]# touch weihong.txt
[root@scchen lianxi]# date
2022年 03月 06日 星期日 15:07:00 CST
[root@scchen lianxi]#
file 查看文件的类型
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
cp:复制 copy
-i, --interactive ==>交互的
prompt before overwrite (overrides a previous -n option)
一、文件的复制
1.单个文件的复制
[root@localhost lianxi]# ls
changsha feng.txt songyuxiao.txt
[root@localhost lianxi]# cp songyuxiao.txt changsha
#源文件 #目的地
#source #destination
[root@localhost lianxi]# ls changsha
songyuxiao.txt
2.多个文件的复制
[root@localhost lianxi]# ls
changsha feng.txt hunan songyuxiao.txt weijunlin.txt
[root@localhost lianxi]# cp feng.txt songyuxiao.txt weijunlin.txt hunan
#前面的3个文件都是源文件,最后一个是目的地文件
[root@localhost lianxi]# ls
changsha feng.txt hunan songyuxiao.txt weijunlin.txt
[root@localhost lianxi]# ls hunan
feng.txt songyuxiao.txt weijunlin.txt
[root@localhost lianxi]# cp *.txt hunan 将所有的.txt结尾的文件复制到hunan
3.文件的复制,粘贴,重命名一步到位
将songyuxiao.txt复制到hunan目录,改名为syx.txt
[root@localhost lianxi]# cp songyuxiao.txt hunan/syx.txt
[root@localhost lianxi]# cp feng.txt hunan/fengdeyong.txt
4.cp命令和绝对路径的使用
[root@localhost lianxi]# cp /etc/passwd . #将/etc/passwd 文件复制到当前目录下
[root@localhost lianxi]# cp /etc/passwd /lianxi/hunan
[root@localhost lianxi]# ls /lianxi/hunan
fengdeyong.txt feng.txt passwd songyuxiao.txt syx.txt weijunlin.txt
二、文件夹的复制
1.单个文件夹的复制
[root@localhost lianxi]# cp -r hunan hubei
# 源文件 目的地
[root@localhost lianxi]# cp changsha hubei -r
2.多个文件夹的复制
[root@localhost lianxi]# cp -r hunan hubei guangdong
# 源文件夹 目的地
[root@localhost lianxi]# ls guangdong/
hubei hunan
3.文件夹的复制,粘贴,重命名一步到位
[root@localhost lianxi]# cp guangdong hunan/yue -r
三、复制如何直接覆盖,不给予提醒
[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
rm -rf:删除文件或文件夹
通配符:通通匹配 --》中
*: 可以表示任意个任意字符
?: 表示一个任意字符
[root@localhost lianxi]# rm -rf * #删除当前目录下的所有的文件和文件夹,但是不会删除隐藏文件和文件夹
[root@localhost lianxi]# ls -a
. .. liuxing .xiongxihao .yibangyou
[root@localhost lianxi]# rm -rf *
[root@localhost lianxi]# ls
[root@localhost lianxi]# ls -a
. .. .xiongxihao .yibangyou
[root@localhost lianxi]# touch liuhongjie{1..100}.txt
#{1..100} 表示1到100的集合
[root@localhost lianxi]# rm -rf liuhongjie*.txt
#删除了liuhongjie{1..100}.txt
[root@localhost lianxi]# rm -rf lilanqing???.txt
#删除了liuhongjie100.txt
[root@localhost lianxi]# rm -rf lilanqing??.txt
#删除了liuhongjie10~99.txt
[root@localhost lianxi]# rm -rf lilanqing?.txt
#删除了liuhongjie1~9.txt
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
ln -s :创建符号链接
[root@localhost lianxi]# ln -s guangdong yue #创建链接文件yue指向guangdong
ln 是==创建链接文件的命令,创建链接文件可以给文件或者文件夹==
-s 创建符号链接(软链接)–》相当于windows里的快捷方式
[root@localhost lianxi]# ln -s hunan xiang
# 源文件 链接文件
[root@localhost lianxi]# ll
total 0
drwxr-xr-x 3 root root 23 Nov 20 15:28 guangdong
drwxr-xr-x 3 root root 22 Nov 13 15:03 hunan
lrwxrwxrwx 1 root root 5 Nov 20 15:34 xiang -> hunan
lrwxrwxrwx 1 root root 10 Nov 20 15:28 yue -> guangdong/
stat:显示文件的状态信息(比ls命令输出更详细)
[root@wh lianxi]# stat scwh.py
File: ‘scwh.py’
Size: 308 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 51181031 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-04-07 19:55:16.809327564 +0800
Modify: 2022-04-07 19:55:16.809327564 +0800
Change: 2022-04-07 19:55:16.905325845 +0800
Birth: -
[root@wh lianxi]# stat wh
stat: cannot stat ‘wh’: No such file or directory
[root@master /]# LANG=en_US.UTF8 #修改当前终端的语言编码为美式英语utf8编码 language English US
#en_US.UTF8 是美式英语
#zh_CN.UTF8 是简体中文的编码
#zh_TW.UTF8 是繁体中文的编码
touch:文件不存在则新建空文件,文件或文件夹存在则更改时间,atime、mtime、ctime都会更改
[root@wh lianxi]# stat scwh.py
File: ‘scwh.py’
Size: 68 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 54947580 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-04-07 19:43:59.861671342 +0800
Modify: 2022-04-07 19:43:59.861671342 +0800
Change: 2022-04-07 19:43:59.861671342 +0800
Birth: -
vim:修改文件前会先查看,接着会更改文件内容,从而更改了文件大小–》三个时间也会改变
[root@wh lianxi]# vim scwh.py
[root@wh lianxi]# stat scwh.py
File: ‘scwh.py’
Size: 308 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 51181031 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-04-07 19:55:16.809327564 +0800
Modify: 2022-04-07 19:55:16.809327564 +0800
Change: 2022-04-07 19:55:16.905325845 +0800
Birth: -
test:测试文件是否存在
[root@master lianxi]# echo $?
1
[root@master lianxi]# [ -f scfenfjkdjfkdg ]
[root@master lianxi]# echo $?
1
[root@master lianxi]# test -f fjdkfjdkjfkd
[root@master lianxi]# echo $?
1
[root@master lianxi]# [ -d scfenfjkdjfkdg ]
[root@master lianxi]# echo $?
1
[root@master lianxi]# [ -d /lianxi ]
[root@master lianxi]# echo $?
0
[root@master lianxi]# [ -f /etc/passwd ]
[root@master lianxi]# echo $?
0
alias:查看命令的别名
[root@localhost zhouyiwei]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
临时定义别名:
alias lk=‘ls -l -a --color=auto’ 临时定义lk是ls -l -a --color=auto的别名
永久定义别名:
vim /root/.bashrc
[root@localhost yum.repos.d]# vim /root/.bashrc
alias lk='ls -a -l -h --color=auto'
alias c='clear'
alias mk='mkdir -p'
[root@localhost yum.repos.d]# source /root/.bashrc #执行/root/.bashrc脚本
unalias :取消定义别名
unalias mkdir 取消定义的别名mkdir