1、Linux上的文件管理命令都有哪些,其常用的使用方法及其相關示例演示
文件管理命令:mkdir,rmdir,cp,mv,rm,cat,tac,head,tail,more,less
mkdir命令:
mkdir – make directories
mkdir [OPTION]… DIRECTORY…
[root@localhost ~]# mkdir -pv /tmp/test/A/B mkdir: created directory ‘/tmp/test’ mkdir: created directory ‘/tmp/test/A’ mkdir: created directory ‘/tmp/test/A/B’
rmdir命令:
rmdir – remove empty directories
rmdir [OPTION]… DIRECTORY…
[root@localhost ~]# mkdir -pv /tmp/test/A/B mkdir: created directory ‘/tmp/test’ mkdir: created directory ‘/tmp/test/A’ mkdir: created directory ‘/tmp/test/A/B’ [root@localhost ~]# rmdir -pv /tmp/test/A/B rmdir: removing directory, ‘/tmp/test/A/B’ rmdir: removing directory, ‘/tmp/test/A’ rmdir: removing directory, ‘/tmp/test’ rmdir: removing directory, ‘/tmp’ rmdir: failed to remove directory ‘/tmp’: Directory not empty
cp命令:
cp – copy files and directories
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
[root@localhost ~]# cp -rf /tmp/a /tmp/b [root@localhost ~]# tree /tmp/b /tmp/b └── a
mv命令:
mv – move (rename) files
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
[root@localhost ~]# mv -f /tmp/a /tmp/b
rm命令:
rm – remove files or directories
rm [OPTION]… FILE…
[root@localhost ~]# rm -rf /tmp/b
cat命令:
cat – concatenate files and print on the standard output
cat [OPTION]… [FILE]…
[root@localhost ~]# cat -n /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 11 games:x:12:100:games:/usr/games:/sbin/nologin 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13 nobody:x:99:99:Nobody:/:/sbin/nologin
tac命令:倒序顯示
[root@localhost ~]# tac /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh mageia:x:1100:1100::/home/linux:/bin/bash test2:x:4003:4003::/home/test2:/bin/bash fedors:x:4002:4002:Fedora Core:/home/fedors:/bin/tcsh test:x:4001:1000:shijl,hs,110,119:/var/tmp/gentoo:/bin/bash gentoo:x:1000:1000::/home/gentoo:/bin/bash sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
head命令:
head – output the first part of files
head [OPTION]… [FILE]…
[root@localhost ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
tail命令:
tail – output the last part of files
tail [OPTION]… [FILE]…
[root@localhost ~]# tail -2 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh
[root@localhost ~]# tail -f /etc/passwd tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin gentoo:x:1000:1000::/home/gentoo:/bin/bash test:x:4001:1000:shijl,hs,110,119:/var/tmp/gentoo:/bin/bash fedors:x:4002:4002:Fedora Core:/home/fedors:/bin/tcsh test2:x:4003:4003::/home/test2:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh
more命令:分屏查看,特點:顯示完成后退出
less命令:與man命令基本相同,此處不做說明
2、bash的工作特性之命令執行返回狀態值和命令行展開所涉及內容及其示例演示
bash的基礎特性之命令的執行狀態結果
命令執行的狀態結果:
bash通過狀態返回值來輸出此結果;
成功:0
失?。?-255
命令執行完成后,其狀態返回值保存于bash的特殊變量$?中;
命令正常執行時,有的還返回命令返回值;
根據命令及其功能不同,結果各不相同;
[root@localhost sysconfig]# echo $? 0 [root@localhost sysconfig]# cd /etc/sss -bash: cd: /etc/sss: No such file or directory [root@localhost sysconfig]# echo $? 1
bash的基礎特性之:命令行展開
~:自動展開為用戶的家目錄,或指定用戶的家目錄;
{}:可承載一個以逗號分隔的路徑列表,并能夠將其展開為多個路徑;
例如:/tmp/{a,b}相當與/tmp/a ,/tmp/b
[root@localhost sysconfig]# mkdir -pv /tmp/{f,g} mkdir: created directory ‘/tmp/f’ mkdir: created directory ‘/tmp/g’
3、請使用命令行展開功能來完成以下練習
(1)創建/tmp目錄下的:a_c,a_d,b_c,b_d
[root@localhost sysconfig]# mkdir -v /tmp/{a,b}_{c,d} mkdir: created directory ‘/tmp/a_c’ mkdir: created directory ‘/tmp/a_d’ mkdir: created directory ‘/tmp/b_c’ mkdir: created directory ‘/tmp/b_d’
(2)創建/tmp/mylinux目錄下的:
[root@localhost ~]# mkdir -pv /tmp/mylinux/{bin,boot/grbu,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 ~]# tree /tmp/mylinux/ /tmp/mylinux/ ├── bin ├── boot │ └── grbu ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── proc ├── sbin ├── sys ├── tmp ├── usr │ ├── local │ │ ├── bin │ │ └── sbin │ ├── localbin │ └── localsbin └── var ├── lock ├── log └── run 26 directories, 0 files
4、文件元數據信息由哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息。
-
文件的數據分兩種:一種元數據,既屬性數據;一種就是數據本身;可使用stat命令查看文件的元數據:
-
例如:
-
[root@localhost ~]# stat /etc/passwd File: ‘/etc/passwd’ Size: 1143 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 135045131 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:passwd_file_t:s0 Access: 2017-07-12 14:25:02.663609106 -0300 Modify: 2017-07-12 14:25:02.662609106 -0300 Change: 2017-07-12 14:25:02.662609106 -0300 Birth: -
其中含義:
-
file:文件名;
-
size:文件大小
-
block:文件占了多少個數據塊
-
IO Block:文件所占數據塊的塊大小
-
Device:硬件,既說明該文件在硬盤的那個柱面
-
Inode:節點號
-
links:鏈接
-
Access(第一個):權限
-
Uid:該文件所屬的屬主
-
Gid:該文件所屬的屬組
-
context:安全上下文
-
Access(第二個):文件上一次的訪問時間
-
Modify:文件上一次修改的時間
-
Change:文件上一次屬性更改的時間
修改文件的時間戳信息:
-
可以使用touch命令更改文件的時間戳:
語法:
-
touch [OPTION]… FILE…
常用選項:
-
-c: 指定的文件路徑不存在時不予創建;
-
-a: 僅修改access time;
-
-m:僅修改modify time;
-
-t:使用指定的日期時間,而非現在的時間;[[CC]YY]MMDDhhmm[.ss]
示例演示:
touch命令:
[root@localhost ~]# stat hello File: ‘hello’ Size: 6 Blocks: 0 IO Block: 4096 directoryDevice: fd00h/64768d Inode: 202498400 Links: 2Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:admin_home_t:s0Access: 2017-07-12 13:22:37.507678020 -0300Modify: 2017-07-12 13:20:43.929680110 -0300Change: 2017-07-12 13:20:43.929680110 -0300 Birth: -[root@localhost ~]# touch /tmp/hello [root@localhost ~]# stat /tmp/hello File: ‘/tmp/hello’ Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 793926 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:user_tmp_t:s0Access: 2017-07-16 22:20:11.103730815 -0300Modify: 2017-07-16 22:20:11.103730815 -0300Change: 2017-07-16 22:20:11.103730815 -0300 Birth: -
[root@localhost ~]# stat hello File: ‘hello’ Size: 6 Blocks: 0 IO Block: 4096 directoryDevice: fd00h/64768d Inode: 202498400 Links: 2Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:admin_home_t:s0Access: 2017-07-12 13:22:37.507678020 -0300Modify: 2017-07-12 13:20:43.929680110 -0300Change: 2017-07-12 13:20:43.929680110 -0300 Birth: -[root@localhost ~]# touch /tmp/hello [root@localhost ~]# stat /tmp/hello File: ‘/tmp/hello’ Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 793926 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:user_tmp_t:s0Access: 2017-07-16 22:20:11.103730815 -0300Modify: 2017-07-16 22:20:11.103730815 -0300Change: 2017-07-16 22:20:11.103730815 -0300 Birth: -
[root@localhost ~]# touch -t 200101010101.33 /tmp/hello [root@localhost ~]# stat /tmp/hello File: ‘/tmp/hello’ Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 793926 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:user_tmp_t:s0Access: 2001-01-01 01:01:33.000000000 -0400Modify: 2001-01-01 01:01:33.000000000 -0400Change: 2017-07-16 22:22:59.367733545 -0300 Birth: -
原創文章,作者:N27_shijinlong,如若轉載,請注明出處:http://www.www58058.com/80174
基礎知識雖然簡單,但需要牢記??吹某鲇泻苷J真的在學,加油。