Linux学习博客2

2017-12-10  本文已影响0人  sky_c146

Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示

目录管理命令:cd, pwd, ls, mkdir, rmdir, tree

  1. cd命令:change directory切换工作目录
  1. pwd命令:print working directory显示当前工作目录

  2. ls命令:显示当前路径下的文件

  1. mkdir命令:创建目录
  1. rmdir命令:删除目录
  1. tree命令:显示某目录的详细路径结构信息(树形结构)
~]# tree /tmp/magedu.com/
/tmp/magedu.com/
├── machine-id
└── mtab

文件管理命令:cat, more, less, head, tail, touch, cp, mv, rm

  1. cat命令:查看文本文件内容,一次性输出所有信息
  1. more命令:查看文本文件内容,提供翻页功能
  1. less命令:查看文本文件内容,比more命令更强大,不仅提供翻页功能,还可以在查看过程中进行匹配搜索等功能
  1. head命令:显示文本文件的开头N行内容,一次性输出
  1. tail命令:显示文本文件从结尾向上数N行内容,一次性输出
  1. touch命令:更改文件的时间戳
  1. cp命令:复制文件
  1. mv命令:移动文件
  1. rm命令:删除文件(目录)

Bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示

  1. Bash的命令执行状态返回值保存在变量"?"中,可以使用 echo $? 输出其值。
  1. Bash的命令行展开

请使用命令行展开功能来完成以下练习

  1. 创建/tmp目录下的:a_c, a_d, b_c, b_d
]# mkdir /tmp/{a,b}
]# touch /tmp/{a,b}/{c,d}
]# tree /tmp/{a,b}
/tmp/a
├── c
└── d
/tmp/b
├── c
└── d
  1. 创建/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

代码如下:

]# mkdir -p /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}}
]# tree /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

文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息

  1. 文件的元数据包括:名称,路径,大小,inode号,文件类型,硬链接数,权限,时间戳等
  1. stat命令可以显示文件的元数据信息
  1. touch命令可以用来修改文件的时间戳信息

如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果

  1. 定义命令别名
]# alias "abc"="ls -l"
]# abc
total 15088
drwxr-xr-x.  2 root root       24 Dec  9 21:51 a
drwxr-xr-x. 28 1001  1001    4096 Dec  7 16:50 apr-1.6.3
-rwxr-xr-x.  1 root root  1072661 Dec  7 16:47 apr-1.6.3.tar.gz
drwxr-xr-x. 21 1001  1001    4096 Dec  7 16:54 apr-util-1.6.1
-rwxr-xr-x.  1 root root   554301 Dec  7 16:37 apr-util-1.6.1.tar.gz

]# vim .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

  1. 引用另一个命令的执行结果
]# ls
anaconda-ks.cfg  scripts  vmware-tools-distrib
]# ls $(ls)
anaconda-ks.cfg

scripts:
file_type.sh  num_sum.sh  sum_txt_linecount.sh  sum_uid.sh  sys_info.sh  user_type.sh

vmware-tools-distrib:
bin  caf  doc  etc  FILES  INSTALL  installer  lib  vgauth  vmware-install.pl
ls -l /etc | less

显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录

可以采用glob匹配文件名的机制来完成

]# ls /var
adm  cache  crash  db  empty  games  gopher  kerberos  l2cn1  l4_t  lib  list12a  local  lock  log  mail  nis  opt  preserve  run  spool  tmp  yp
]# ls /var/l*[[:digit:]]*[a-z]
/var/list12a

/var/l4_t:
lu98i

显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录

]# ls -d /etc/*
/etc/123                      /etc/cron.weekly              /etc/groff          /etc/locale.conf
/etc/1a                       /etc/crypttab                 /etc/group          /etc/localtime
/etc/1A1                      /etc/csh.cshrc                /etc/group-         /etc/localtime.bak
/etc/1.b                      /etc/csh.login                /etc/grub2.cfg      /etc/login.defs
/etc/1b.d
]# ls -d /etc/[0-9]*[^[:digit:]]
/etc/1a  /etc/1.b  /etc/1b.d

显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

]# ls -d /etc/[^[:alpha:]][a-zA-Z]*
/etc/1a  /etc/1A1  /etc/1b.d

在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22

]# touch /tmp/tfile-$(date +"%Y-%m-%d-%H-%M-%S")
]# ls /tmp/tfile*
/tmp/tfile-2017-12-10-09-17-45

复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

]# cp -a /etc/p*[^0-9] /tmp/mytest1/
]# ls /tmp/mytest1/
pam.d  passwd  passwd-  pki  plymouth  pm  popt.d  postfix  ppp  prelink.conf.d  printcap  profile  profile.d  protocols  python

复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中

]# cp -a /etc/*.d /tmp/mytest2/
]# ls /tmp/mytest2/
1b.d               chkconfig.d  dracut.conf.d  init.d        logrotate.d     my.cnf.d  prelink.conf.d  rc1.d  rc4.d  rc.d       statetab.d  tmpfiles.d
bash_completion.d  cron.d       gdbinit.d      krb5.conf.d   modprobe.d      pam.d     profile.d       rc2.d  rc5.d  rsyslog.d  sudoers.d   xinetd.d
binfmt.d           depmod.d     grub.d         ld.so.conf.d  modules-load.d  popt.d    rc0.d           rc3.d  rc6.d  rwtab.d    sysctl.d    yum.repos.d

复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中

]# cp -a /etc/[lmn]*.conf /tmp/mytest3
]# ls /tmp/mytest3
ld.so.conf  libaudit.conf  libuser.conf  locale.conf  logrotate.conf  man_db.conf  mke2fs.conf  nsswitch.conf
上一篇 下一篇

猜你喜欢

热点阅读