N26 第二周
1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關(guān)示例演示。
[1]mkdir : make directories
mkdir [OPTION]... DIRECTORY...
example:
[root@localhost test]# ls
[root@localhost test]# mkdir a
[root@localhost test]# ls
a
-p:自動按需創(chuàng)建父目錄(-p, --parents: no error if existing, make parent directories as needed)
[root@localhost test]# mkdir -p a/b
-v, --verbose 顯示創(chuàng)建過程
[root@localhost test]# mkdir -pv a/b/c
mkdir: 已創(chuàng)建目錄 "a/b"
mkdir: 已創(chuàng)建目錄 "a/b/c"
-m, --mode=M set file mode (as in chmod), not a=rwx - umask
[root@localhost test]# mkdir b
[root@localhost test]# mkdir -m 666 c
[root@localhost test]# ll
總用量 0
drwxr-xr-x. 4 root root 22 2月 5 13:35 a
drwxr-xr-x. 2 root root 6 2月 5 13:38 b
drw-rw-rw-. 2 root root 6 2月 5 13:38 c
[2]rmdir : remove empty directories
rmdir [OPTION]... DIRECTORY...
example:
[root@localhost test]# tree
.
├── a
│ ├── b
│ │ └── c
│ └── d
└── c
5 directories, 0 files
[root@localhost test]# rmdir c
[root@localhost test]# ls
a
-p:刪除目錄后,如果其父目錄為空,則一并刪除之
-v:顯示過程
[root@localhost test]# tree a
a
└── b
1 directory, 0 files
[root@localhost test]# rmdir -pv a/b
rmdir: 正在刪除目錄 "a/b"
rmdir: 正在刪除目錄 "a"
[3]tree命令
tree - list contents of directories in a tree-like format
tree [options] [directory]
-L level:指定要顯示的層級
example:
[root@localhost test]# tree
.
└── a
└── b
└── c
3 directories, 0 files
[root@localhost test]# tree -L 2
.
└── a
└── b
2 directories, 0 files
[4]cat命令和tac命令
cat [OPTION]... [FILE]... #concatenate files and print on the standard output
-n : 顯示輸出行數(shù)
[root@localhost test]# cat -n b
1 a
2 a
3 a
[root@localhost test]# tac --help b
用法:tac [選項]... [文件]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-b, --before 在行前而非行尾添加分隔標志
-r, --regex 將分隔標志視作正則表達式來解析
-s, --separator=字符串 使用指定字符串代替換行作為分隔標志
[5]more命令和less命令
more命令:
more FILE
特點:翻屏至文件尾部后自動退出
less命令和more命令相反
[6]head命令
查看文件的前n行
head [options] FILE
-n #=-#
[root@localhost test]# head b
a
b
c
[root@localhost test]# head -n 2 b
a
b
[root@localhost test]# head -2 b
a
b
[7]tail命令:
查看文件的后n行
tail [options] FILE
-n #=-#
[root@localhost test]# tail b
a
b
c
[root@localhost test]# tail -n 2 b
b
c
[root@localhost test]# tail -2 b
b
c
-f:查看文件尾部內(nèi)容結(jié)束后不退出,跟隨顯示新增的行

[8]stat命令
stat -display file or file system status
stat FILE...
-f:--file-system display file system status instead of file status
example:
[root@localhost test]# stat a
文件:"a"
大小:14 塊:0 IO 塊:4096 目錄
設(shè)備:fd03h/64771d Inode:8404856 硬鏈接:3
權(quán)限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-02-05 14:21:28.944173969 +0800
最近更改:2017-02-05 14:21:28.944173969 +0800
最近改動:2017-02-05 14:21:28.944173969 +0800
創(chuàng)建時間:-
[root@localhost test]# stat -f a
文件:"a"
ID:fd0300000000 文件名長度:255 類型:xfs
塊大?。?span style="font-family:Calibri">4096 基本塊大小:4096
塊:總計:1277440 空閑:1244764 可用:1244764
Inodes: 總計:5120000 空閑:5115491
[9]touch 命令
touch - change file timestamps
touch [OPTION]... FILE
example:
-c --no create 不創(chuàng)建文件
[root@localhost test]# touch -c c
[root@localhost test]# ls
a b
-t 改成指定的時間 synopsis:YYMMDDHHMM.SS
-a change only the access time
[root@localhost test]# stat a
文件:"a"
大?。?span style="font-family:Calibri">0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-02-05 19:20:33.997396041 +0800
最近更改:2017-02-05 19:20:33.997396041 +0800
最近改動:2017-02-05 19:20:33.997396041 +0800
創(chuàng)建時間:-
[root@localhost test]# touch -a -t 1612261759.56 a
[root@localhost test]# stat a
文件:"a"
大小:0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2016-12-26 17:59:56.000000000 +0800
最近更改:2017-02-05 19:20:33.997396041 +0800
最近改動:2017-02-05 19:20:48.373396219 +0800
創(chuàng)建時間:--
-m change only the modification time
-r --references use this file's times instead of current time
[root@localhost test]# stat a
文件:"a"
大?。?span style="font-family:Calibri">14 塊:0 IO 塊:4096 目錄
設(shè)備:fd03h/64771d Inode:8404856 硬鏈接:3
權(quán)限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2016-01-12 17:58:36.000000000 +0800
最近更改:2017-02-05 14:21:28.944173969 +0800
最近改動:2017-02-05 15:22:34.117219326 +0800
創(chuàng)建時間:-
[root@localhost test]# touch -r a c
[root@localhost test]# stat c
文件:"c"
大小:0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2016-01-12 17:58:36.000000000 +0800
最近更改:2017-02-05 14:21:28.944173969 +0800
最近改動:2017-02-05 15:36:29.399229662 +0800
創(chuàng)建時間:-
[10]cp命令:copy
源文件:目標文件
單源復制:cp [OPTION]... [-T] SOURCE DEST
如果DEST不存在:則事先創(chuàng)建此文件,并復制源文件的數(shù)據(jù)流至DEST中
[root@localhost test]# ls
[root@localhost test]# touch 1
[root@localhost test]# mkdir a
[root@localhost test]# cp 1 a/2
[root@localhost test]# ls a
2
如果DEST存在:
如果DEST是非空目錄文件,則覆蓋目標文件
[root@localhost test]# cat 1
111
[root@localhost test]# cat a/1
[root@localhost test]# cp 1 a/
cp:是否覆蓋"a/1"? y
[root@localhost test]# cat a/1
111
如果DEST是目錄文件,則先在DEST目錄下創(chuàng)建一個與源文件同名的文件,并
復制數(shù)據(jù)流
[root@localhost test]# ls
1 a
[root@localhost test]# ls a
2
[root@localhost test]# cp 1 a/
[root@localhost test]# ls a
1 2
多源復制:cp [OPTION]...SOURCE...DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
如果DEST不存在,錯誤
[root@localhost test]# ls
1 2
[root@localhost test]# cp {1,2} a
cp: 目標"a" 不是目錄
如果DEST存在:
如果DEST是非目錄文件,錯誤
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 2月 5 16:46 1
-rw-r--r--. 1 root root 0 2月 5 16:46 2
-rw-r--r--. 1 root root 0 2月 5 16:48 3
[root@localhost test]# cp {1,2} 3
cp: 目標"3" 不是目錄
如果DEST是目錄文件,分別復制每個文件至目標目錄中,并保持原名
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 2月 5 16:46 1
-rw-r--r--. 1 root root 0 2月 5 16:46 2
-rw-r--r--. 1 root root 0 2月 5 16:48 3
drwxr-xr-x. 2 root root 6 2月 5 16:49 a
[root@localhost test]# cp {1,2,3} a
[root@localhost test]# ls a
1 2 3
常用選項:
-i:--interactive交互式復制,既覆蓋之前提醒用戶確認
[root@localhost test]# ls
1 2 3 a
[root@localhost test]# ls a
1 2 3
[root@localhost test]# cp -i 1 a/
cp:是否覆蓋"a/1"? y
-f:--force強制覆蓋目標文件
-r=-R:遞歸復制目錄
[root@localhost test]# ls
1 2 3 a
[root@localhost test]# mkdir b
[root@localhost test]# cp a b
cp: 略過目錄"a"
[root@localhost test]# cp -r a b
[root@localhost test]# ls b
a
-d:復制符號鏈接文件本身,而非指向的源文件
[root@localhost test]# touch 1
[root@localhost test]# ln -s 1 2
[root@localhost test]# cp -d 2 3
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 2月 5 17:16 1
lrwxrwxrwx. 1 root root 1 2月 5 17:16 2 -> 1
lrwxrwxrwx. 1 root root 1 2月 5 17:16 3 -> 1
-a:-dR --preserve=all,archive,用于實現(xiàn)歸檔
[root@localhost test]# cp -a 3 4
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 2月 5 17:16 1
lrwxrwxrwx. 1 root root 1 2月 5 17:16 2 -> 1
lrwxrwxrwx. 1 root root 1 2月 5 17:16 3 -> 1
lrwxrwxrwx. 1 root root 1 2月 5 17:16 4 -> 1
[root@localhost test]#
-preserv=
mode:權(quán)限
[root@localhost test]# ll
總用量 0
-rw-rw-rw-. 1 root root 0 2月 5 17:16 1
[root@localhost test]# cp 1 5
[root@localhost test]# cp --preserve=mode 1 6
[root@localhost test]# ll
總用量 0
-rw-rw-rw-. 1 root root 0 2月 5 17:16 1
-rw-r--r--. 1 root root 0 2月 5 17:25 5
-rw-rw-rw-. 1 root root 0 2月 5 17:26 6
ownership:屬主和屬組
[root@localhost test]# ll
總用量 0
-rw-rw-r--. 1 l_cong l_cong 0 2月 5 17:29 1
[root@localhost test]# cp 1 2
[root@localhost test]# cp --preserve=ownership 1 3
[root@localhost test]# ll
總用量 0
-rw-rw-r--. 1 l_cong l_cong 0 2月 5 17:29 1
-rw-r--r--. 1 root root 0 2月 5 17:30 2
-rw-rw-r--. 1 l_cong l_cong 0 2月 5 17:30 3
timestamp:時間戳
[root@localhost test]# cp --preserve=timestamp 1 3
[root@localhost test]# ll
總用量 0
-rw-rw-r--. 1 l_cong l_cong 0 2月 5 17:29 1
-rw-r--r--. 1 root root 0 2月 5 17:31 2
-rw-r--r--. 1 root root 0 2月 5 17:29 3
context:安全標簽
xattr:擴展屬性
links:符號鏈接
all:上述所有屬性
[12]mv命令:move
mv [OPTION]...[-T] SOURCE DEST
mv [OPTION]... SOURCE...DIRECTORY
mv [OPTION]... DIRECTORY SOURCE...
常用選項:
-i:交互式
-f:force
example:
[root@localhost test]# cat 1
qqqqqq
[root@localhost test]# cat 6
[root@localhost test]# mv -f 1 6
[root@localhost test]# ls
3 6
[root@localhost test]# cat 6
qqqqqq
[root@localhost test]#
[13]rm命令:remove
rm [OPTION]...FILE...
常用選項:
-i:interactive
-f:force
-r:recursive
example:
[root@localhost test]# rm 3
rm:是否刪除普通空文件 "3"?
[root@localhost test]# rm -r 3
rm:是否刪除普通空文件 "3"?
[root@localhost test]# mkdir a
[root@localhost test]# rm a
rm: 無法刪除"a": 是一個目錄
[root@localhost test]# rm -r a
rm:是否刪除目錄 "a"?
[root@localhost test]# rm -f a
rm: 無法刪除"a": 是一個目錄
[root@localhost test]# rm -f 1
[root@localhost test]# rm -rf a
2、bash的工作特性之命令執(zhí)行狀態(tài)返回值和命令行展開所涉及的內(nèi)容及其示例演示。
[1]命令執(zhí)行的狀態(tài)結(jié)果:
bash通過狀態(tài)返回值來輸出此結(jié)果:
成功:0
失?。?span style="font-family:Calibri">1-255
命令執(zhí)行完成之后,其狀態(tài)返回值保存于bash的特殊變量$?中
命令正常執(zhí)行時,有的還回有命令返回值
根據(jù)命令及其功能不同,結(jié)果各不相同
example:
[root@localhost test]# ls
3 6
[root@localhost test]# echo $?
0
[root@localhost test]# lll
bash: lll: 未找到命令...
[root@localhost test]# echo $?
127
[2]引用命令的執(zhí)行結(jié)果:$(COMMAND)或`COMMAND`
example:
[root@localhost test]# touch 1
[root@localhost test]# cat 1
[root@localhost test]# echo $(ls /) >> 1
[root@localhost test]# cat 1
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost test]# echo `ls /root` >> 1
[root@localhost test]# cat 1
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
[3]強引用:''和弱引用:""
[4]快捷鍵
Ctrl+a:跳轉(zhuǎn)至命令行行首
Ctrl+e:跳轉(zhuǎn)至命令行行尾
Ctrl+l:清屏,相當于clear
3、請使用命令行展開功能來完成以下練習:
(1)、創(chuàng)建/tmp目錄下的:a_c, a_d, b_c, b_d
[root@localhost /]# cd /var/tmp/test/
[root@localhost test]# mkdir -v /tmp/{a,b}_{c,d}
mkdir: 已創(chuàng)建目錄 "/tmp/a_c"
mkdir: 已創(chuàng)建目錄 "/tmp/a_d"
mkdir: 已創(chuàng)建目錄 "/tmp/b_c"
mkdir: 已創(chuàng)建目錄 "/tmp/b_d"
(2)、創(chuàng)建/tmp/myLinux目錄下的:
myLinux/
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
├── lock
├── log
└── run
[root@localhost test]# mkdir -pv
/tmp/myLinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/bin"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/boot"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/boot/grub"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/dev"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/etc"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/etc/rc.d"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/etc/rc.d/init.d"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/etc/sysconfig"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/etc/sysconfig/network-scripts"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/lib"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/lib/modules"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/lib64"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/proc"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/sbin"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/sys"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/tmp"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/usr"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/usr/local"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/usr/local/bin"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/usr/local/sbin"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/var"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/var/lock"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/var/log"
mkdir: 已創(chuàng)建目錄 "/tmp/myLinux/var/run"
4、文件的元數(shù)據(jù)信息有哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息
元數(shù)據(jù)信息有:文件名稱,文件大小,文件類型,訪問權(quán)限,時間戳等等。用stat命令查看,touch命令更改時間戳。
Example:
[root@localhost test]# stat a
文件:"a"
大小:0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-02-05 19:11:49.380389549 +0800
最近更改:2017-02-05 19:11:49.380389549 +0800
最近改動:2017-02-05 19:11:49.380389549 +0800
創(chuàng)建時間:-
更改訪問的時間為指定的時間:
[root@localhost test]# stat a
文件:"a"
大?。?span style="font-family:Calibri">0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-02-05 19:20:33.997396041 +0800
最近更改:2017-02-05 19:20:33.997396041 +0800
最近改動:2017-02-05 19:20:33.997396041 +0800
創(chuàng)建時間:-
[root@localhost test]# touch -a -t 1612261759.56 a
[root@localhost test]# stat a
文件:"a"
大小:0 塊:0 IO 塊:4096 普通空文件
設(shè)備:fd03h/64771d Inode:11830 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2016-12-26 17:59:56.000000000 +0800
最近更改:2017-02-05 19:20:33.997396041 +0800
最近改動:2017-02-05 19:20:48.373396219 +0800
創(chuàng)建時間:-
。
5、如何定義一個命令的別名,如何在命令中引用另一個命令的執(zhí)行結(jié)果?
用alias命令定義別名:
alias name=’value’
注意:別名只對當前shell有效,如果想永久生效,需要定義在配置文件中。
引用命令的執(zhí)行結(jié)果:$(COMMAND)或`COMMAND`
6、顯示/var目錄下所有以1開頭,以一個小寫字母結(jié)尾,且中間至少出現(xiàn)一位數(shù)字(可以有其它字符)的文件或目錄。
[root@localhost test]# ls -d /var/1*[0-9]*[[:lower:]]
/var/1a2sa
7、顯示/etc目錄下,以任意一個數(shù)字開頭,且以非數(shù)字結(jié)尾的文件或目錄?
[root@localhost test]# ls -d /etc/[0-9]*[^0-9]
/etc/1aaa
8、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄。
[root@localhost test]# ls -d /etc/[^[:alpha:]][a-z]*
/etc/1aaa
9、在/tmp目錄下創(chuàng)建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。
[root@localhost test]# touch /var/tfile-$(date +%F-%H-%M-%S)
10、復制/etc目錄下所有以p開頭,以非數(shù)字結(jié)尾的文件目錄復制到/tmp/mytest1目錄中。
[root@localhost test]# cp -r /etc/p*[^0-9] /tmp/mytest1/
11、復制/etc目錄下所有以.d結(jié)尾的文件或目錄至/tmp/mytest2目錄中。
[root@localhost test]# cp -r /etc/*.d /tmp/mytest2
12、復制/etc/目錄下所有以1或m或n開頭,以.conf結(jié)尾的文件至/tmp/mytest3中。
[root@localhost test]#cp -r /etc/[1mn]*.conf /tmp/mytest3