M20 – 1- 第二周博客(2):Linux的常用命令与通配符

1、Linux的常用命令

pwd 命令

简介:
Print the name of the current working directory.
格式:
pwd [-LP]
实例1
[root@centos6 local]# pwd     #显示当前的工作目录在/usr/local下
/usr/local

    

cd 命令

简介:
  Change the shell working directory.
格式:
cd [-L|-P] [dir]
实例1:
[root@centos6 network-scripts]# cd /usr/local/  
[root@centos6 local]# pwd
/usr/local
实例2:
[root@centos6 local]# cd ..     #到上一级目录(..:为上一级目录)
[root@centos6 usr]# pwd
/usr
实例3:
[root@centos6 usr]# cd          #回到家目录
[root@centos6 ~]# pwd
/root
实例4:
[root@centos6 ~]# cd -
/usr
[root@centos6 usr]# pwd
/usr

ls 命令

简介:
ls - list directory contents
格式:
ls [OPTION]... [FILE]...
选项:
-a: 列出所有文件,包括以 "." 开头的隐含文件
-l: 列出文件的详细信息,如创建者,创建时间,文件的读写权限列表等等
-R: 递归列出遇到的子目录
-d: 将目录名象其它文件一样列出,而不是列出它们的内容
-1: 文件独立分行显示
-S: 按文件大小而不是字典序排序目录内容,大文件靠前
-u: 使用最近访问时间代替最近修改时间为依据来排序
-U: 不排序目录内容;按它们在磁盘上存储的顺序列出
实例1:
[root@centos6 ~]# ls
anaconda-ks.cfg  edrawmax-cn.exe  edrawmax_V8.3.2.550_setup.exe  install.log  install.log.syslog  Office_2016.rar  test  wget-log
[root@centos6 ~]# ls -a   #显示已.开头的因此文件以及当前目录.与上级目录..
.   anaconda-ks.cfg  .bash_logout   .bashrc  edrawmax-cn.exe                install.log         .lesshst         .ssh     test      wget-log
..  .bash_history    .bash_profile  .cshrc   edrawmax_V8.3.2.550_setup.exe  install.log.syslog  Office_2016.rar  .tcshrc  .viminfo
实例2:
[root@centos6 ~]# ls -l
total 171436
-rw-------. 1 root root      2429 Aug  2 09:46 anaconda-ks.cfg
-rw-r--r--  1 root root   3604480 Aug  2 13:28 edrawmax-cn.exe
-rw-r--r--  1 root root 171294244 Jul 11 10:39 edrawmax_V8.3.2.550_setup.exe
-rw-r--r--. 1 root root     45324 Aug  2 09:46 install.log
-rw-r--r--. 1 root root      9963 Aug  2 09:44 install.log.syslog
-rw-r--r--  1 root root    137601 Aug  2 10:49 Office_2016.rar
drwxr-xr-x  2 root root      4096 Aug  2 18:57 test
-rw-r--r--  1 root root    429430 Aug  2 13:01 wget-log
实例3:
[root@centos6 boot]# pwd
/boot
[root@centos6 boot]# ls -R
.:
config-2.6.32-642.el6.x86_64  initramfs-2.6.32-642.el6.x86_64.img    symvers-2.6.32-642.el6.x86_64.gz
efi                           initrd-2.6.32-642.el6.x86_64kdump.img  System.map-2.6.32-642.el6.x86_64
grub                          lost+found                             vmlinuz-2.6.32-642.el6.x86_64

./efi:
EFI

./efi/EFI:
redhat

./efi/EFI/redhat:
grub.efi

./grub:
device.map     fat_stage1_5  grub.conf         jfs_stage1_5  minix_stage1_5     splash.xpm.gz  stage2         vstafs_stage1_5
e2fs_stage1_5  ffs_stage1_5  iso9660_stage1_5  menu.lst      reiserfs_stage1_5  stage1         ufs2_stage1_5  xfs_stage1_5

./lost+found:
实例4
[root@centos6 ~]# ls -1
anaconda-ks.cfg
install.log
install.log.syslog
Office_2016.rar
test
wget-log

stat 命令

简介:
stat - display file or file system status
格式:
stat [OPTION]... FILE...
实例1:
[root@centos6 ~]# stat anaconda-ks.cfg 
  File: `anaconda-ks.cfg'
  Size: 2429      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 131083      Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-02 18:57:51.284999765 +0800
Modify: 2016-08-02 09:46:34.172999723 +0800
Change: 2016-08-02 09:46:38.611999719 +0800

blob.png

touch 命令

简介:
touch - change file timestamps
格式:
touch [OPTION]... FILE...
选项:
-a: 仅改变文件的访问时间
-m: 仅改变文件的修改时间
-t: 设置文件的访问、修改、改变时间,[[CC]YY]MMDDhhmm[.ss]
-c: 如果文件不存在,则不予创建
实例1:
使用-a选项更改文件的访问时间,以系统时间为准

blob.png

实例2:

blob.png

实例3:

blob.png

cp 命令

简介:
cp - copy files and directories
格式:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
格式(1)   
cp SOURCE DEST,详解如下:

SRC是文件:
   如果目标不存在:新建DEST,并将SRC中内容填充至新建DEST中
   如果目标存在:
       如果DEST是文件:将SRC中的内容覆盖至DEST中,基于安全,建议为cp命令使用-i选项
       如果DEST是目录:在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中
       
格式(2)   
cp SRC... DEST

SRC...:多个文件
  DEST必须存在,且为目录,其它情形均会出错;
  
格式(3)
cp  SRC  DEST

SRC是目录:此时使用选项:-r
     如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;
     如果DEST存在:
             如果DEST是文件:报错
             如果DEST是目录:复制SRC目录中所有文件至DEST中
选项:
-a:复制时,尽可能保持文件的结构和属性.(但不保持目录结构)等同于 -dpR
-d:复制符号链接作为符号链接而不是复制它指向的文件, 并且保护在副本中原文件之间的硬链接
-f:强制
-i:交互式
-r:递归地复制目录
--preserv:保持原始文件的所有者,组,许可,和时间表属性
-p:与--preserv 一致
-v:显示复制过程
实例1
[root@centos6 ~]# cp -r /usr/local /testdir/     #递归复制
cp: overwrite `/testdir/local/share/applications/mimeinfo.cache'? y
[root@centos6 ~]# ls -R /testdir/    #递归查看
/testdir/:
local

/testdir/local:
bin  etc  games  include  lib  lib64  libexec  sbin  share  src

/testdir/local/bin:

/testdir/local/etc:

/testdir/local/games:

/testdir/local/include:

/testdir/local/lib:

/testdir/local/lib64:

/testdir/local/libexec:

/testdir/local/sbin:

/testdir/local/share:
applications  info  man
...
实例2:
[root@centos6 local]# pwd
/testdir/local
[root@centos6 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@centos6 local]# cp -rf /usr/local/etc* ./     #强制递归复制覆盖,不做交互式

mv 命令

简介:
mv - move (rename) files
格式:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
选项:
-f:强制
-i:交互式
实例1
[root@centos6 ~]# mv -f /testdir/local/etc /testdir/local/etc1
[root@centos6 ~]#

rm命令

简介:
rm - remove files or directories
格式:
rm [OPTION]... FILE...
选项:
-i: 交互式
-f: 强制删除
-r: 递归
--no-preserve-root
实例1
[root@centos6 ~]# cd /testdir/local/
[root@centos6 local]# ls
bin  etc1  games  include  lib  lib64  libexec  sbin  share  src
[root@centos6 local]# rm -rf etc1   #强制递归删除目录etc1,不做交互式提醒(建议:少用)
[root@centos6 local]# ls
bin  games  include  lib  lib64  libexec  sbin  share  src
[root@centos6 local]#

tree 命令

简介:
tree - list contents of directories in a tree-like format.
格式:
tree [options] [directory ...]
选项:
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定pattern匹配到的路径
实例1:
[root@centos6 ~]# rpm -aq|grep tree   #查看是否已安装tree命令
tree-1.5.3-3.el6.x86_64
[root@centos6 ~]# ls
anaconda-ks.cfg  file  file2  install.log  install.log.syslog  test  wget-log
[root@centos6 ~]# tree   #不加参数,直接对当前目录使用tree命令
.
├── anaconda-ks.cfg
├── file
├── file2
├── install.log
├── install.log.syslog
├── test
└── wget-log

1 directory, 6 files     #root目录下有一个目录和六个普通文件
[root@centos6 ~]#

mkdir 命令

简介:
mkdir - make directories
格式:
mkdir [OPTION]... DIRECTORY...
选项:
-p: 存在于不报错,且可自动创建所需的各目录
-v: 显示详细信息
-m MODE: 创建目录时直接指定权限
实例1:
[root@centos6 testdir]# ls
local
[root@centos6 testdir]# mkdir -v test
mkdir: created directory `test'
[root@centos6 testdir]# ls
local  test
[root@centos6 testdir]#
实例2:
[root@centos6 testdir]# mkdir -pv /testdir/test/a/b/c/d
mkdir: created directory `/testdir/test/a'
mkdir: created directory `/testdir/test/a/b'
mkdir: created directory `/testdir/test/a/b/c'
mkdir: created directory `/testdir/test/a/b/c/d'
[root@centos6 testdir]# tree -d /testdir/test/
/testdir/test/
└── a
    └── b
        └── c
            └── d

4 directories
[root@centos6 testdir]#
实例3:
[root@centos6 testdir]# mkdir test1    
[root@centos6 testdir]# ls -ld test1 
drwxr-xr-x 2 root root 4096 Aug  3 12:21 test1     #默认创建的目录权限为755
[root@centos6 testdir]# mkdir -m 444 test2
[root@centos6 testdir]# ls -ld test2 
dr--r--r-- 2 root root 4096 Aug  3 12:22 test2     #设置创建test2目录的权限为444

rmdir 命令

简介:
rmdir - remove empty directories
格式:
rmdir [OPTION]... DIRECTORY...
选项:
-p: 递归删除父空目录
-v: 显示详细信息
实例1
[root@centos6 ~]# mkdir -pv /testdir/test/a/b/c/d
mkdir: created directory `/testdir/test'
mkdir: created directory `/testdir/test/a'
mkdir: created directory `/testdir/test/a/b'
mkdir: created directory `/testdir/test/a/b/c'
mkdir: created directory `/testdir/test/a/b/c/d'
[root@centos6 ~]# rmdir -pv /testdir/test/a/b/c/d
rmdir: removing directory, `/testdir/test/a/b/c/d'
rmdir: removing directory, `/testdir/test/a/b/c'
rmdir: removing directory, `/testdir/test/a/b'
rmdir: removing directory, `/testdir/test/a'
rmdir: removing directory, `/testdir/test'
rmdir: removing directory, `/testdir'
rmdir: failed to remove directory `/testdir': Directory not empty   #因为testdir目录并非空目录,因此无法删除
[root@centos6 ~]# ll /testdir/      
total 12
drwxr-xr-x 11 root root 4096 Aug  3 12:11 local
drwxr-xr-x  2 root root 4096 Aug  3 12:21 test1
dr--r--r--  2 root root 4096 Aug  3 12:22 test2
[root@centos6 ~]#

2、Linux中的通配符glob

通配符:*, ?, []

(1) *
任意长度的任意字符
a*b: 
匹配:aab, ab, a123b, 
非匹配:abc
(2) ?
任意单个字符
a?b:
匹配aab
非匹配ab, a12b, abc
(3) []
匹配指定范围内的任意单个字符
[0-9]:数字0-9
[a-z]:不区分字符大小写,也就是a-z和A-Z
(4) [^]
匹配指定范围外的任意单个字符
[^0-9]:非0-9的数字的任意单个字符
专用字符集合:
[:digit:]:任意数字,相当于0-9
[:lower:]:任意小写字母
[:upper:]: 任意大写字母
[:alpha:]: 任意大小写字母
[:alnum:]:任意数字或字母
[:space:]:空格
[:punct:]:标点符号

原创文章,作者:Aleen,如若转载,请注明出处:http://www.178linux.com/27468

(0)
AleenAleen
上一篇 2016-08-03
下一篇 2016-08-03

相关推荐

  • N25第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     其常用的有: touch、stat、rm、cp、mv、install touch 命令    命令格式: touch [OPTION]… File… 命令参数: -a&n…

    Linux干货 2016-12-12
  • Linux Zabbix监控

                           Linux Zabbix监控 zabbix监控系统:    zabbix是基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zab…

    Linux干货 2016-11-19
  • HA cluster应用—CoroSync+Pacemaker

    HA cluster应用——CoroSync v2 (quorum system)+Pacemaker (standalone daemon)+ crmsh/pcsh corosync简述: Corosync是OpenAIS发展到Wilson版本后衍生出来的开放性集群引擎工程。可以说Corosync是OpenAIS工程的一部分。OpenAIS从openais…

    2016-11-27
  • MySQL高级特性-合并表

    1. Merge Tables         如果愿意的话,可以把合并表看成一种较老的、有更多限制的分区表,但是它们也有自己的用处,并且能提供一些分区表不能提供的功能。 合并表实际是容纳真正的表的容器。可以使用特殊的UNION语法来CREATE TABLE。下面是一个合并表的例子: mysql> &n…

    Linux干货 2015-04-13
  • 马哥linux 0801作业

    将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 2.将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中 3.一个linux用户给root发邮件,要求邮件标题为"help",邮件正文如下: Hello,I am 用户名,the system version is here,ple…

    Linux干货 2016-08-04
  • KVM基本功能

    前言:本来想写出关于KVM的精品文章,然而学的时间不够长,理解也不够透彻。所以有关KVM的高级功能只能等下一次在写。本次只涉及到KVM的基础核心功能,平时经常会使用的功能.。 一、环境准备: 1、BIOS启用Virtualication。 2、在linux系统上查看系统是否支持硬件虚拟化。Intel系列CPU支持虚拟化标识为vmx,AMD系列CPU标识为sv…

    Linux干货 2015-10-27

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-03 15:18

    写的很详细,总结的很好,对常见选项分析很到位