1.Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例
按增刪改查的思路來
增:
touch filename:這個命令本身不是用來新建文件的,它是用來修改文件的時間屬性,不過默認情況時當filename不存在時,它會默認建立一個空文件
mkdir:
常用參數
-p:相當于遞歸創建目錄
-v:顯示出創建過程中的信息
cp :
常用參數
-r:遞歸復制目錄,用于復制目錄時使用
-a:歸檔復制,相當于保留原文件的所有屬性信息
幾條結論:
[root@zejin240 test]# tree
.
├── dir1
└── f1
drwxr-xr-x. 2 root root 4096 Nov 6 11:46 dir1
-rw-r–r–. 1 root root 0 Nov 6 11:45 f1
[root@zejin240 test]# cp f1 f2
[root@zejin240 test]# cp f1 dir1
[root@zejin240 test]# tree
.
├── dir1
│ └── f1
├── f1
└── f2
[root@zejin240 test]# cp f1 f2
cp: overwrite `f2'? N
1. cp時,如果源是一個文件,目標是一個未存在的文件名時,就會把源文件復制到目標,如果目標是一個文件夾時,則會復制到這個文件夾里面去,如果目標是已經存在的一個文件時,會提示是否要覆蓋。
[root@zejin240 test]# cp dir1 dir2
cp: omitting directory `dir1'
[root@zejin240 test]# ll
total 4
drwxr-xr-x. 2 root root 4096 Nov 6 15:04 dir1
-rw-r–r–. 1 root root 0 Nov 6 11:45 f1
-rw-r–r–. 1 root root 0 Nov 6 15:04 f2
[root@zejin240 test]# cp -r dir1 dir2
[root@zejin240 test]# tree
.
├── dir1
│ └── f1
├── dir2
│ └── f1
├── f1
└── f2
[root@zejin240 test]# cp -r dir1 dir2
[root@zejin240 test]# tree
.
├── dir1
│ └── f1
├── dir2
│ ├── dir1
│ │ └── f1
│ └── f1
├── f1
└── f2
2.如果源是一個文件夾,得加上-r參數,目標是一個未存在的文件名時,就會把源文件夾復制成目標文件夾,如果目標是一個已存在的文件夾時,就會把源文件夾復制到目標文件夾下。
[root@zejin240 test]# cp -r f1 f2 dir1 dir3
cp: target `dir3' is not a directory
[root@zejin240 test]# mkdir dir3
[root@zejin240 test]# cp -r f1 f2 dir1 dir3
[root@zejin240 test]# tree
.
├── dir1
│ └── f1
├── dir2
│ ├── dir1
│ │ └── f1
│ └── f1
├── dir3
│ ├── dir1
│ │ └── f1
│ ├── f1
│ └── f2
├── f1
└── f2
3. cp命令時,會把最后一個當成目標,其它的文件及文件夾當成源,而且源有多個時,最后一個必須是一個已經存在的文件夾
最普通的文件復制:cp srcfile destfile
最普通的文件夾復制:cp -r srcdir destdir
多個文件或文件夾復制到一個文件夾去 cp -r srcfile srcdir existdesdir
刪
rm 默認不刪除目錄
常用參數
-f:強制刪除,不用提示
-r:遞歸刪除,當刪除目標為文件時,需要此參數
[root@zejin240 test]# ll
total 12
drwxr-xr-x. 2 root root 4096 Nov 6 15:04 dir1
drwxr-xr-x. 3 root root 4096 Nov 6 15:26 dir2
drwxr-xr-x. 3 root root 4096 Nov 6 15:28 dir3
-rw-r–r–. 1 root root 0 Nov 6 11:45 f1
-rw-r–r–. 1 root root 0 Nov 6 15:04 f2
[root@zejin240 test]# rm f2
rm: remove regular empty file `f2'? y
[root@zejin240 test]# rm f1 -f
[root@zejin240 test]# rm dir3
rm: cannot remove `dir3': Is a directory
[root@zejin240 test]# rm dir3 -rf
[root@zejin240 test]# ll
total 8
drwxr-xr-x. 2 root root 4096 Nov 6 15:04 dir1
drwxr-xr-x. 3 root root 4096 Nov 6 15:26 dir2
改
mv 重命名源文件名成目標文件名,也可以將文件移到某個目錄下
常用用法:
mv file1 file2 將文件file1重命名為file2
mv dir1 dir2 將目錄dir1重命名為dir2
mv file1 dir1 dir2 將文件file1及目錄dir1 移動到dir3目錄下
[root@zejin240 test]# ll
total 8
drwxr-xr-x. 2 root root 4096 Nov 6 15:39 dir2
drwxr-xr-x. 2 root root 4096 Nov 6 15:39 dir3
-rw-r–r–. 1 root root 0 Nov 6 15:38 f3
[root@zejin240 test]# tree
.
├── dir2
├── dir3
└── f3
2 directories, 1 file
[root@zejin240 test]# mv f3 f1
[root@zejin240 test]# mv dir3 dir1
[root@zejin240 test]# tree
.
├── dir1
├── dir2
└── f1
2 directories, 1 file
[root@zejin240 test]# mv f1 dir2 dir1
[root@zejin240 test]# tree
.
└── dir1
├── dir2
└── f1
查
ls
2.bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示
每個命令執行后,狀態返回結果保留在$?中,其中0表示執行正常,1-255表示執行異常
[root@zejin240 test]# echo "a"
a
[root@zejin240 test]# echo $?
0
[root@zejin240 test]# echoo
-bash: echoo: command not found
[root@zejin240 test]# echo $?
127
3. 創建/tmp目錄下的a_c,a_d,b_c,b_d文件夾
[root@zejin240 tmp]# mkdir /tmp/{a,b}_{c,d}
[root@zejin240 tmp]# ll /tmp/
total 16
drwxr-xr-x. 2 root root 4096 Nov 6 09:58 a_c
drwxr-xr-x. 2 root root 4096 Nov 6 09:58 a_d
drwxr-xr-x. 2 root root 4096 Nov 6 09:58 b_c
drwxr-xr-x. 2 root root 4096 Nov 6 09:58 b_d
創建如下目錄結構
[root@zejin240 tmp]# tree /tmp/mylinux/
/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
[root@zejin240 tmp]# mkdir -p /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}}
4.文件的元數據信息有哪些,分別表示什么含義,如何查看,如何修改文件的時間戮信息
文件權限rwx,文件的modify time,access time,change time,文件大小,文件所屬的用戶及用戶組,鏈接數
可以用stat命令查看時間戮信息,touch命令修改文件的時間戮信息
touch t_file
-a :修改access time屬性
-m:修改modify time屬性
-d:指定時間
[root@zejin240 tmp]# touch -a tfile -d '2016-01-01'
[root@zejin240 tmp]# stat tfile
File: `tfile'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 676569 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-01-01 00:00:00.000000000 +0800
Modify: 2016-11-06 11:20:40.002008933 +0800
Change: 2016-11-06 11:21:10.405007827 +0800
[root@zejin240 tmp]# touch -m tfile -d '2016-02-01'
[root@zejin240 tmp]# stat tfile
File: `tfile'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 676569 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-01-01 00:00:00.000000000 +0800
Modify: 2016-02-01 00:00:00.000000000 +0800
Change: 2016-11-06 11:21:36.291007800 +0800
如果不加-d選項,則默認為當前時間
-r :參考某個文件的時間屬性
[root@zejin240 tmp]# stat tfile1
File: `tfile1'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 676570 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-11-06 11:26:03.023007347 +0800
Modify: 2016-11-06 11:26:03.023007347 +0800
Change: 2016-11-06 11:26:03.023007347 +0800
[root@zejin240 tmp]# touch tfile1 -r tfile
[root@zejin240 tmp]# stat tfile1
File: `tfile1'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 676570 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-01-01 00:00:00.000000000 +0800
Modify: 2016-02-01 00:00:00.000000000 +0800
Change: 2016-11-06 11:26:34.211008000 +0800
5.如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果
alias youraliasname=`your command`
[root@zejin240 test]# alias mkdir='mkdir -p'
[root@zejin240 test]# mkdir /tmp/a/b/c/d
[root@zejin240 test]# ll /tmp/a
total 4
drwxr-xr-x. 3 root root 4096 Nov 6 19:40 b
[root@zejin240 test]# tree /tmp/a
/tmp/a
└── b
└── c
└── d
6.顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數據(可以有其它字符)的文件或目錄
ll /var/l*[0-9]*[a-z]
7 顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄
ll /etc/[0-9]*[!0-9]
–
8.顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄
ll /etc/[![:alpha:]][[:alpha:]]*
9.在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名如:/tfile-2016-05-27-09-43-22
[root@zejin240 tmp]# touch /tmp/tfile-`date +%F-%H-%M-%S`
[root@zejin240 tmp]# ll /tmp/tfile-2016-11-06-10-24-55
-rw-r–r–. 1 root root 0 Nov 6 10:24 /tmp/tfile-2016-11-06-10-24-55
10、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中
cp -r /etc/p*[![:digit:]] /tmp/mytest1
11.復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中
cp -r /etc/*.d /tmp/mytest2/
12.復制/etc目錄下所有以1或m或n開頭,惟.conf結尾的文件至/tmp/mytest3目錄中
[root@zejin240 tmp]# cp /etc/{1,m,n}*.conf /tmp/mytest3/
原創文章,作者:chenzejin,如若轉載,請注明出處:http://www.www58058.com/58008
內容比較多,排版能加強一下,會更好~~繼續加油~