目录
目录列表
pwd:显示目前的目录;ls命令输出的列表是按照字母排序的(按列排序而不是按行排序),选项参数之间可以组合如 ls -FR
-a | 显示全部的文件,连同隐藏的文件)一起列出来 |
-d | 仅列出目录本身,而不是列出目录内的文件数据 |
-l | 产生长列表格式的输出,包含文件的属性与权限等等数据 |
-F | 彩色区分显示文件和目录。目录后面加入(/),可执行文件加入*或@ |
-R | 递归选项,列出当前目录下的子目录中的文件 |
-
文件属性
$ ls -l
-rw-rw-r--. 1 root user_1 57 Aug 20 17:29 hello.c
-rwxrwxr-x. 1 root user_1 8440 Aug 20 17:29 hello.exe
drwxrwxr-x. 3 root user_1 19 Aug 20 16:39 test1
-
过滤输出列表
ls命令支持在命令行中定义过滤器,进行简单文本匹配的字符串。当用户指定特定文件的名称作为过滤器时,ls命令只会显示该文件的信息
$ ls -l my_script
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$
ls命令能够识别标准通配符,并在过滤器中用它们进行模式匹配:
- 问号(?)代表一个字符;可用于过滤器字符串中替代任意位置的单个字符。
- 星号(*)代表零个或多个字符。
$ ls -l my_scr?pt
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$ ls -l my*
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_file
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$
处理文件
-
创建文件
touch命令创建了指定的新文件,并将用户名作为文件的属主。文件的大小是零,因为touch命令只创建了一个空文件。
$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one
$
touch命令还可用来改变文件的修改时间。这个操作并不需要改变文件的内容。
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one
$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$
如果只想改变访问时间,可用-a参数。使用ls –l命令默认显示的是修改时间。要想查看文件的访问时间,需要加入另外一个参数:--time=atime。
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$ touch -a test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$ ls -l --time=atime test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:55 test_one
$
-
复制文件
cp命令需要两个参数——源对象和目标对象: cp source destination 。
- -p:连同文件的属性一起复制过去,而非使用默认属性;
- -i:若目标档(destination)已经存在时,在覆盖时会先询问动作的进行
- -r:递归持续复制,用于目录的复制行为;
当两个参数都是文件名,cp命令将源文件复制成一个新文件,并且以destination命名。新文件就像全新的文件一样,有新的修改时间。
$ cp test_one test_two
$ ls -l test_*
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
-rw-rw-r-- 1 christine christine 0 May 21 15:15 test_two
$
当两个参数有一个是目录,可以将文件复制到目录中。绝对路径和相对路径都可以,另外单点符(.)表示当前工作目录,能够简化该任务。
$ cp -i test_one /home/christine/Documents/
$ cp -i test_one Documents/
$ cp -i /etc/NetworkManager/NetworkManager.conf .
$ ls -l NetworkManager.conf
-rw-r--r-- 1 christine christine 76 May 21 15:55 NetworkManager.conf
$
cp命令的-R参数威力强大。可以用它在一条命令中递归地复制整个目录的内容,变为完整副本
$ ls -Fd *Scripts
Scripts/
$ ls -l Scripts/
total 25
-rwxrw-r-- 1 christine christine 929 Apr 2 08:23 file_mod.sh
-rwxrw-r-- 1 christine christine 254 Jan 2 14:18 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 Jan 2 13:42 SUID_search.sh
$ cp -R Scripts/ Mod_Scripts
$ ls -Fd *Scripts
Mod_Scripts/ Scripts/
$ ls -l Mod_Scripts
total 25
-rwxrw-r-- 1 christine christine 929 May 21 16:16 file_mod.sh
-rwxrw-r-- 1 christine christine 254 May 21 16:16 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 May 21 16:16 SUID_search.sh
$
也可以在cp命令中使用通配符,该命令将所有以script结尾的文件复制到Mod_Scripts目录中。在这里,只需要复制一个文件:my_script
$ cp *script Mod_Scripts/
$ ls -l Mod_Scripts
total 26
-rwxrw-r-- 1 christine christine 929 May 21 16:16 file_mod.sh
-rwxrw-r-- 1 christine christine 54 May 21 16:27 my_script
-rwxrw-r-- 1 christine christine 254 May 21 16:16 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 May 21 16:16 SUID_search.sh
$
制表键自动补全
制表键自动补全允许你在输入文件名或目录名时按一下制表键,让shell帮忙将内容补充完整。我们输入了命令cp really,然后按制表键,shell就将剩下的文件名自动补充完整了
$ ls really*
really_ridiculously_long_file_name
$ cp really_ridiculously_long_file_name Mod_Scripts/
-
重命名文件
重命名文件称为移动。mv命令可以将文件和目录移动到另一个位置或重新命名。
- -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
- -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
移动文件会将文件名从fall更改为fzll,但inode编号和时间戳保持不变。这是因为mv只影响文件名。
$ ls -li f?ll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fall
$ mv fall fzll
$ ls -li f?ll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll
$
mv来移动文件的位置,这个操作并没有改变文件的inode编号或时间戳,
$ ls -li /home/christine/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 /home/christine/fzll
$ ls -li /home/christine/Pictures/
total 0
$ mv fzll Pictures/
$ ls -li /home/christine/Pictures/
total 0
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll
$ ls -li /home/christine/fzll
ls: cannot access /home/christine/fzll: No such file or directory
$
mv命令移动文件位置并修改文件名称,
$ ls -li Pictures/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 Pictures/fzll
$ mv /home/christine/Pictures/fzll /home/christine/fall
$ ls -li /home/christine/fall
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 /home/christine/fall
$ ls -li /home/christine/Pictures/fzll
ls: cannot access /home/christine/Pictures/fzll: No such file or directory
-
删除文件
bash shell中删除文件的命令是rm。
- -f :就是 force 的意思,忽略不存在的文件,不会出现警告信息;
- -i :互动模式,在删除前会询问使用者是否动作
- -r :递归删除啊!最常用在目录的删除了!
也可以使用通配符删除成组的文件。别忘了使用-i选项保护好自己的文件。
$ rm -i f?ll
rm: remove regular empty file 'fell'? y
rm: remove regular empty file 'fill'? y
rm: remove regular empty file 'full'? y
$
$ ls -l f?ll
ls: cannot access f?ll: No such file or directory
$
-
连接文件
链接文件是Linux文件系统的一个优势。如需要在系统上维护同一文件的两份或多份副本,除了保存多份单独的物理文件副本之外,还可以采用保存一份物理文件副本和多个虚拟副本的方法。这种虚拟的副本就称为链接。链接是目录中指向文件真实位置的占位符。在Linux中有两种不同类型的文件链接:
- 符号链接
符号链接就是一个实实在在的文件,它指向存放在虚拟目录结构中某个地方的另一个文件。这两个通过符号链接在一起的文件,彼此的内容并不相同。使用ln命令以及-s选项来创建符号链接
$ ls -l data_file
-rw-rw-r-- 1 christine christine 1092 May 21 17:27 data_file
$
$ ln -s data_file sl_data_file
$
$ ls -l *data_file
-rw-rw-r-- 1 christine christine 1092 May 21 17:27 data_file
lrwxrwxrwx 1 christine christine 9 May 21 17:29 sl_data_file -> data_file
$
注意符号链接的名字sl_data_file位于ln命令中的第二个参数位置上。显示在长列表中符号文件名后的->符号表明该文件是链接到文件data_file上的一个符号链接
- 硬链接
硬链接会创建独立的虚拟文件,其中包含了原始文件的信息及位置。但是它们从根本上而言是同一个文件。引用硬链接文件等同于引用了源文件。用ln命令时不再需要加入额外的参数
$ ls -l code_file
-rw-rw-r-- 1 christine christine 189 May 21 17:56 code_file
$
$ ln code_file hl_code_file
$
$ ls -li *code_file
296892 -rw-rw-r-- 2 christine christine 189 May 21 17:56
code_file
296892 -rw-rw-r-- 2 christine christine 189 May 21 17:56
hl_code_file
$
处理目录
-
创建目录
mkdir命令创建目录
- -m :配置文件的权限喔!直接配置
- -p :帮助你直接将所需要的目录(包含上一级目录)递归创建起来!可以根据需要创建缺失的父目录
要想同时创建多个目录和子目录,需要加入-p参数:
$ mkdir -p New_Dir/Sub_Dir/Under_Dir
$
$ ls -R New_Dir
New_Dir:
Sub_Dir
New_Dir/Sub_Dir:
Under_Dir
New_Dir/Sub_Dir/Under_Dir:
$
-
删除目录
删除目录的基本命令是rmdir。默认情况下,rmdir命令只删除空目录。因为我们在New_Dir目录下创建了一个文件my_file,所以rmdir命令拒绝删除目录。
$ touch New_Dir/my_file
$ ls -li New_Dir/
total 0
294561 -rw-rw-r-- 1 christine christine 0 May 22 09:52 my_file
$
$ rmdir New_Dir
rmdir: failed to remove 'New_Dir': Directory not empty
$
也可以在整个非空目录上使用rm命令。使用-r选项使得命令可以向下进入目录,删除其中的文件,然后再删除目录本身。
$ ls -l My_Dir
total 0
-rw-rw-r-- 1 christine christine 0 May 22 10:02 another_file
$
$ rm -ri My_Dir
rm: descend into directory 'My_Dir'? y
rm: remove regular empty file 'My_Dir/another_file'? y
rm: remove directory 'My_Dir'? y
$
$ ls -l My_Dir
ls: cannot access My_Dir: No such file or directory
$
一口气删除目录及其所有内容的终极大法就是使用带有-r参数和-f参数的rm命令
$ rm -rf My_Dir
查看文件内容
-
文件类型
file命令是一个随手可得的便捷工具。它能够探测文件的内部,并决定文件是什么类型的:
$ file my_file
my_file: ASCII text
$ file New_Dir
New_Dir: directory
$
-
查看整个文件
cat命令是显示文本文件中所有数据的得力工具
- -n :参数会给所有的行加上行号,nl命令有同样效果
- -b:如果只想给有文本的行加上行号
- -T:如果不想让制表符出现;用^I字符组合去替换文中的所有制表符
$ cat test1
hello
This is a test file.
That we'll use to test the cat command.
$ cat -n test1
1 hello
2
3 This is a test file.
4
5
6 That we'll use to test the cat command.
$ cat -b test1
1 hello
2 This is a test file.
3 That we'll use to test the cat command.
$ cat -T test1
hello
This is a test file.
That we'll use to^Itest the cat command.
$
more命令是分页工具,会在显示每页数据之后停下来;less命令的操作和more命令基本一样,一次显示一屏的文件文本。除了支持和more命令相同的命令集,它还包括更多的选项,可以输入man less浏览对应的手册页
-
查看部分文件
cat或more在加载完整个文件之后才能看到。如果数据是在文件的末尾,怎么查看。
tail命令会显示文件最后几行的内容默认情况下,它会显示文件的末尾10行
- -n:参数来修改所显示的行数
- -f:允许你在其他进程使用该文件时查看文件的内容;tail命令会保持活动状态,并不断显示添加到文件中的内容。用于实时监测系统日志
$ tail log_file
$ tail -n 2 log_file
head命令会显示文件开头那些行的内容。默认情况下,它会显示文件前10行的文本
- -n:参数来修改所显示的行数,允许你在破折号后面输入想要显示的行数
$ head -5 log_file
文件权限
$ ls –l
total 68
-rw-rw-r-- 1 rich rich 50 2010-09-13 07:49 file1.gz
-rw-rw-r-- 1 rich rich 23 2010-09-13 07:50 file2
-rw-rw-r-- 1 rich rich 48 2010-09-13 07:56 file3
-rw-rw-r-- 1 rich rich 34 2010-09-13 08:59 file4
[ d ]是目录、[ - ]是文件、[c]字符型设备、[l]连接、[n]网络设备、[b]块设备;
3组三字符编码,每一组定义3种访问权限:[r]对象可读、[w]对象可写、[x]对象可执行、[-]没有权限。
3组权限分别对应对象的3个安全级别:[对象的属主、对象的属组、系统其他用户]
权限 | 二进制值 | 八进制值 |
--- | 000 | 0 |
--x | 001 | 1 |
-w- | 010 | 2 |
-wx | 011 | 3 |
r-- | 100 | 4 |
r-x | 101 | 5 |
rw- | 110 | 6 |
rwx | 111 | 7 |
-
改变权限
chmod命令用来改变文件和目录的安全性设置
chmod options mode file
options: -R : 进行递归(recursive)的持续变更,亦即连同次目录下的所有文件都会变更
mode:可以采用八进制模式或者符号模式进行设置
- 八进制:直接用期望赋予文件的标准3为八进制权限码就可以
$ chmod 777 file.txt
- 符号模式的mode为:[ugoa][+-=][rwxXstugo]
u | 代表用户 |
g | 代表组 |
o | 代表其他用户 |
a | 代表上述所有用户 |
+ | 增加权限 |
- | 移除权限 |
= | 将权限设置成后面的值 |
rwx | 读-写-执行 |
X | 如果对象是目录或它已经有执行权限,则赋予执行权限 |
u | 将权限设置和属主一样 |
g | 将权限设置和属组一样 |
o | 将权限设置和其他用户一样 |
$ chmod o+r file
$ chmod u-x file
-
改变所属关系
chown命令用来改变文件的属主,chgrp命令用来改变文件的默认属组。
chown options owner[.group] file
$ chown dan newfile
$ ls -l newfile
-rw-rw-r-- 1 dan rich 0 Sep 20 19:16 newfile
$ chgrp shared newfile
$ ls -l newfile
-rw-rw-r-- 1 rich shared 0 Sep 20 19:16 newfile
chown命令也支持同时改变文件的属主和属组
$ chown dan.shared newfile
$ ls -l newfile
-rw-rw-r-- 1 dan shared 0 Sep 20 19:16 newfile