Linux 文件管理

一、新建文件与目录

1、文件建立 touch命令
格式:touch file file1 file2 …(可连续建立多个文件 touch后也可跟文件路径)
注意:
touch默认用来建立文件,同时此命令具备修改文件时间戳功能

[root@localhost Desktop]# touch file
[root@localhost Desktop]# ls
bin_westos_file.txt passwd test~ unit7 zuoye zuoye1
file test time.txt Untitled Folder zuoye~ zuoye1~

[root@localhost Desktop]# touch /mnt/file ##在其他盘建立文件
[root@localhost Desktop]# ls /mnt

2、新建目录(directory)mkdir命令
(1)新建目录
格式:mkdir dir

[root@localhost Desktop]# mkdir mulu
[root@localhost Desktop]# ls
bin_westos_file.txt mulu test time.txt Untitled Folder zuoye~ zuoye1~
file passwd test~ unit7 zuoye zuoye1

(2)新建递归目录
格式:mkdir -p dir1/dir2/dir3 (-p=path)

[root@localhost Desktop]# mkdir -p a/b/c
[root@localhost Desktop]# ls a
b
[root@localhost Desktop]# ls a/b
c

二、删除文件与目录

1、删除文件(rm)
格式:rm -f file1 file2 file3…(强制删除多个文件)

[root@localhost Desktop]# ls
a file passwd test~ unit7 zuoye zuoye1
bin_westos_file.txt mulu test time.txt Untitled Folder zuoye~ zuoye1~
[root@localhost Desktop]# rm -f file zuoye zuoye1 bin_westos_file.txt time.txt ##同时强制删除多个文件
[root@localhost Desktop]# ls
a mulu passwd test test~ unit7 Untitled Folder zuoye~ zuoye1~
[root@localhost Desktop]#

2、删除目录
rm -fr dir (-f force强制删除不提示)(-r recursive 递归删除)

[root@localhost Desktop]# ls
a file passwd test~ unit7 zuoye zuoye1
bin_westos_file.txt mulu test time.txt Untitled Folder zuoye~ zuoye1~
[root@localhost Desktop]# rm -f file zuoye zuoye1 bin_westos_file.txt time.txt
[root@localhost Desktop]# ls
a mulu passwd test test~ unit7 Untitled Folder zuoye~ zuoye1~
[root@localhost Desktop]#

三、复制
复制是按照模版新建过程
1、复制文件
(1)复制单个文件
格式:cp file dest

[root@localhost Desktop]# touch file1 file2
[root@localhost Desktop]# cat file1
hello
[root@localhost Desktop]# cp file1 file2
cp: overwrite ‘file2’? y
[root@localhost Desktop]# cat file2
hello

(2)复制多个文件到指定目录中
格式:cp file file1 file2 dir

[root@localhost Desktop]# mkdir a
[root@localhost Desktop]# cp file1 a
[root@localhost Desktop]# ls a
file1
[root@localhost Desktop]#

2.复制目录
复制目录到指定目录中
格式:cp -r sourcedir1 sourcedir2 destdir

[root@localhost Desktop]# mkdir b c
[root@localhost Desktop]# cp -r a b c /mnt
[root@localhost Desktop]# ls /mnt
a b c file

四、移动
相同磁盘文件移动是重命名
不同磁盘文件移动是复制后删除
(1)重命名
格式:mv file file1

[root@localhost Desktop]# mv file1 file3
[root@localhost Desktop]# ls
a c file2 passwd test~ Untitled Folder zuoye1~
b file1~ file3 test unit7 zuoye~
[root@localhost Desktop]#

(2)移动文件
格式:mv file dir

[root@localhost Desktop]# mv file2 /mnt
[root@localhost Desktop]# ls /mnt
a b c file file2
[root@localhost Desktop]# ls #文件移动后之前磁盘此文件删除
a c file3 test unit7 zuoye~
b file1~ passwd test~ Untitled Folder zuoye1~
[root@localhost Desktop]#

五、查看文件
1、显示文件全部内容
格式:cat file

[root@localhost Desktop]# cat passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
shutdown❌6:0:shutdown:/sbin:/sbin/shutdown
halt❌7:0:halt:/sbin:/sbin/halt
mail❌8:12:mail:/var/spool/mail:/sbin/nologin
operator❌11:0:operator:/root:/sbin/nologin
games❌12?games:/usr/games:/sbin/nologin
ftp❌14:50:FTP User:/var/ftp:/sbin/nologin
nobody❌99:99:Nobody:/:/sbin/nologin

2、显示文件内容并在每行前加入行号
格式:cat -n file

[root@localhost Desktop]# cat -n passwd
1 root❌0:0:root:/root:/bin/bash
2 bin❌1:1:bin:/bin:/sbin/nologin
3 daemon❌2:2:daemon:/sbin:/sbin/nologin
4 adm❌3:4:adm:/var/adm:/sbin/nologin
5 lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync❌5:0:sync:/sbin:/bin/sync
7 shutdown❌6:0:shutdown:/sbin:/sbin/shutdown
8 halt❌7:0:halt:/sbin:/sbin/halt
9 mail❌8:12:mail:/var/spool/mail:/sbin/nologin
10 operator❌11:0:operator:/root:/sbin/nologin
11 games❌12?games:/usr/games:/sbin/nologin
12 ftp❌14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody❌99:99:Nobody:/:/sbin/nologin
14 dbus❌81:81:System message bus:/:/sbin/nologin

3、显示文件内容并在每行前加入行号但是不包含空行
格式:cat -b file

六、文件查看方式
1、less
less ##分页浏览
上|下 ##逐行查看
pgup|pgdn ##主页查看
/关键字 ##搜索关键字并高亮显示,"n"向下匹配一个关键字
##"N"想上匹配一个关键字
七、文件显示
1、head 显示头
head file ##显示文件前10行
head -n 3 file ##显示文件前3行

[root@localhost Desktop]# head -n 3 passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin

2、tail 显示尾
tail file ##显示文件后10行
tail -n 3 file ##显示文件后3行

[root@localhost Desktop]# tail -n 3 passwd
gdm❌42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup❌993:991::/run/gnome-initial-setup/:/sbin/nologin
tcpdump❌72:72:?:/sbin/nologin
[root@localhost Desktop]#

八、文件修改
1、图形工具
gedit file

2、vim
vim file

九、文件路径
1、相对路径
相对与当前系统位置,文件名称的简写,此名称前会自动添加’pwd’命令的输出,相对路径文件名称前不会出现"/"
2、绝对路径
文件在系统中的真实位置,在任何情况下都可以使用
绝对路径一定以"/" 开头
3.linux的系统结构及目录分类
linux是一个倒树型结构顶级目录 “/” 根目录
"/"下的二级目录
/bin ##系统常规命令
/boot ##系统启动目录
/dev ##设备影射文件
/etc ##系统配置文件
/home ##普通用户家目录
/lib ##32位函数库
/lib64 ##64位函数库
/media ##光盘临时挂载
/mnt ##手动临时挂载
/run ##自动临时挂载
/opt ##第三方软件安装位置
/proc ##系统进程信息和硬件信息
/root ##超级用户家目录
/sbin ##系统管理命令
/srv ##系统数据
/sys ##内核相关调式
/tmp ##临时文件
/usr ##用户相关设定
/var ##系统数据
关于系统路径的命令
pwd ##显示当前路径
4、显示
(1)显示路径信息
格式:ls dir
(2)显示目标属性
格式:ls -l file

[root@localhost Desktop]# ls -l passwd
-rw-r–r--. 1 root root 2005 Sep 29 02:33 passwd

(3)显示目录本身属性
格式:ls -l -d dir

[root@localhost Desktop]# ls -ld b
drwxr-xr-x. 2 root root 6 Oct 8 07:54 b

(4)递归显示目录中的所有内容
格式:ls -R dir

[root@localhost Desktop]# mkdir -p r/g/t
[root@localhost Desktop]# ls -R r
r:
g
r/g:
t
r/g/t:
[root@localhost Desktop]#

(5)显示所有文件包括以"."开头的隐藏文件
格式:ls -a
(6) 显示所有隐藏文件
格式:ls -ad .*

[root@localhost Desktop]# ls -ad .*
. … .stydy
[root@localhost Desktop]#

5、路径切换
(1)进入当前用户家目录
cd
(2)进入mnt目录
cd /mnt
(3)进入到当前目录的上一级目录
cd …
(4)当前目录和进入当前目录之前所在目录之间的切换
cd -

[root@localhost Desktop]# cd
[root@localhost ~]# cd /mnt
[root@localhost mnt]# cd …
[root@localhost /]# cd -
/mnt
[root@localhost mnt]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值