Linux系统管理学习笔记2-文件管理

1.Linux系统的目录结构

Linux的根目录下分别具有以下文件夹 /bin /boot/dev/etc/home/root/lib/srv/sbin/tmp/opt/media

不同的目录存放不同类型的文件。

2.查看当前所处目录

指令:pwd

3.目录之间的切换cd

指令:cd [目录路径]
 
变换工作目录至制定目录路径,若[目录路径]参数省略则变换至使用者的家目录,其中[目录路径]可为绝对路径或相对路径
 
另外  "~" 在Bash中表示当前用户家目录,  "." 表示当前所在的目录 ".." 表示当前所在目录的上一层目录,”-” 则表示本次跳转前所处的目录。"/"根目录

使用举例

[dxw@localhost ~]$ cd /etc
[dxw@localhost etc]$ cd
[dxw@localhost ~]$ cd /etc
[dxw@localhost etc]$ cd ..
[dxw@localhost /]$ cd /etc/sysconfig/
[dxw@localhost sysconfig]$ cd ..
[dxw@localhost etc]$ cd sysconfig
[dxw@localhost sysconfig]$ cd -
/etc
[dxw@localhost etc]$ cd ..
[dxw@localhost /]$ cd ~
[dxw@localhost ~]$ cd /
[dxw@localhost /]$
4.目录到创建与删除

创建:指令 mkdir 

用法:mkdir [选项]... 目录...
 
若指定目录不存在则创建目录。
¨       -m,  --mode=模式    设置权限模式(类似chmod命令)
 
¨       -p   需要时创建目标目录的上层目录,但即使这些目录已存在也不当作错误处理

举例:

[dxw@localhost ~]$ mkdir -v test
mkdir: created directory `test'
[dxw@localhost ~]$ mkdir -m 755 test/test1
[dxw@localhost ~]$ mkdir test/test1/test2/test3
mkdir: cannot create directory `test/test1/test2/test3': No such file or directory
[dxw@localhost ~]$ mkdir -p test/test1/test2/test3
[dxw@localhost ~]$ cd test/test1/test2/test3
[dxw@localhost test3]$

删除:指令rmdir

用法:rmdir[选项]... 目录...
 
若指定目录不存在则创建目录。
¨       -m,  --mode=模式    设置权限模式(类似chmod命令)
 
¨       -p  嵌套删除空目录

        -v    打印执行结果
举例:

[dxw@localhost ~]$ rmdir test/test1/test2/test3
[dxw@localhost ~]$ rmdir -v test/test1/test2
rmdir: removing directory, `test/test1/test2'
[dxw@localhost ~]$ rmdir -p test/test1
[dxw@localhost ~]$ cd test
bash: cd: test: No such file or directory

注意:

只能删除空的目录(文件夹)

5.列出文件和文件属性

指令:ls

用法:ls [选项]... [文件]...
 
列出 FILE 的信息(默认为当前目录)。
 
如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序。
 
¨       -a 不隐藏任何以. 开始的项目
¨       -A  列出除. 及.. 以外的任何项目
¨       -c   按ctime(文件属性或者内容最后更改的时间) 排序;同-lt 一起使用:根据ctime 排序并显示ctime;同-l一起使用:显示ctime 但根据名称排序。
¨       -d   当遇到目录时列出目录本身而非目录内的文件
¨       -F   加上文件类型的指示符号(*/=@| 其中一个)
¨       -h   与-l一起,以易于阅读的格式输出文件大小 (例如 1K、 234M、 2G等,默认情况下的单位是Byte)
¨       -i    显示每个文件的inode 号
¨       -l    使用较长格式列出信息
¨       -n   类似 -l,但列出UID 及GID 号
¨       -r    排序时保留顺序
¨       -R  递归显示子目录
¨       -S   根据文件大小排序
¨       -t  根据mtime(文件内容最后修改时间)排序
¨       -u  按照atime(文件最后访问时间)排序;同-lt 一起使用:按照访问时间排序并显示;同-l一起使用:显示访问时间并按文件名排序。

举例:

[dxw@localhost ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[dxw@localhost ~]$ ls -l
total 32
drwxr-xr-x. 3 dxw dxw 4096 Oct 17 09:28 Desktop
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Documents
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Downloads
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Music
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Pictures
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Public
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Templates
drwxr-xr-x. 2 dxw dxw 4096 Oct 17 09:17 Videos
[dxw@localhost ~]$ ls -al
total 152
drwx------. 25 dxw  dxw  4096 Oct 29 10:23 .
drwxr-xr-x.  3 root root 4096 Oct 17 17:16 ..
-rw-------.  1 dxw  dxw   296 Oct 20 10:17 .bash_history

文件属性

举例

drwxr-xr-x.                           2                        dxw              dxw             4096              Oct 17 09:17                Downloads

文件类型及访问权限    硬链接数               属主               属组          文件大小        最后修改日期                文件名(目录也是文件)

重点说明:

drwxr-xr-x         第一位说明文件的类型 此处为d,该文件为目录。常见文件类型如下

1.       -        常规文件
2.       d       目录文件
3.       l         符号链接文件
4.       c        字符特殊设备文件
5.       b       模块特殊设备文件
6.       p       FIFO管道文件
7.       s        套接字文件

6.修改文件权限

指令:chmod

用法:chmod [选项]... 模式[,模式]... 文件...
 
 或:chmod [选项]... 八进制模式文件...
 
 或:chmod [选项]...  --reference=参考文件文件...
 
将每个文件的模式更改为指定值。
 
¨       --reference=参考文件   使用指定参考文件的模式,而非指定权限值
¨       -R,  --recursive   以递归方式更改所有的文件及子目录

举例:

[dxw@localhost ~]$ ls -l test
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 24 01:55 test2.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 24 01:54 test.txt
[dxw@localhost ~]$ chmod 555 test/test.txt
[dxw@localhost ~]$ ls -l test
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 24 01:55 test2.txt
-r-xr-xr-x. 1 dxw dxw 0 Nov 24 01:54 test.txt
指令chown

用法:chown [选项]... [所有者][:[组]] 文件...
 
 或:chown [选项]...  --reference=参考文件文件...
 
更改每个文件的所有者和/或所属组。当使用  --referebce 参数时,将文件的所有者和所属组更改为与指定参考文件相同,注意,只有root用户才能变更文件的所有者,只有某用户组的成员用户才能将文件组设置为为该用户组。
 
¨       --reference=参考文件使用参考文件的相关属性,而非指定值
¨       -R,  --recursive   递归处理所有的文件及子目录

举例:

[root@localhost dxw]# touch test/test3
[root@localhost dxw]# ls -l test
total 0
-rw-rw-r--. 1 dxw  dxw  0 Nov 24 01:55 test2.txt
-rw-r--r--. 1 root root 0 Nov 24 01:59 test3
-r-xr-xr-x. 1 dxw  dxw  0 Nov 24 01:54 test.txt
[root@localhost dxw]# chown dxw test3
chown: cannot access `test3': No such file or directory
[root@localhost dxw]# chown dxw test/test3
[root@localhost dxw]# ls -l test
total 0
-rw-rw-r--. 1 dxw dxw  0 Nov 24 01:55 test2.txt
-rw-r--r--. 1 dxw root 0 Nov 24 01:59 test3
-r-xr-xr-x. 1 dxw dxw  0 Nov 24 01:54 test.txt
[root@localhost dxw]# chown root:dxw test/test3
[root@localhost dxw]# ls -l test
total 0
-rw-rw-r--. 1 dxw  dxw 0 Nov 24 01:55 test2.txt
-rw-r--r--. 1 root dxw 0 Nov 24 01:59 test3

-r-xr-xr-x. 1 dxw  dxw 0 Nov 24 01:54 test.tx

注意:只用root才能修改文件拥有者

7.复制、删除、移动

复制指令:cp

用法:cp [选项]... [-T] 源文件目标文件
 
 或:cp [选项]... 源文件... 目录
 
 或:cp [选项]... -t 目录源文件...
 
将源文件复制至目标文件,或将多个源文件复制至目标目录。

¨         -f       如果目标文件无法打开则将其移除并重试
 
¨         -i       覆盖前询问(使-n选项失效)
 
¨         -l       只创建硬链接文件而不复制文件
 
¨         -n     不覆盖已存在的文件(使-i选项失效)
 
¨         -p     复制时保留文件的访问权限,拥有者,文件组和时间戳
 
¨         -R     递归复制目录及其子目录内的所有内容
 
¨         -s      只创建符号链接而不复制文件
 
¨         -u     只在源文件文件比目标文件新,或目标文件不存在时才进行复制

用法举例:

[dxw@localhost ~]$ ls -l ./dir1/dir2
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:01 test.txt
[dxw@localhost ~]$ cp dir1/test.txt ./dir1/dir2/test1.txt
[dxw@localhost ~]$ ls -l ./dir1/dir2
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:02 test1.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:01 test.txt
[dxw@localhost ~]$ cp dir1/test2.txt dir1/test3.txt ./dir1/dir2/
[dxw@localhost ~]$ cp -R dir1/dir2 ~
[dxw@localhost ~]$ ls -l ./dir2
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test1.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test2.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test3.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test.txt

删除指令:rm

用法:rm [选项]... 文件...
 
删除 (unlink) 文件。
 
¨         -f       强制删除。忽略不存在的文件,不提示确认
 
¨         -i       在删除前需要确认(默认选项)
 
¨         -R     递归删除目录及其内容
举例:

[dxw@localhost ~]$ ls -l dir2
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test1.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test2.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test3.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test.txt
[dxw@localhost ~]$ rm dir2/test.txt
[dxw@localhost ~]$ ls -l dir2
total 0
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test1.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test2.txt
-rw-rw-r--. 1 dxw dxw 0 Nov 26 03:04 test3.txt
[dxw@localhost ~]$ ls -l dir1
total 4
drwxrwxr-x. 2 dxw dxw 4096 Nov 26 03:07 dir2
-rw-rw-r--. 1 dxw dxw    0 Nov 25 05:27 test2.txt
-rw-rw-r--. 1 dxw dxw    0 Nov 25 05:27 test3.txt
-rw-rw-r--. 1 dxw dxw    0 Nov 25 05:27 test.txt
[dxw@localhost ~]$ rm -Rf dir1
[dxw@localhost ~]$ ls
Desktop  dir2  Documents  Downloads  Music  Pictures  Public  Templates  Videos

移动指令:mv
用法:mv [选项]... [-T] 源文件目标文件
 
 或:mv [选项]... 源文件... 目录
 
 或:mv [选项]... -t 目录源文件...
 
将源文件重命名为目标文件,或将源文件移动至指定目录。
 
¨         -f       覆盖前不询问
 
¨         -i       覆盖前询问
 
¨         -n     不覆盖已存在文件
 
¨         -u     只在源文件文件比目标文件新,或目标文件不存在时才进行移动
 
如果指定了-i、-f、-n 中的多个,仅最后一个生效。

注意: mv 没有-R 选项,mv可以直接移动目录。
[dxw@localhost dir2]$ cd ~
[dxw@localhost ~]$ mv dir2/test1.txt dir2/test1_newname.txt
[dxw@localhost ~]$ ls ./dir2
test1_newname.txt  test2.txt  test3.txt
[dxw@localhost ~]$ mv dir2/test2.txt dir2/test3.txt ~/
[dxw@localhost ~]$ ls
Desktop  Documents  Music     Public     test2.txt  Videos
dir2     Downloads  Pictures  Templates  test3.txt
[dxw@localhost ~]$ mv dir2/test1_newname.txt ~/test2.txt
[dxw@localhost ~]$ ls ./dir2
[dxw@localhost ~]$ ls
Desktop  Documents  Music     Public     test2.txt  Videos
dir2     Downloads  Pictures  Templates  test3.txt
[dxw@localhost ~]$ mv -i test2.txt test3.txt
mv: overwrite `test3.txt'? y

8.创建链接

用法:ln [选项]... 目标链接名        (第一种格式)
 
 或:ln [选项]... 目标                    (第二种格式)
 
 或:ln [选项]... 目标... 目录        (第三种格式)
 
 
在第一种格式中,创建指定名称且指向指定目标链接。
 
在第二种格式中,在当前目录创建指向目标位置的链接。
 
在第三种格式中,在指定目录中创建指向指定目标的链接。
 
默认创建硬链接,当使用-s 时创建符号链接。当创建硬链接时每个指定的目标都必须存在。符号链接可以指向任意的位置(包括不存在的位置)。
 
¨       -d   创建指向目录的硬链接(只适用于超级用户)
¨       -f,   强行覆盖任何已存在的目标文件
¨       -i    覆盖文件前进行确认
¨       -s   创建符号链接

  普通用户没有创建目录硬链接的权限,只有根用户可以通过“ln –f”命令强制为目录创建硬链接
 
¨       虽然根用户是可以为目录创建硬链接的。但是我们不推荐这样做,因为创建目录硬链接有可能会在文件系统中引入目录遍历的死循环而造成文件系统的混乱,具体原因可参见下面的链接http://c2.com/cgi/wiki?HardLink。
 
¨       例外情况是每个目录中目录本身及其父目录(. 和 ..)这两个硬链接。

¨       删除一个符号链接不会影响到这个符号链接指向的目标文件或目录;
 
¨       反过来,删除、移动或者重命名某个符号链接所指向的目标文件或目录不会导致这个符号链接被删除,只会让这个符号链接“断裂”。

¨       如果一个文件有多个硬链接,仅在其最后一个硬链接被删除且硬链接数为 0 时文件才会被真正从文件系统中删除;
 
¨       由于inode 仅在特定文件系统内是惟一的,因此硬链接不能够跨越文件系统;
 
¨       符号链接没有上面的限制,具有更大的灵活性,甚至可以跨越不同机器、不同网络对文件进行链接。[dxw@localhost ~]$ cp dir1/test.txt ./dir1/dir2/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值