Linux文件管理命令和bash基础特性

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目录下的:

 Linux文件管理命令和bash基础特性

[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,如若转载,请注明出处:https://www.178linux.com/80174

赞 (0)
N27_shijinlongN27_shijinlong
上一篇 2017-07-13 15:18
下一篇 2017-07-13 16:38

相关推荐

  • Ansible+Corosync+Pacemaker+nfs实现http高可用

    目录: (一)实验环境 (二)准备工作 (三)为node1和node2配置基础配置 (四)使用ansible部署nfs (五)使用ansible部署corosync和pacemaker (六)使用ansible安装crmsh工具 (七)使用crmsh配置http高可用 (八)验证 (九)需要注意的地方 (一)实验环境 1.1、环境拓扑 1.2、所需系统 4台…

    Linux干货 2016-04-29
  • 如何实现在命令行输入pwd时显示出ifconfig的效果

    1、使用type ifconfig 查看   2、使用type pwd 查看 如果还没有使用过pwd则显示如下,表示pwd属于内部命令,然后输入enable -n pwd 禁用这个内部命令   如果已经使用过,就会显示hash,已经缓存过   此时就不仅需要禁用内部命令,还需要使用hash -d pwd 清除pwd的缓存,使其在…

    Linux干货 2017-07-13
  • 系统启动及内核管理

    一、知识整理 1、nohup 命令:不挂断地运行命令,远程连接当断网或断开shell时还可以继续运行。无论是否将nohup命令的输出重定向到终端,输出都将附加到当前目录下的nohup.out文件中,如果当前不可写,则重定向到家目录nohup.out文件,除非定义了输出文件。 nohup COMMAND 输出重定向至文件 nohup COMMAND &…

    Linux干货 2016-09-19
  • redis主从复制(4)— client buffer

    1、 client buffer的设计 redis server以单进程的方式处理接收到的请求,而redis完成请求有些工作比较慢,比如网络IO和磁盘IO等比较慢的操作。redis为了提高处理客户端请求的响应时间,做了很多优化。比如网络io和磁盘io是异步完成、使用后台进程完成bgsave和bgrewriteaof工作,在server端为客户提供读buffe…

    Linux干货 2016-03-28
  • linux下手工创建新用户

    当我们要创建一个用户时,通常使用useradd命令就可以自动创建了一个用户;实际上,在linux下,一切皆文件,设置好几个文件,也是可以创建出一个用户: 文件如下: /etc/passwd  :用户 /etc/shadow  :密码 /home/username:用户家目录 /etc/group:用户组 /etc/skel/*: &nbs…

    Linux干货 2015-03-30
  • N26 – 第三周作业

    1、列出当前系统上所有已登录的用户的用户名,同一用户登录多次只显示一次     ~]# who -u |cut -d ' ' -f1|uniq -u 2、最后登录到当前系统的用户的相关信息     ~]#id $(last | h…

    Linux干货 2017-01-03

评论列表(1条)

  • 马哥教育
    马哥教育 2017-07-22 21:42

    基础知识虽然简单,但需要牢记。看的出有很认真的在学,加油。