文件管理"/>
linux—文件管理
1、常用文件命令
touch —新建文件,修改文件时间戳
touch file1 file2… 建立空文件
touch file 当文件存在,再次touch这个文件,表示把文件时间同步到当前时间
touch -t YYYYMMDDHHmm file 把文件时间更改到指定时间
[root@foundation50 Desktop]# stat file 查看文件时间标识File: fileSize: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 868628592 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:user_home_t:s0 Access: 2021-07-30 10:20:38.005413746 +0800 表示文件最后一次被访问的时间Modify: 2021-07-30 10:20:38.005413746 +0800 表示文件内容最后一次被修改的时间Change: 2021-07-30 10:20:38.005413746 +0800 标识文件内容或在文件属性被修改的时间Birth: -
mkdir 建立目录
mkidr -p test/redhat/linux -p建立多级目录 (当父级目录不存在时可以一并建立)
rm 删除文件或者目录
rm file file1 … 删除文件有提示
rm -f file 强制删除文件不提示
rm -r dir dir1… 删除目录需要加-r, -r表示递归删除,删除目录和里面的内容
rm -rf dir 强制递归删除
gedit 图形的文本编辑器
gedit file file1… 编辑file file1
#gedit 在使用时必须有图形
#file file1可以不存在
vim 文本编辑器
vim file
##进入到vim的浏览模式
##浏览模式不能编辑文件
##在浏览模式中按i进入插入模式
#在vim中鼠标不能控制光标用上下左右移动光标
#按退出插入模式
#按:wq保存更改
#q 当vim打开文件后未作任何操作直接退出
#q! 当vim打开文件后修改文件内容但不想保存可以强制退出
vim异常退出
##当vim编写文件时在未保存更改内容被强制关闭
##vim出现异常
O
只读打开文件
E
继续编辑
R
恢复未保存内容编辑
D
删除.swp文件进入编辑
Q
退出当前文件编辑 (vim -p westos westos1 如果同时打开两个文件,westos有问题,westos1没问题,按q推出只退出有问题的)
A
推出vim程序 (vim -p westos westos1不管westos westos1文件有没有问题都退出)
vim同时打开多个文件
vim -o westos1 westos2 上下模式打开两个文件<ctrl>+<w> <上|下>来转换控制窗口
vim -p westos1 westos2 左右打开两个不同文件tabn 进入到下一个窗口tabp 进入到上一个窗口
cat 显示文件的全部内容
[root@foundation50 Desktop]# cat file
westos
cat -b 显示文件内容并显示行号,但不显示空行号
[root@foundation50 Desktop]# cat -b file1 westos2 linux3 too
cat -n 显示文件内容并显示空行号
[root@foundation50 Desktop]# cat -n file1 2 westos3 linux4 too
head -n 查看文件前多少行 , -n 代表数字
[root@foundation50 Desktop]# cat -n file1 2 westos3 linux4 too
[root@foundation50 Desktop]# head -2 filewestos
tail -n 查看文件后多少行
[root@foundation50 Desktop]# cat -n file1 2 westos3 linux4 too
[root@foundation50 Desktop]# tail -2 file 查看后两行
linux
too
less 分页浏览
less westos 进入到less模式查看westos
less模式中 <上><下> 逐行查看文件内容
<pgup><pgdn>逐页查看文件内容
/关键字 搜索关键字 n 向下匹配 N 向上匹配
#<v> 有less模式进入到vim 编辑完成:wq退出返回less
#在less中<q> 退出
cp 复制文件
cp 文件 目的地文件
cp 文件1 文件2 目的地目录(此目录必须先存在)
cp -r 目录1 目录2
#复制原理:
#按照源文件建立新文件(抄作业)
mv 移动文件
mv file file1 dir 目的地文件要存在
mv dir dir1 dir 此时目的目录一定要存在
mv file westos 不存在westos 时相当于重新命名的过程
相同设备分区中移动是重新命名的过程,不同分区是建立和删除的过程,可以用ls -i查看文件节点号
file 查看文件真实类型
文件名称不能确定文件类型,确定文集内容的因素是文件内容
[root@foundation50 Desktop]# file westos.mp3
westos.mp3: empty 是一个空文件[root@foundation50 Desktop]# cat westos.mp3
westos
[root@foundation50 Desktop]# file westos.mp3 纯文本文件
westos.mp3: ASCII text[root@foundation50 Desktop]# cat westos.mp3
#!/bin/bash
[root@foundation50 Desktop]# file westos.mp3 shell文件
westos.mp3: Bourne-Again shell script, ASCII text executable[root@foundation50 Desktop]# cat westos.mp3
#include<stdio.h>
[root@foundation50 Desktop]# file westos.mp3 c语言文件
westos.mp3: C source, ASCII text
wc 文件容量统计
[root@foundation50 Desktop]# wc westos
1(行数1) 1 (单词数1) 7 westos (子符数7,六个字母+一个换行符)
wc -l 统计文件行数
[root@foundation50 Desktop]# wc -l westos
1 westos
wc -c 统计文件字节数 英文一个单词代表一个字节,一个汉字代表三个字节
[root@foundation50 Desktop]# wc -c westos
7 westos
wc -m 统计文件字符数
[root@foundation50 Desktop]# wc -m westos
7 westos
wc -w 统计文件单词数
[root@foundation50 Desktop]# wc -w westos
1 westos
2、linux层级结构
目录名 | 目录作用 |
---|---|
bin | 系统常规命令 |
sbin | 系统管理命令 |
boot | 系统启动分区,系统启动时读取的文件,如果boot里面的没了,系统就启动不了了 |
dev | 设备文件 |
etc | 系统配置文件 |
home | 普通用户家目录集合,相当于王子荣耀的泉水,也就是linux刚登陆进取没做任何操作的目录 |
root | 超级用户的家目录 |
lib/lib64 | 系统函数库 |
media | 移动设备挂载点 |
mnt | 临时挂载点 |
opt | 第三方软件安装的位置 |
proc | 系统进程目录 |
run | 系统正在运行的 |
srv | 系统数据常量 |
var | 系统数据变量,例如log日志 |
sys | 系统识别的驱动,或在系统优化时优化的规则 |
tmp | 临时文件,临时目录 |
usr | 系统资源目录,仅此于/ |
lib函数库理解图 :
3、linux 系统如何识别u盘
下载ntfs安装包就可以识别u盘了
[root@foundation50 pub]# rpm -ivh ntfs-3g-2015.3.14-2.el7.x86_64.rpm
reboot
4、linux文件寻址命令
pwd 显示当前的工作目录
[root@foundation50 ~]# pwd
/root
cd ~-=cd - 表示目录之前所在目录 (-只能在cd中使用其他命令里面使用不了)
[root@foundation50 ~]# pwd
/root
[root@foundation50 ~]# cd /mnt
[root@foundation50 mnt]# cd -
/root
[root@foundation50 ~]# cd -
/mnt
5、ls 显示文件信息
命令 | 作用 |
---|---|
ls | 显示当前目录中文件的名称 |
ls file | 显示指定文件名称 |
ls dir | 显示指定目录中内容的名称 |
ls -d dir | 显示目录本身的名称 |
ls -l file | 显示文件属性 |
ls -l dir | 显示目录中内容的属性 |
ls -ld dir | 显示目录本身属性 |
ls -a dir | 显示目录中所有文件名称包括隐藏文件案 |
ls -s | 显示文件大小 |
[root@foundation50 Desktop]# ls 显示当前文件名称
file1 file2 RH124 westosdir
[root@foundation50 Desktop]# ls -a 显示目录中所有文件名称包括隐藏文件案
. .. file1 file2 RH124 .westos westosdir
[root@foundation50 Desktop]# ls -s file1 显示文件大小
0 file1
[root@foundation50 Desktop]# vim file1
[root@foundation50 Desktop]# ls -s file1
4 file1
[root@foundation50 Desktop]# ls -l file1 显示文件属性
-rw-r--r--. 1 root root 8 Aug 1 14:28 file1
[root@foundation50 Desktop]# ls -l westosdir/ 只能显示目录低下的文件属性
total 0
-rw-r--r--. 1 root root 0 Aug 1 14:19 file1
-rw-r--r--. 1 root root 0 Aug 1 14:19 file2
root@foundation50 Desktop]# ls -ld westosdir/** 显示目录本身属性
drwxr-xr-x. 2 root root 32 Aug 1 14:19 westosdir/
[root@foundation50 Desktop]# ls -R 递归显示文件
.:
file1 file2 RH124 westosdir./RH124:
'10. Linux系统中的日志管理' '7. Linux 中的进程管理'
'11. Linux 中的网络配置' '8. Linux 中的远程登陆服务'
'13. Linux 下的虚拟化部署' '9. Linux 系统中的文件传输'
'14. Linux 中的无人职守安装脚本kickstart' uint.1
'15. Linux 系统引导过程及引导修复' unit.2
'16. 系统定时任务和延时任务' unit.3
'5. Linux系统中的用户管理' unit.4
'6. Linux系统中的权限管理'./westosdir:
file1 file2
4. 文件批量处理
命令 | 作用 |
---|---|
* | ##匹配0~任意字符 |
? | ##匹配单个字符 |
[[:alpha:]] | ##匹配单个字母 |
[[:lower:]] | ##匹配单个小写字母 |
[[:upper:]] | ##匹配单个大写字母 |
[[:digit:]] | ##匹配单个数字 |
[[:alnum:]] | ##匹配单个数字或字母 |
[[:punct:]] | ##匹配单个符号 |
[[:space:]] | ##匹配单个空格 |
[root@foundation50 Desktop]# cp /etc/*.conf . *表示0到任意字符
[root@foundation50 Desktop]# touch file1 file2 file11 file22 file12
[root@foundation50 Desktop]# rm -fr file? ?表示匹配单个字符
[root@foundation50 Desktop]# ls
file11 file12 file22
[root@foundation50 Desktop]# rm -fr file?? ??表示删除两个字符以此类推
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes?tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:alpha:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:lower:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:upper:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:digit:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:slnum:]]tos
[root@westoslinux Desktop]# rm -fr wes[[:alnum:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:punct:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:space:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[![:space:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[![:space:][:digit:]]tos
[root@westoslinux Desktop]# touch wes1tos wesAtos wesatos wes@tos wes" "tos
[root@westoslinux Desktop]# rm -fr wes[[:space:][:digit:]]tos
5、 字符集合表示方法
[] 条件是或者关系模糊匹配,[1-10] 1到10 [!d-f]|[^d-f] 除了d-f以外
若在操作时没有该文件会自动跳过不会报错
{} 精确指定集合中的每一个元素 {1…10} 1-10 {a…c} a-c {1,3,5}表示1、3、5三个
若在操作时没有该文件会报错
[root@westoslinux Desktop]# touch westos{1..5}{a..f}
[root@westoslinux Desktop]# rm -fr westos[3-5]?
[root@westoslinux Desktop]# touch westos{1..5}{a..f}
[root@westoslinux Desktop]# rm -fr westos[^3-5]?
[root@westoslinux Desktop]# touch westos{1..5}{a..f}
[root@westoslinux Desktop]# rm -fr westos[^35]?
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos[1-3]*
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos[13]*
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos[1,3]*
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos{1..3}*
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos[!1,3]*
[root@foundation50 Desktop]# touch westos{1,5}{a..f}
[root@foundation50 Desktop]# touch westos{1,5}{a..f}
[root@foundation50 Desktop]# touch westos{1..5}{a..f}
[root@foundation50 Desktop]# rm -fr westos{1,3}*
cd .. 当前目录的上级目录~ 默认代表当前用户家目录~username 指定用户家目录~+ = . 当前目录
[root@foundation50 ~]# cd ~westos
[root@foundation50 westos]# pwd
/home/westos
[root@foundation50 westos]# cd ~+
[root@foundation50 westos]# pwd
/home/westos
[root@foundation50 westos]# cd -
/home/westos
[root@foundation50 westos]# cd /mnt
[root@foundation50 mnt]# cd -
/home/westos
[root@foundation50 westos]# pwd
/home/westos
[root@foundation50 westos]# cd ..
[root@foundation50 home]# pwd
/home
练习题:
1.用命令和正则表达式按照要求建立文件
*)用一条命令建立12个文件WESTOS_classX_linuxY(X的数值范围为1-2,Y的数值范围为1-6)
*)这些文件都包含在root用户桌面的study目录中
*)用一条命令建立8个文件redhat_versionX(x的范围为1-8)
*)redhat_virsionX这些文件都包含在/mnt目录中的VERSION中
2.管理刚才信建立的文件要求如下
*)用一条命令把redhat_versionX中的带有奇数的文件复制到桌面的SINGLE中
*)用一条命令把redhat_versionX中的带偶数数的文件复制到/DOUBLE中
*)用一条命令把WESTOS_classX_linuxY中class1的文件一动到当前用户桌面的CLASS1中
*)用一条命令把WESTOS_classX_linuxY中class2的文件一动到当前用户桌面的CLASS2中
[root@westoslinux Desktop]# mkdir study SINGLE CLASS{1,2} /mnt/VERSION /DOUBLE
[root@westoslinux Desktop]# ls -d study SINGLE CLASS{1,2} /mnt/VERSION /DOUBLE
CLASS1 CLASS2 /DOUBLE /mnt/VERSION SINGLE study
[root@westoslinux Desktop]# touch study/WESTOS_class{1,2}_Linux{1..6} /mnt/VERSION/redhat_version{1..8}
[root@westoslinux Desktop]# ls study/WESTOS_class{1,2}_Linux{1..6} /mnt/VERSION/redhat_version{1..8}
/mnt/VERSION/redhat_version1 study/WESTOS_class1_Linux3
/mnt/VERSION/redhat_version2 study/WESTOS_class1_Linux4
/mnt/VERSION/redhat_version3 study/WESTOS_class1_Linux5
/mnt/VERSION/redhat_version4 study/WESTOS_class1_Linux6
/mnt/VERSION/redhat_version5 study/WESTOS_class2_Linux1
/mnt/VERSION/redhat_version6 study/WESTOS_class2_Linux2
/mnt/VERSION/redhat_version7 study/WESTOS_class2_Linux3
/mnt/VERSION/redhat_version8 study/WESTOS_class2_Linux4
study/WESTOS_class1_Linux1 study/WESTOS_class2_Linux5
study/WESTOS_class1_Linux2 study/WESTOS_class2_Linux6
[root@westoslinux Desktop]# cp /mnt/VERSION/redhat_version{1,3,5,7} SINGLE/
[root@westoslinux Desktop]# ls SINGLE/
redhat_version1 redhat_version3 redhat_version5 redhat_version7
[root@westoslinux Desktop]# cp /mnt/VERSION/redhat_version[2468] /DOUBLE/
[root@westoslinux Desktop]# ls /DOUBLE/
redhat_version2 redhat_version4 redhat_version6 redhat_version8
[root@westoslinux Desktop]# mv study/*class1* CLASS1/
[root@westoslinux Desktop]# mv study/*class2* CLASS2/
[root@westoslinux Desktop]# ls CLASS1/
WESTOS_class1_Linux1 WESTOS_class1_Linux3 WESTOS_class1_Linux5
WESTOS_class1_Linux2 WESTOS_class1_Linux4 WESTOS_class1_Linux6
[root@westoslinux Desktop]# ls CLASS2/
WESTOS_class2_Linux1 WESTOS_class2_Linux3 WESTOS_class2_Linux5
WESTOS_class2_Linux2 WESTOS_class2_Linux4 WESTOS_class2_Linux6
6、whatis命令的用法
whatis rm ##查看命令的基本用途
##查看过程中出现 #rm: nothing appropriate.
#1.表示要查看内容没有帮助
#2.系统帮助数据库未更新,用mandb命令更新帮助数据库
“注意:当执行whatis命令出现 nothing appropriate 时大多数情况是因为”
“系统的帮助数据库未更新,如何解决此问题,需要在root用户下执行mandb”
7、如何获得命令的简要帮助
rm --help 查看命令的基本用法
[] 内容可加可不加
… 内容个数任意
<> 必须在命令执行时加入的元素
8、man rm 命令用法详解 man是manual的缩写
man -k passwd passwd关键字有多少级别的man
man的级别
#1命令
#2系统调用
#3函数库调用
#4特殊文件(设备文件等)
#5文件
#6游戏
#7特殊的包
#8系统管理命令
#9内核信息规则
man rm 进入到rm命令的帮助
q 退出
/关键字 搜索关键字,n 向下匹配, N 向上匹配
G 快速移动到man的最后
g 表示快速移动到man的最前
更多推荐
linux—文件管理
发布评论