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
失败:1-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.178linux.com/80174
评论列表(1条)
基础知识虽然简单,但需要牢记。看的出有很认真的在学,加油。