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)
上一篇 2016-08-03 15:05
下一篇 2016-08-03 15:29

相关推荐

  • 第三周课堂练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@myserver ~]# whoami  root  [root@myserver ~]# who | cut -d' ' -…

    Linux干货 2016-09-19
  • N25_第二周作业_leon

    第二周博客作业_leon 1.       Linux上的文件管理类命令有哪些?其常用的使用方法及其相关示例演示 常用文件管理类命令有: mkdir、touch、rmdir、cp、rm、mv   mkdir—-创建目录 -m  直接设置权限(非默认权限) -v&nbs…

    Linux干货 2016-12-23
  • N25-第三周作业

      alias NAME='COMMAND'           定义命令的别名—对当前用户有效  un为解除别名 Which   查看命令别名 Whereis    查看程序的文档和源码路径(-b &nbs…

    Linux干货 2016-12-18
  • Linux基础之软件包管理

    一.概述 在redhat系列的发行版中,采用rpm软件包管理器,rpm原名是Red Hat Package Manager,后来当其他发行版也采用这种软件包管理机制以后,重新命名,改为RPM Package Manager,它所能提供的功能是将编译好的应用程序文件打包成一个或几个程序文件,从而使得用户能够方便的安装,升级,卸载软件,而yum则是rpm包管理器…

    Linux干货 2016-11-16
  • Linux目录结构

    Linux目录结构 查看目录(/) 树状目录结构: 常见目录的解释 Linux 下的文件类型 路径 Linux目录结构 查看目录(/) 登录系统后,在当前命令窗口下输入命令: [root@localhost ~]# ls /bin   dev  home  lib64       media &nb…

    Linux干货 2016-09-06
  • bash

    bash 配置文件   按生效范围划分:存在两类;     全局配置:            /etc/profile       &nbs…

    Linux干货 2015-04-15

评论列表(1条)

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

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