RH124 第二章 Linux下的文件管理
1.文件管理命令
-
touch #新建文件,修改文件时间戳
·touch file1 file2… ##建立空文件
·touch file ##把文件时间同步到当前时间
·touch -t YYYYMMDDHHmm file ##把文件时间更改到指定时间 -
mkdir (make directory)
·mkdir dir1 dir2 … ##建立空目录
·mkdir -p test/redhat/linux ##建立多层目录
[westos@localhost Desktop]$ mkdir mkdir1 mkdir2
[westos@localhost Desktop]$ mkdir -p test/lunux/jml/jk
- rm #删除文件或者目录
·rm file file1 … ##删除文件有提示
·rm -f file ##强制删除文件不提示
·rm -r dir dir1… ##删除目录
·rm -rf dir (用的最多)
[westos@localhost Desktop]$ rm -fr test
- gedit#图形的文本编辑器
·gedit file file1… ##编辑file file1
·#gedit 在使用时必须有图形(会自动弹出文件编辑界面,必须输入文字)
·#file file1可以不存在 - vim #文本编辑器
·vim file ##进入到vim的浏览模式
·##浏览模式不能编辑文件
·##在浏览模式中按<i>进入插入模式
·#在vim中鼠标不能控制光标用上下左右移动光标
·#按<ESC>退出插入模式
·#按:wq保存更改 - vim异常退出
·##当vim编写文件时在未保存更改内容被强制关闭
·##vim出现异常
·#[O] 只读打开文件
·#(E) 继续编辑
·#(R)恢复未保存内容编辑
·#(D) 删除.swp文件进入编辑
·#(Q) 退出当前文件编辑
·#(A) 推出vim程序 - vim同时打开多个文集时
·vim -o westos1 westos2 ##上下模式打开两个文件
·##<ctrl>+<w> <上|下>来转换控制窗口
·vim -p westos1 westos2 ##左右打开两个不同文件
·##:tabn 进入到下一个窗口
·##:tabp 进入到上一个窗口
[westos@localhost ~]$ vim -o westos5 westos6
2 files to edit
[westos@localhost ~]$ cat westos5
djsklsjdljdh
[westos@localhost ~]$ cat westos6
idsakjdhl:
[westos@localhost Desktop]$ vim -p westos1 westos2
2 files to edit
[westos@localhost Desktop]$ cat westos1
qdnskaaan
[westos@localhost Desktop]$ cat westos2
hkjkw
- cat##显示文件的全部内容
·cat file
·cat -b file ##显示文件内容并显示行号,不显示空行的行号
·cat -n file ##显示文件内容并显示行号
·实验素材
··history > westos ##把history的执行结果存放到westos中
- head ##查看文件前多少行
·head westos ##默认显示文件前10行
·head -n westos ##n 代表数字 显示文件的前n行
[westos@localhost Desktop]$ vim westos
[westos@localhost Desktop]$ head westos
sjdlk
adshk
asdk
sadkhid
Djkals
[westos@localhost Desktop]$ head -2 westos
sjdlk
adshk
- tail ##查看文件的后多少
·tail westos ##默认显示文件后10行
·tail -n westos ##n 代表数字 显示文件的后n行
[westos@localhost ~]$ history > westos
[westos@localhost ~]$ tail -5 westos
126 cat westos5
127 cat westos6
128 history > westos
129 cat westos
130 history > westos
- less ##分页浏览
·##less westos 进入到less模式查看westos
·#less模式中 <上><下> 逐行查看文件内容
·#<pgup><pgdn> 逐页查看文件内容
·#/关键字子 搜索关键字 n 向下匹配 N 向上匹配
·# 有less模式进入到vim 编辑完成:wq退出返回less
·#在less中退出
** - cp ##复制文件
·cp 文件 目的地文件
·cp 文件1 文件2 目的地目录(将多个文件同时复制到目录2下)
·cp -r 目录1 目录2 (将目录中包包括的子目录复制到目录2 下)
·#复制原理: #按照源文件建立新文件(抄作业)
[westos@localhost ~]$ cp /home/westos/Desktop/westosb /home/westos/Desktop/mkdir1
[westos@localhost ~]$ cd -
/home/westos
[westos@localhost ~]$ cd Desktop
[westos@localhost Desktop]$ cp westos1 mkdir1
- mv##移动文件
·mv file file1 dir
·mv dir dir1 dir
·mv file 不存在file 重命名
·移动原理: #相同设备分区中的文件移动是重命名过程;不同分区的文件移动是建立删除过程
·ls -i file 查看文件节点号(相当与文件身份证号)
[westos@localhost Desktop]$ mv westosb mkdir5
[westos@localhost Desktop]$ mv mkdir1 mkdir3 mkdir5
- file ##文件类型的查看
·##file westos 查看westos的真实类型
[westos@localhost Desktop]$ file westosa
westosa: empty
[westos@localhost Desktop]$ file file
file: UTF-8 Unicode text
- wc ##文件容量统计
·wc -l 行数
·wc -m 字符数
·wc -w 单词数
·wc -c 字节数
[westos@localhost Desktop]$ wc -l westos1
1 westos1
[westos@localhost Desktop]$ wc -m westos1
10 westos1
[westos@localhost Desktop]$ wc -w westos1
1 westos1
[westos@localhost Desktop]$ wc -c westos1
10 westos1
[westos@localhost Desktop]$ cat westos1
qdnskaaan
2.Linux中文件的寻址#
- Linux系统结构
·#FHS (Filesystem Hierarchy Standard) Linux层级结构标准3
·#LINUX中一切都是文件
·/ #Linux顶级目录 rm -fr /* 此命令卡万不要在服务器上运行
#系统二级目录
/bin ##系统常规命令
/sbin ##系统管理命令
/dev ##设备文件
/home ##普通用户家目录集合,此目录中的子目录为普通用户家目录
/root ##超级用户家目录
/lib64 ##64位函数库
/lib ##系统函数库
/proc ##进程信息,系统识别的硬件信息
/run ##当前运行的硬件和系统模块
/srv ##系统数据(常量) /var ##系统数据(变化) /tmp ##系统临时文件存放目录
/mnt ##临时设备挂载点
/media ##cdrom临时挂载点
/etc ##系统大多数程序的配置目录
/opt ##第三方软件安装位置
/boot ##系统启动分区,系统启动时读取的文件都在此目录中
/sys ##关于内核设定目录
/usr ##unix system resource 仅次于/ 重要 多数系统资源都在此目录中 - 相对路径:
·绝对路径名称的简写,省略了用户当前所在的系统位置
·此名称只有在管理当前所在系统目录中子文件时才能使用
·系统中不以/开有的文件名称都为相对路径在程序操作时,会自动识别当前路径的信息并关联操作目标 - 绝对路径 :
·文件在系统中的真实位置,在任何场景下都可以使用
·通常以/开头,不会考虑当前系统位置
3.关于文件寻址的命令#
- pwd ##显示当前的工作目录
- cd ##切换当前的工作目录
·##“cd ~-” = “cd -”
[westos@localhost Desktop]$ cd mkdir1
[westos@localhost mkdir1]$ pwd
/home/westos/Desktop/mkdir1
[westos@localhost mkdir1]$ cd -
/home/westos/Desktop
[westos@localhost Desktop]$ cd -
/home/westos/Desktop/mkdir1
[westos@localhost mkdir1]$ cd /home/westos/
[westos@localhost ~]$ pwd
/home/westos
[westos@localhost ~]$ cd /home
- ls##显示文件信息
·ls 显示当前目录中文件的名称
·ls file 显示指定文件名称
·ls dir 显示指定目录中内容的名称
·ls -d dir 显示目录本身的名称
·ls -l file 显示文件属性
·ls -l dir 显示目录中内容的属性
·ls -ld dir 显示目录本身属性
·ls -a dir 显示目录中所有文件名称包括隐藏文件案
·ls -s 显示文件大小
·ls -R dir 第归显示目录中的内容
[westos@localhost Desktop]$ ls mkdir5
mkdir1 mkdir2 mkdir3 westosb
[westos@localhost Desktop]$ ls -s file
4 file
[westos@localhost Desktop]$ ls passwd
passwd
[westos@localhost Desktop]$ ls -R mkdir5
mkdir5:
mkdir1 mkdir2 mkdir3 westosb
mkdir5/mkdir1:
westos1 westosb westosc
mkdir5/mkdir2:
mkdir5/mkdir3:
mkdir1 mkdir2
mkdir5/mkdir3/mkdir1:
mkdir5/mkdir3/mkdir2:
4.文件批量处理#
·<*> ##匹配0~任意字符
·<?> ##匹配单个字符
·[[:alpha:]] ##匹配单个字母
·[[:lower:]] ##匹配单个小写字母
·[[:upper:]] ##匹配单个大写字母
·[[:digit:]] ##匹配单个数字
·[[:alnum:]] ##匹配单个数字或字母
·[[:punct:]] ##匹配单个符号
·[[:space:]] ##匹配单个空格
[westos@localhost Desktop]$ touch westos1 westos5 westosa westosb westosaaa westosnnn 'wes tos'
[westos@localhost Desktop]$ rm -fr westos?
[westos@localhost Desktop]$ rm -fr westos*
[westos@localhost Desktop]$ touch westos1 westos5 westosa westosb westosaaa westosnnn 'wes tos'
[westos@localhost Desktop]$ rm -fr wes[[:space:]]
[westos@localhost Desktop]$ rm -fr wes[[:space:]]tos
[westos@localhost Desktop]$ rm -fr westos[[:alpha:]][[:alpha:]][[:alpha:]]
[westos@localhost Desktop]$ rm -fr westos[[:alpha:][:digit:]]
解释:
[[:alpha:]][[:alpha:]][[:alpha:]]三位
[[:alpha:][:digit:]] 一位,是字母或者数字
练习题
复制/etc/中带数字的以.conf为结尾的文件到/mnt/里
[root@localhost ~]# cp /etc/*[[:digit:]]*.conf /mnt/
[root@localhost ~]# ls /mnt
file hgfs krb5.conf mke2fs.conf mnt pbm2ppa.conf pnm2ppa.conf
- 字符集合表示方法
·[] ##条件是或者关系是模糊匹配,对象和几个对比匹配就操作
##[1-10] 1到10 [!d-f]|[^d-f] 除了d-f以外
·{} ##点名机制,经精确指定集合中的每一个元素
##{1…10} 1-10 {a…c} a-c ~ ##默认代表当前用户家目录
[westos@localhost Desktop]$ touch file{1..5}
[westos@localhost Desktop]$ mv file{1..2} mkdir5
[westos@localhost Desktop]$ cp file{2..3} mkdir10
cp: cannot stat 'file2': No such file or directory
[westos@localhost Desktop]$ cp file[2-3] mkdir10
[westos@localhost Desktop]$ touch westos{a..f}
[westos@localhost Desktop]$ rm -fr westos{a,f}
[westos@localhost Desktop]$ rm -fr file[34]
[westos@localhost Desktop]$ touch file{6..10}
[westos@localhost Desktop]$ rm -fr file[!9]
[westos@localhost Desktop]$ touch file{6..10}
[westos@localhost Desktop]$ rm -fr file[!45]
[westos@localhost Desktop]$ rm -fr westos[^45]
[westos@localhost Desktop]$ rm -fr westos[![:alpha:][:digit:]]
解释:
[34]是删除3和4 文件
{a,f}是删除a和f文件
[!9] 文件10还会留着,因为10是两位
[!45]和[^45] 是一样的
westos[![:alpha:][:digit:]]一位,不是字母也不是数字
~username ##指定的用户家目录
“~+” = “.” ##当前目录
~- ##当前目录之前所在目录
… ##当前目录的上级目录