Linux学习03--文件与目录管理1

1.目录与路径

1.1 相对路径与绝对路径

  • 相对路径:路径写法 根目录/…(省略号,省略其中路径)/目标文件
    /写起
    举例:
/hello/test1/test2 这个目录
  • 绝对路径:路径写法 不是由根目录写起,而是从当前所在文档的目录写起
    不是由 / 写起
    举例:
    由目录 /hello/test1/test2 转到 /hello/test1/test3
    可以写成:
cd ../test3

1.2 目录的相关操作

.   			代表当前目录
..  			代表上一级目录
-  				代表前一个工作目录
~ 				代表目前用户身份所在home的目录
~account 		代表account用户身份所在home的目录

常见的目录处理指令:

  • cd:变换目录
  • pwd:显示当前目录
  • mkdir:建立一个新的目录
  • rmdir:删除一个的目录

变换目录:cd

写法:cd [相对路径或者绝对路径]

范例1: 绝对路径切换

[root@admin /]# cd /hello/test1 <==切换到hello目录下的test1目录下
[root@admin test1]# 

范例2: 相对路径切换

[root@admin test1]# cd ../test2 <==从当前路径返回上一级切换到test2目录下
[root@admin test2]# 

范例3:返回当前用户home目录
方法一:

[root@admin test2]# cd  <==直接cd后面什么都不跟
[root@admin ~]# 

方法二:

[root@admin test1]# cd ~  <==~后面什么都不跟
[root@admin ~]# 

范例四:返回其他用户home目录

[root@admin ~]# cd ~Danboard <==~后面跟目的用户名
[root@admin Danboard]# 

显示目前所在目录:pwd

选项参与参数:
-P : 显示出确实的路径,而非使用链接(link的路径),包括连结档

范例1:显示出当前目录

[root@admin ~]# pwd 
/root <== 显示出目录

范例2:显示出实际工作目录

[root@admin ~]# cd /var/mail <==/var/mail是一个连结档
[root@admin mail]# pwd 
/var/mail <==列出目前的工作目录 
[root@admin mail]# pwd -P 
/var/spool/mai
[root@admin mail]# ls -ld /var/mail 
lrwxrwxrwx. 1 root root 10 May 4 17:51 /var/mail -> spool/mail  <==真实的工作目录

建立新目录:mkdir

写法:mkdir [-mp] 目录名称
相关参数:

  • -m:配置所创建文件的权限

  • -p:递归建立所需目录

范例1:在hello目录下建立test3目录

[root@admin hello]# mkdir test3
[root@admin hello]# ll
总用量 3
drwxr-xr-x. 2 root yang  22 720 11:47 test1
drwxr-xr-x. 3 root root  19 721 11:59 test2
drwxr-xr-x. 2 root root   6 721 16:55 test3

范例2:在hello目录下建立 拥有者可读可写可执行,群组和用户可读的目录test4

[root@admin hello]# mkdir -m 744 test4
[root@admin hello]# ll
总用量 4
drwxr-xr-x. 2 root yang  22 720 11:47 test1
drwxr-xr-x. 3 root root  19 721 11:59 test2
drwxr-xr-x. 2 root root   6 721 16:55 test3
drwxr--r--. 2 root root   6 721 16:57 test4

范例3:在hello目录下递归建立 test5/test6/test7

[root@admin hello]# mkdir -p test5/test6/test7

删除空的目录:rmdir

写法: rmdir [-p] 目录名称
参数:

  • -p:连同上层空的目录一起删除

范例1:删除目录test4

[root@admin hello]# rmdir test4
[root@admin hello]# 

若删除目录不为空

[root@admin hello]# rmdir test1
rmdir: 删除 "test1" 失败: 目录非空

范例2:删除test5及其子目录

[root@admin hello]# rmdir -p test5/test6/test7

1.3 执行文件路径的变量–PATH

如同Windows系统的环境变量一般,Linux系统也拥有自己的环境变量,设置环境变量之后你可以在任何地方使用PATH中路径的指令,不用再重新执行绝对路径了

PATH的相关指令

查看系统中那些目录被定义:

[root@admin ~]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 

将文件/root路径加入到PATH中,这样在任何地方都可以直接使用root目录下的文件

[root@study ~]# PATH="${PATH}:/root" 

重点关注:

  • 不同身份使用者预设的PATH不同,默认能够随意执行的指令也不同(如root与dmtsai);
  • PATH是可以修改的;
  • 使用绝对路径或相对路径直接指定某个指令的文件名来执行,会比搜寻PATH来的正确;
  • 指令应该要放置到正确的目录下,执行才会比较方便;
  • 本目录(.)最好不要放到PATH当中。

2.文件与目录管理

2.1文件与目录的检视:ls

写法:ls [-aAdfFhilnrRSt] 文件名或目录名称..
相关参数:

  • -a : 展现出全部文件,包括隐藏文档,包括.和…
  • -A:展现出全部文件,但不包括.和…这两个目录
  • -d :仅列出目录本身,而不是列出目录内的文件数据(常用)
  • -f :直接列出结果,而不进行排序 (ls 预设会以档名排序!)
  • -F :根据文件、目录等信息,给予附加数据结构
  • -h :将文件容量以人类较易读的方式(例如 GB, KB 等等)列出来
  • -i :列出 inode 号码,inode 的意义下一章将会介绍;
  • -l :长数据串行出,包含文件的属性与权限等等数据;(常用)
  • -n :列出 UID 与 GID 而非使用者与群组的名称
  • -r :将排序结果反向输出(文档名从小到大,反向则从大到小)
  • -R :连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来;
  • -S :以文件容量大小排序,而不是用档名排序
  • -t :依时间排序,而不是用档名。
  • –color=never :不要依据文件特性给予颜色显示;
  • –color=always :显示颜色
  • –color=auto :让系统自行依据设定来判断是否给予颜色
  • –full-time :以完整时间模式 (包含年、月、日、时、分) 输出
  • -time={atime,ctime} :输出 access 时间或改变权限属性时间 (ctime)

范例1:展现自家目录下的所有文件

[root@admin ~]# ls -al ~ 

范例2:不显示颜色,但在文件名末显示出该文件名代表的类型

[root@admin ~]# ls -alF --color=never ~

范例3:完整呈现文件的时间线

[root@study ~]# ls -al --full-time ~ 

2.2复制文件与目录:cp

写法:cp [-adfilprsu] 来源文件(source) 目标文件(destination)
相关参数:

  • -a :相当于 -dr --preserve=all 的意思,至于 dr 请参考下列说明;(常用)
  • -d :若来源文件为链接文件的属性(link file),则复制链接文件属性而非文件本身;
  • -f :为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;
  • -i :若目标文件(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
  • -l :进行硬式连结(hard link)的连结档建立,而非复制文件本身;
  • -p :连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用)
  • -r :递归持续复制,用于目录的复制行为;(常用)
  • -s :复制成为符号链接文件 (symbolic link),亦即『快捷方式』文件;
  • -u :destination 比 source 旧才更新 destination,或 destination 不存在的情况下才复制。
  • –preserve=all :除了 -p 的权限相关参数外,还加入 SELinux 的属性, links, xattr 等也复制了。

特殊范例:
-l 和 -s都会建立所谓的连接档
-l为实体链接
-s为符号链接

[root@study tmp]# cp -s bashrc bashrc_slink 
[root@study tmp]# cp -l bashrc bashrc_hlink 
[root@study tmp]# ls -l bashrc* 
-rw-r--r--. 2 root root 176 Jun 11 19:01 bashrc <==与源文件不太一样了! 
-rw-r--r--. 2 root root 176 Jun 11 19:01 bashrc_hlink 
lrwxrwxrwx. 1 root root 6 Jun 11 19:06 bashrc_slink -> bashrc 

bashrc_slink 是一个『快捷方式』,这个快捷方式会连结到bashrc去

2.3移除文件与目录:rm

写法:rm [-fir] 文件或目录
选项与参数:

  • -f :就是 force 的意思,忽略不存在的文件,不会出现警告讯息;
  • -i :互动模式,在删除前会询问使用者是否动作
  • -r :递归删除

2.4移动文件与目录或者更名:mv

写法:mv [-fiu] source destination
选项与参数:

  • -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
  • -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
  • -u :若目标文件已经存在,且 source 比较新,才会更新 (update)

3.文件内容查阅

若我们需要查看一个文件的内容,我们需要采用以下几种常用命令:

3.1直接检视文件的内容

  1. cat(concatenate)
    写法:cat [-AbEnTv] 文件名
    选项与参数:

    -A:相当于-vET的整理选项,列出一些特殊字符
    -b:列出行号,空白行不显示
    -E:把结尾段字符$显示出来
    -n:打印出行号,连空白行也会有行号
    -T:将Tab案件以^I显示出来
    -v:列出一些特殊字符

范例1:列出特殊字符

[root@admin hello]# cat -A demo.txt
hello world$
$
hello world$
hello world$
hello world$
hello world$
hello world$
$
$
$
attribute$
attribute$
attribute$
attribute$
attribute$
attribute$

范例2:打印出行号,空白行不显示

[root@admin hello]# cat -b demo.txt 
     1	hello world

     2	hello world
     3	hello world
     4	hello world
     5	hello world
     6	hello world



     7	attribute
     8	attribute
     9	attribute
    10	attribute
    11	attribute
    12	attribute

范例3:打印出行号,包括空白行

[root@admin hello]# cat -n demo.txt 
     1	hello world
     2	
     3	hello world
     4	hello world
     5	hello world
     6	hello world
     7	hello world
     8	
     9	
    10	
    11	attribute
    12	attribute
    13	attribute
    14	attribute
    15	attribute
    16	attribute

范例4:把结尾段字符显示出来

[root@admin hello]# cat -E demo.txt
hello world$
$
hello world$
hello world$
hello world$
hello world$
hello world$
$
$
$
attribute$
attribute$
attribute$
attribute$
attribute$
attribute$

范例5:展现出一些特殊字符

[root@admin hello]# cat -v demo.txt
hello world

hello world
hello world
hello world
hello world
hello world



attribute
attribute
attribute
attribute
attribute
attribute
  1. tac(反向展示)
    与cat功能完全相反,cat由第一行到最后一行,tac由最后一行到第一行
    通常用于查阅项目中更新的内容

写法:tac 文件名
范例:

[root@admin hello]# tac demo.txt
attribute
attribute
attribute
attribute
attribute
attribute



hello world
hello world
hello world
hello world
hello world

hello world
  1. nl(添加符号打印)
    写法:nl [-bnw] 文件
    选项与参数:
  • -b:指定行号指定的方式,主要有两种:

    • -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
    • -b t :如果有空行,空的那一行不要列出行号(默认值);
  • -n :列出行号表示的方法,主要有三种:

    • -n ln :行号在屏幕的最左方显示;
    • -n rn :行号在自己字段的最右方显示,且不加 0 ;
    • -n rz :行号在自己字段的最右方显示,且加 0 ;
  • -w:行号字段占用的字符数

范例1:在行号前自动补0

[root@admin ~]# nl -b a -n rz /etc/issue 
000001 \S 
000002 Kernel \r on an \m 
000003 

范例2:显示出行号,包括空行,行号在最右端显示并且加0,行号字段字符长度为3

[root@study ~]# nl -b a -n rz -w 3 /etc/issue 
001 \S 
002 Kernel \r on an \m 
003 

3.2资料撷取

取出前几行:head

写法:head [-n number] 文件
选项和参数:
-n : 后面接数字,代表显示几行的意思

范例:前100行不显示,显示后100行

[root@study ~]# head -n -100 /etc/man_db.conf

取出后面几行:tail

写法:tail [-n number] 文件
数据和参数:
-n :后面接数字,代表显示几行的意思
-f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测

范例1:查看最后20行

[root@admin ~]# tail -n 20 /etc/man_db.conf

范例2:查看100行以后的数据

[root@admin ~]# tail -n +100 /etc/man_db.conf

例题:
在这里插入图片描述

head -n 20 /etc/man_db.conf | tail -n 10 

| : 为管道符,管道符后面的语句会使用前面语句的结果

3.3修改文件时间或建置新档:touch

首先我们来看一下Linux系统下的时间:
modification time(mtime):文件内容发生改变的时间
status time(ctime:文件的权限和属性更改
access time (atime):读取内容时间,就比如使用了cat

执行多重指令用;隔开

[root@admin ~]# date; ls -l /etc/man_db.conf ; ls -l --time=atime /etc/man_db.conf ; \ > ls -l --time=ctime /etc/man_db.conf

写法:touch [-acdmt] 文件
选项和参数:

  • -a :仅修订 access time;
  • -c :仅修改文件的时间,若该文件不存在则不建立新文件;
  • -d :后面可以接欲修订的日期而不用目前的日期,也可以使用 --date=“日期或时间”
  • -m :仅修改 mtime ;
  • -t :后面可以接欲修订的时间而不用目前的时间,格式为[YYYYMMDDhhmm]

范例:将日期修改到两个天前

[root@admin tmp]# touch -d "2 days ago" bashrc

4.文件预设权限:umask

此为新建目录后的默认权限

查看umask:

[root@admin ~]# umask
0022 <==与权限有关的为后3位,第一位是特殊权限
[root@study ~]# umask -S
u=rwx,g=rx,o=rx

那上述例子来说:
默认权限为022,user拥有所有权限,group和other只有w写权限
建立文件时:(-rw-rw-rw-) - (-----w–w-) ——> -rw-r–r--
建立目录时:(drwxrwxrwx) - (d----w–w-) ——> drwxr-xr-x

重新设定umask

[root@admin ~]# umask 002

例题:
在这里插入图片描述
umask为003,则拿掉的权限为— --- -wx
文件:(-rw-rw-rw-) ——> (-rw-rw-r–)
目录:(drwxrwxrwx) ——>(drwxrwxr–)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值