文件管理和IO重定向

1 文件系统目录的结构


来自马哥教育

1.1文件系统的目录结构


  • 文件和目录被组织成一个单根导致树结构
  • 文件系统从根目录下开始,根目录"/'表示
  • 根文件系统(rootfs): root filesystem
  • 标准linux文件系统(如:ext4,xfs),文件名大小写是敏感的,例如: MAIL,Mail不是一个东西
  • 以.开头的文件为隐藏文件。要看就用ls -a命令可以查看
  • 路径分隔用/,window用的是反斜线\
  • 文件名最长255个字节,一般一个汉字3个字节,所有做多80多个汉字。但是不建议用汉字。
  • 包括路径在内文件名称最长4095个字节
  • 蓝色——>目录;绿色——>可执行文件 ;红色——>压缩文件;浅蓝色——>链接文件;灰色——> 其他文件
  • 除了斜杠和NUL,所有字符都有效,但使用特殊字符的目录名和文件不推荐,有些字符需要用引号赖引用
  • 每个文件都有两类相关数据:
    • 元数据:metadata,即属性
    • 数据:data,即文件内容

Linux的文件系统分层结构遵循:FHS Filesystem Hierarchy Standard
标准参考文档:www.pathname.come/fhs/

1.2 常见的文件系统目录功能


/boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader,grub)都存放在这里
/bin:所有用户使用的基本命令;不能关联至单独分区,OS启动就会用到的程序
/sbin:管理类的基本命令;不能关联至独立分区,OS启动就会用到的程序
/lib:启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
/lib64:专用于x86_64系统(就是64位系统)上的辅助共享库文件存放位置
/etc:配置文件目录
/home/$USERNAME:普通用户家目录
/root:管理员的家目录
/media:便携式移动设备挂载点
/mnt:零时文件挂载点
/dev:设备文件以及特殊文件存放位置。

  • b(块设备):block device,随机访问
  • c(字符设备):character device,线性访问

/opt:第三方应用程序的安装位置
/srv:系统上运行的服务用到的数据
/tmp:临时文件存放的位置
/usr:universal shared,read-only data

  • bin:保证系统拥有完整功能而提供的应用程序
  • sbin:
  • lib:34位系统使用
  • lib:64位系统使用
  • inclue: C程序的头文件(haader files)
  • share:结构化独立的数据,例如doc,man等
    • local:第三方应用程序的位置
      • bin,sbin,lib,lib64,etc,share

/var: 会变化的文件,比如日志这些就放在这里

  • cache: 应用程序的缓存数据目录
  • lib:应用程序状态信息数据
  • local:专用于为/usr/local下的应用程序存储可变数据
  • lock:锁文件
  • log:日志目录以及文件
  • opt: 专用于为/opt下的应用程序存储可变数据
  • run:运行中的进程相关数据,通常用于存储进程pid文件
  • spool:应用程序数据池
  • tmp:保存系统两次重启之间产生的临时数据

/proc:用于输出内核与进程信息相关的虚拟文件系统
/sys:用于输出当前系统上硬件设备相关信息虚拟文件系统、
/selinux: security enhanced linux,selinux相关的安全策略等信息的存放位置。

1.3 应用程序的组成部分


二进制程序:/bin,/sbin,/usr/bin,/usr/sbin,/usr/local/bin,/usr/local/sbin
库文件:/lib,/lib64,/usr/lib,/usr/lib64,/usr/local/lib,/usr/local/lib64
配置文件:/etc, /etc/$DIRECTORY,/usr/local/etc
帮助文件:/usr/share/man,/usr/share/doc,/usr/local/share/mane,/usr/local/share/doc

1.5 linux下的文件类型


    • 普通文件(常用)
  • d 目录文件(常用)
  • l 链接文件(常用)
  • b 块设备文件
  • c 字符设备文件
  • p 管道文件pipe(注:程序之间单向传输用,不常用)
  • s 套接字文件socke(注:程序之间共享传输用,不常用)

范例:

[15:32:14 root@cento7-fyg ~]#ll /run/
总用量 28
-rw-r--r--.  1 root   root      5 125 15:41 auditd.pid
drwxr-xr-x.  2 root   root     80 125 15:41 blkid
drwxr-x---.  2 chrony chrony   60 125 15:45 chrony
prw-------.  1 root   root      0 125 15:40 dmeventd-server
-rw-------.  1 root   root      0 125 15:41 ebtables.lock
drwxr-xr-x.  2 root   root     40 125 15:41 faillock
......

2 文件操作命令


2.1 显示当前工作目录


每个shell和系统进程都有一个当前的工作目录 CWD:current work directory
显示当前shell CWD的绝对路径
pwd命令: printing working directory

  • -P 显示真实物理路径
  • -L 显示链接路径(默认)

范例:

[15:36:06 root@cento7-fyg ~]#pwd
/root
[15:36:08 root@cento7-fyg ~]#cd /etc
[15:36:11 root@cento7-fyg etc]#pwd
/etc

2.2绝对和相对路径


  • 绝对路径:

从根/开始写的,完成的文件路径

  • 相对路径:

不以斜线开始
一般情况下,是指相对于当前工作目录的路径,特殊场景下,是相对于某目录的位置
可以作为一个简短的形式指定一个文件名
经常用于要移动项目中。

2.3 更换工作目录


命令cd:
选项:-P 切换至物理路径,而非软连接目录
常用的几个方式:

  • 切换至父目录: cd ..
  • 切换至当前用户主目录: cd
  • 切换至以前的工作目录: cd -

范例:

[15:43:06 root@cento7-fyg bin]#ll /bin
lrwxrwxrwx. 1 root root 7 103 2013 /bin -> usr/bin
[15:43:08 root@cento7-fyg bin]#cd /bin
[15:43:10 root@cento7-fyg bin]#pwd
/bin
[15:43:12 root@cento7-fyg bin]#cd -P /bin
[15:43:17 root@cento7-fyg bin]#pwd
/usr/bin

相关的环境变量:

  • PWD:当前目录的路径
  • OLDPWD:上一次目录路径

范例:

[15:44:52 root@cento7-fyg ~]#cd /bin
[15:44:56 root@cento7-fyg bin]#echo $OLDPWD
/root
[15:45:08 root@cento7-fyg bin]#cd -
/root
[15:45:11 root@cento7-fyg ~]#pwd
/root

2.4 列出目录内容


ls命令可以列出当前目录的内容或指定目录
用法

ls [options] [files_or_dirs]

常见的选项

  • -a 包含隐藏文件
  • -l 显示额外的信息
  • -R 目录递归
  • -ld 目录和符号链接信息
  • -1 文件分行显示
  • -S 按从大到小排序
  • -t 按mtime排序 (什么是mtime ?下一节会说到)
  • -u 配合-t选项,显示并按atime从新到旧排序 (什么是atime? 下一节也会说到)
  • -U 按目录存放顺序显示
  • -X 按文件后缀排序
  • -F 对不同类型文件显示时附加不同的符号:*/=>@|
  • -C 文件多时,以多列的方式显示文件,默认是一列(标准输出)

说明:

  • ls 查看不同后缀文件时的颜色由 /etc/DIR_COLORS$LS_COLORS变量定义
  • ls -l 看到文件的大小,不一定是实际文件真正占用空间的大小

范例:我想让后缀为jpg的文件呈现为红色

[15:51:46 root@cento7-fyg ~]#echo '.jpg 01;31' >> /etc/DIR_COLORS #退出重进

在这里插入图片描述

范例:ll别名

[15:55:58 root@cento7-fyg ~]#alias ll
alias ll='ls -l --color=auto'

2.5查看文件的状态stat


文件相关信息:metedata,data(这块的话后面讲还会讲到,与文件系统的设计有关)。
每个文件有三个时间戳:(在metadata中)

  • access time 访问时间,atime,读取文件内容
  • modify time 修改时间,mtime,改变文件内容(数据)
  • change time 改变时间,ctime,元数据发生改变

#注意:

  • 默认ls看到的是modify时间
  • access的时间更新机制(任意一个都可以),这些机制与relatime有关。mount时候的relatime。
    • atime和访问的时间差一天,他就会更新atime
    • modify时间比atime时间新,访问他就会更新
[16:00:58 root@cento7-fyg ~]#stat test.jpg
  文件:"test.jpg"
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:16866086    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-29 15:54:44.253078207 +0800
最近更改:2022-01-29 15:54:44.253078207 +0800
最近改动:2022-01-29 15:54:44.253078207 +0800
创建时间:- #想看这个就要装工具,现在没必要纠结

#默认ls是mtime,想看其他time的方法。
[22:03:11 root@cento7-fyg data]#ll --time=atime
总用量 0
-rw-r--r--. 1 root root 0 131 21:44 file1.txt
-rw-r--r--. 1 root root 0 131 21:43 file*.txt
-rw-r--r--. 1 root root 0 129 16:52 linux.txt

[22:03:37 root@cento7-fyg data]#ll --time=ctime
总用量 0
-rw-r--r--. 1 root root 0 131 21:44 file1.txt
-rw-r--r--. 1 root root 0 131 21:43 file*.txt
-rw-r--r--. 1 root root 0 129 16:52 linux.txt

2.6确定文件内容


相对于windows注重后缀,如果文本就是.txt,视频就是.avi、.mp4。linux的文件名称对后缀就没啥要求。
文件可以包含多种类型(类型如章节1.5所显示)的数据,使用file命令检查文件的类型,然后确定适当的打开命令或应用程序使用
格式:

file [options] <filename> ...

常用选项:

  • -b 列出文件辨识结果时,不显示文件名称
  • -f filelist列出文件filelist中文件名的文件类型
  • -F 使用指定分隔符号替换输出文件名后默认的”:”分隔符
  • -L 查看对应软链接对应文件的文件类型

范例:

[16:14:39 root@cento7-fyg ~]#ll
总用量 12
-rw-------. 1 root root 1433 103 2013 anaconda-ks.cfg
-rw-r--r--. 1 root root    6 129 16:14 linux.txt
-rw-r--r--. 1 root root    7 129 16:14 win.txt
[16:14:40 root@cento7-fyg ~]#cat linux.txt
a
b
c
[16:14:43 root@cento7-fyg ~]#cat win.txt
a
b
c[16:14:46 root@cento7-fyg ~]#
[16:14:47 root@cento7-fyg ~]#file win.txt linux.txt
win.txt:   ASCII text, with CRLF line terminators
linux.txt: ASCII text
[16:15:06 root@cento7-fyg ~]#hexdump -C linux.txt
00000000  61 0a 62 0a 63 0a                                 |a.b.c.|
00000006
[16:15:24 root@cento7-fyg ~]#hexdump -C win.txt
00000000  61 0d 0a 62 0d 0a 63                              |a..b..c|
00000007

#安装转换工具
[16:16:35 root@cento7-fyg ~]#yum install -y dos2unix
#转换
[16:17:03 root@cento7-fyg ~]#dos2unix win.txt
dos2unix: converting file win.txt to Unix format ...
#查看属性,发现两个文件属性已经相同了
[16:17:16 root@cento7-fyg ~]#file win.txt linux.txt
win.txt:   ASCII text
linux.txt: ASCII text

范例:-f参数的使用

[16:22:45 root@cento7-fyg ~]#vim list.txt
/etc/
/bin
/etc/issue

[16:22:59 root@cento7-fyg ~]#file -f list.txt
/etc/:      directory
/bin:       symbolic link to 'usr/bin'
/etc/issue: ASCII text

2.7文件通配符模式wildcard pattern


文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件
通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符
常见的通配符如下:(其他的请查看man 7 glob,如果无法man,请使用yum install -y man man-pages man-pages-overrides更新man信息)
红色:最常用;深红色:不常用

    • 匹配零个或多个字符,但不匹配 “.” 开头的文件,即隐藏文件
  • ​? 匹配任何单个字符,一个汉字也算一个字符
  • ​~ 当前用户家目录
  • ​~mage 用户mage家目录
  • . 和 ~+ 当前工作目录
  • ~- 前一个工作目录 (知道就好)
  • ​[0-9] 匹配数字范围
  • ​[a-z] 一个字母
  • ​[A-Z] 一个字母
  • [wang] 匹配列表中的任何的一个字符
  • [^wang] 匹配列表中的所有字符以外的字符
  • [^a-z] 匹配列表中的所有字符以外的字符
  • [:digit:]:任意数字,相当于0-9
  • [:lower:]:任意小写字母,表示 a-z
  • [:upper:]: 任意大写字母,表示 A-Z
  • [:alnum:]:任意数字或字母
  • [:blank:]:水平空白字符
  • [:space:]:水平或垂直空白字符
  • [:punct:]:标点符号
  • [:print:]:可打印字符
  • [:cntrl:]:控制(非打印)字符
  • [:graph:]:图形字符
  • [:xdigit:]:十六进制字符

范例:环境中 /data目录中有Linux.txt文件。
“使用前一个目录”除了cd命令可以用-,ll命令要用-~进行
在这里插入图片描述

范例:[]和{}


[16:59:48 root@cento7-fyg ~]#ll f{1..5}
ls: 无法访问f1: 没有那个文件或目录
ls: 无法访问f2: 没有那个文件或目录
-rw-r--r--. 1 root root 0 129 16:59 f3
-rw-r--r--. 1 root root 0 129 16:59 f4
-rw-r--r--. 1 root root 0 129 16:59 f5

[17:00:09 root@cento7-fyg ~]#ll f[1-5]
-rw-r--r--. 1 root root 0 129 16:59 f3
-rw-r--r--. 1 root root 0 129 16:59 f4
-rw-r--r--. 1 root root 0 129 16:59 f5

[21:18:09 root@cento7-fyg ~]#ll f[a-z]
-rw-r--r--. 1 root root 0 131 21:17 fa
-rw-r--r--. 1 root root 0 131 21:17 fA
-rw-r--r--. 1 root root 0 131 21:17 fb
-rw-r--r--. 1 root root 0 131 21:17 fc
-rw-r--r--. 1 root root 0 131 21:17 fC


[21:18:36 root@cento7-fyg ~]#ll f{a..z}
ls: 无法访问fd: 没有那个文件或目录
ls: 无法访问fe: 没有那个文件或目录
ls: 无法访问ff: 没有那个文件或目录
ls: 无法访问fg: 没有那个文件或目录
ls: 无法访问fh: 没有那个文件或目录
ls: 无法访问fi: 没有那个文件或目录
ls: 无法访问fj: 没有那个文件或目录
ls: 无法访问fk: 没有那个文件或目录
ls: 无法访问fl: 没有那个文件或目录
ls: 无法访问fm: 没有那个文件或目录
ls: 无法访问fn: 没有那个文件或目录
ls: 无法访问fo: 没有那个文件或目录
ls: 无法访问fp: 没有那个文件或目录
ls: 无法访问fq: 没有那个文件或目录
ls: 无法访问fr: 没有那个文件或目录
ls: 无法访问fs: 没有那个文件或目录
ls: 无法访问ft: 没有那个文件或目录
ls: 无法访问fu: 没有那个文件或目录
ls: 无法访问fv: 没有那个文件或目录
ls: 无法访问fw: 没有那个文件或目录
ls: 无法访问fx: 没有那个文件或目录
ls: 无法访问fy: 没有那个文件或目录
ls: 无法访问fz: 没有那个文件或目录
-rw-r--r--. 1 root root 0 131 21:17 fa
-rw-r--r--. 1 root root 0 131 21:17 fb
-rw-r--r--. 1 root root 0 131 21:17 fc

范例:
[]中的字符

[21:21:09 root@cento7-fyg ~]#touch file{a..z}.txt file{A-Z}.txt file{0..9}.txt
#查看一下创建的文件
[21:22:46 root@cento7-fyg ~]#ll file{0..9}.txt
-rw-r--r--. 1 root root 0 131 21:21 file0.txt
-rw-r--r--. 1 root root 0 131 21:21 file1.txt
-rw-r--r--. 1 root root 0 131 21:21 file2.txt
-rw-r--r--. 1 root root 0 131 21:21 file3.txt
-rw-r--r--. 1 root root 0 131 21:21 file4.txt
-rw-r--r--. 1 root root 0 131 21:21 file5.txt
-rw-r--r--. 1 root root 0 131 21:21 file6.txt
-rw-r--r--. 1 root root 0 131 21:21 file7.txt
-rw-r--r--. 1 root root 0 131 21:21 file8.txt
-rw-r--r--. 1 root root 0 131 21:21 file9.txt
[21:24:22 root@cento7-fyg ~]#ll file[0-9].txt
-rw-r--r--. 1 root root 0 131 21:21 file0.txt
-rw-r--r--. 1 root root 0 131 21:21 file1.txt
-rw-r--r--. 1 root root 0 131 21:21 file2.txt
-rw-r--r--. 1 root root 0 131 21:21 file3.txt
-rw-r--r--. 1 root root 0 131 21:21 file4.txt
-rw-r--r--. 1 root root 0 131 21:21 file5.txt
-rw-r--r--. 1 root root 0 131 21:21 file6.txt
-rw-r--r--. 1 root root 0 131 21:21 file7.txt
-rw-r--r--. 1 root root 0 131 21:21 file8.txt
-rw-r--r--. 1 root root 0 131 21:21 file9.txt
[21:24:53 root@cento7-fyg ~]#ll file[a-c].txt
-rw-r--r--. 1 root root 0 131 21:21 filea.txt
-rw-r--r--. 1 root root 0 131 21:21 fileA.txt
-rw-r--r--. 1 root root 0 131 21:21 fileb.txt
-rw-r--r--. 1 root root 0 131 21:21 fileB.txt
-rw-r--r--. 1 root root 0 131 21:21 filec.txt
[21:26:29 root@cento7-fyg ~]#ll file[C-E].txt
-rw-r--r--. 1 root root 0 131 21:26 fileC.txt
-rw-r--r--. 1 root root 0 131 21:26 filed.txt
-rw-r--r--. 1 root root 0 131 21:26 fileD.txt
-rw-r--r--. 1 root root 0 131 21:26 filee.txt
-rw-r--r--. 1 root root 0 131 21:26 fileE.txt

[21:27:27 root@cento7-fyg ~]#ls file[fang].txt
filea.txt  filef.txt  fileg.txt  filen.txt
[21:28:10 root@cento7-fyg ~]#ls file[^fang].txt | grep filef.txt
[21:28:41 root@cento7-fyg ~]#ls file[^fang].txt | grep filea.txt
[21:28:44 root@cento7-fyg ~]#ls file[^fang].txt | grep filen.txt
[21:28:48 root@cento7-fyg ~]#ls file[^fang].txt | grep fileg.txt

[21:35:30 root@cento7-fyg ~]#ls file[:lower:].txt
filee.txt  filel.txt  fileo.txt  filer.txt  filew.txt

[21:37:03 root@cento7-fyg ~]#ls file[[:lower:]].txt
filea.txt  filed.txt  fileg.txt  filej.txt  filem.txt  filep.txt  files.txt  filev.txt  filey.txt
fileb.txt  filee.txt  fileh.txt  filek.txt  filen.txt  fileq.txt  filet.txt  filew.txt  filez.txt
filec.txt  filef.txt  filei.txt  filel.txt  fileo.txt  filer.txt  fileu.txt  filex.txt

范例:*是不包括‘.’开头的文件的!

[21:37:20 root@cento7-fyg ~]#ls -a *
[21:39:40 root@cento7-fyg ~]#ls -a

范例:

[21:44:04 root@cento7-fyg data]#ll
总用量 0
-rw-r--r--. 1 root root 0 131 21:44 file1.txt
-rw-r--r--. 1 root root 0 131 21:43 file*.txt
-rw-r--r--. 1 root root 0 129 16:52 linux.txt
[21:44:05 root@cento7-fyg data]#ls file*.txt
file1.txt  file*.txt
[21:44:13 root@cento7-fyg data]#ls 'file*.txt'
file*.txt

请尝试自己做以下范例:
1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表
5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录列表
6、只显示/root下的隐藏文件和目录列表
7、只显示/etc下的非隐藏目录列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Microsoft Graph API 来下载 OneDrive 中的文件。这个API可以用于 .NET Framework 4.5.2 及以上版本,可以通过 NuGet 包管理器安装。以下是一个简单的示例代码: ```csharp using Microsoft.Graph; using Microsoft.Identity.Client; using System.IO; using System.Threading.Tasks; public class OneDriveService { private readonly string clientId; private readonly string[] scopes; private readonly string redirectUri; private readonly IPublicClientApplication publicClientApplication; private readonly GraphServiceClient graphClient; public OneDriveService(string clientId, string[] scopes, string redirectUri) { this.clientId = clientId; this.scopes = scopes; this.redirectUri = redirectUri; publicClientApplication = PublicClientApplicationBuilder .Create(clientId) .WithRedirectUri(redirectUri) .Build(); graphClient = new GraphServiceClient(new DelegateAuthenticationProvider( async (requestMessage) => { var authResult = await GetAccessTokenAsync(); requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken); })); } public async Task<Stream> DownloadFileAsync(string itemId) { var response = await graphClient.Me.Drive.Items[itemId].Content.Request().GetAsync(); return response.Content; } private async Task<AuthenticationResult> GetAccessTokenAsync() { var accounts = await publicClientApplication.GetAccountsAsync(); var result = await publicClientApplication.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync(); return result; } } ``` 在使用这个服务之前,你需要先注册一个 Azure 应用程序,并在应用程序中添加 OneDrive API 的权限。然后,在调用服务之前,你需要获取授权令牌(token)。这个示例代码中使用 Microsoft.Identity.Client 库来获取授权令牌,你需要提供应用程序的客户端 ID、权限范围和定向 URI。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值