Linux文件管理

1. 文件和目录管理
linux的目录是一颗目录树。
cd / 进入根目录
1.绝对路径
正如前文所述,Linux系统采用了目录树的文件组织结构,在Linux下每个目录或文件都可以从根目录处开始寻找,比如:/usr/local/src目录。这种从根目录开始的全路径被称为“绝对路径”,绝对路径一定是以“/”开头的。
2.当前目录:pwd
想要确定当前所在的目录,可以使用以下pwd命令查看

[root@localhost ~]# pwd
/root

3.特殊目录:(.)和(…)
在每个目录下,都会固定存在两个特殊目录,分别是一个点(.)和两个点(…)的目录。一个点(.)代表的是当前目录,两个点(…)代表的是当前目录的上层目录。在Linux下,所有以点开始的文件都是“隐藏文件”,对于这类文件,只使用命令ls-l是看不到的,必须要使用ls-la才可以看到,如下所示:

[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls -la
total 16
drwxr-xr-x  2 root root 4096 Jan 27  2010 .
drwxr-xr-x 24 root root 4096 Jan  2 01:50 ..
  1. 相对路径
    相对路径”的关键在于当前在什么路径下。假设当前目录在/usr/local下,那么它的上层目录(/usr目录)可以用…/表示,而/usr/local的下层目录(src)则可以用./src表示。前面讲到的(.)和(…)目录实际上也是属于相对路径,来看下面的例子:
[root@localhost ~]# cd /mnt   #
现在进入/mnt
目录
[root@localhost mnt]# ls -la
total 16
drwxr-xr-x  2 root root 4096 Jan 27  2010 .   #
代表当前目录
drwxr-xr-x 24 root root 4096 Jan  2 01:50 ..   #
代表上层目录
[root@localhost mnt]# cd .   #
进入当前目录(cd
命令后面再介绍)
[root@localhost mnt]# pwd   #
显示当前目录
/mnt   #
看到我们还是在/mnt
目录中
[root@localhost mnt]# cd ..   #
进入当前目录的上层目录
[root@localhost /]# pwd
/   #
进入了上层目录,也就是/
目录中
  1. 文件的相关操作
    1.创建文件:touch
    2.删除文件:rm
    3.移动或重命名文件:mv
[root@localhost ~]# cd /tmp/   #
进入/tmp
目录
[root@localhost tmp]# ls   #
看一下目录中有什么
[root@localhost tmp]#   #
确认什么都没有
[root@localhost tmp]# touch test.txt   #
创建一个文件
[root@localhost tmp]# ls   #
再看一下目录内容
test.txt   #
确认文件已经创建成功了
[root@localhost tmp]# ls /mnt   #
看一下/mnt
目录中有什么
[root@localhost tmp]#   #
什么都没有
[root@localhost tmp]# mv test.txt /mnt/   #
移动文件到/mnt
下
[root@localhost tmp]# ls /mnt   #
再看一下/mnt
中有什么
test.txt   #
文件移动到/mnt
中了
[root@localhost tmp]# ls   #
看一下当前目录中的内容
[root@localhost tmp]#   #
文件已经被移走了
[root@localhost ~]# cd /mnt/   #
进入/mnt
目录
[root@localhost tmp]# ls   #
看一下当前目录中有什么
test.txt   #
这是刚刚移动过来的文件
[root@localhost mnt]# mv test.txt test.doc   #
修改了文件名
[root@localhost mnt]# ls
test.doc   #
确认文件名修改成功

4.查看文件:cat
5.查看文件头:head
6.查看文件尾以及动态查看文件内容:tail

4. 文件压缩和打包

  1. gzip/gunzip
  2. tar
    tar不但可以打包文件,还可以将整个目录中的全部文件整合成一个包,整合包的同时还能使用gzip的功能进行压缩,比如说把整个/boot目录整合并压缩成一个文件。一般来说,整合后的包习惯使用.tar作为其后缀名,使用gzip压缩后的文件则使用.gz作为其后缀名。因为tar有同时整合和压缩的功能,所以可使用.tar.gz作为后缀名,或者简写为.tgz。下面的命令将/boot目录整合压缩成了boot.tgz文件:
[root@localhost ~]# tar -zcvf boot.tgz /boot
[root@localhost ~]# tar -zxvf boot.tgz
[root@localhost ~]# tar -zxvf boot.tgz -C /tmp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值