Linux系统操作离不开命令,基础命令更显得尤为重要,下面几个让我们来一一尝试

ls:列出当前目录的内容或指定目录

  用法:ls [options] [files_or_dirs]

     ls -a包含隐藏文件
     ls -l显示额外的信息
     ls -R目录递归通过
     ls -ld目录和符号链接信息
     ls -1 文件分行显示
     ls –S 按从大到小排序
     ls –u 配合-t选项,显示并按atime从新到旧排序
     ls –U 不排序按目录存放顺序显示

ls -a 列出目录下的所有文件或目录包括隐藏文件

[root@localhost /]# ls -a
.             bin   etc   lib64  opt   run   sys      tmp
..            boot  home  media  proc  sbin  test     usr
.autorelabel  dev   lib   mnt    root  srv   testdir  var

ls -R

[root@localhost /]# ls -R /home
/home:
mageedu  shui  xiaoshui

/home/mageedu:

/home/shui:

/home/xiaoshui:
text

/home/xiaoshui/text:

ls -1

[root@localhost /]# ls -1 /home/
mageedu
shui
xiaoshui

ls -S 按照所占内存从大到小进行排序

[root@localhost /]# ls -Sl /root
total 16
-rw-------  1 root     root     5237 Jul 28 12:23 history.1
-rw-------. 1 root     root     2633 Jul 25 20:42 anaconda-ks.cfg
-rw-------  1 root     root      119 Jul 26 14:28 history_log
drwxr-xr-x  2 root     root        6 Jul 27 15:11 test
drwxrwxr-x  2 xiaoshui xiaoshui    6 Jul 27 15:15 text
-rw-r--r--  1 root     root        0 Jul 26 17:32 1
-rw-r--r--  1 root     root        0 Jul 26 17:32 100
-rw-r--r--  1 root     root        0 Jul 26 17:32 12

ls -tu 按照atime(访问时间进行排序)

[root@localhost /]# ls -ltu /root
total 16
drwxr-xr-x  2 root     root        6 Jul 28 20:03 test
drwxrwxr-x  2 xiaoshui xiaoshui    6 Jul 28 20:03 text
-rw-------  1 root     root     5237 Jul 28 12:23 history.1
-rw-r--r--  1 root     root        0 Jul 27 09:13 1
-rw-r--r--  1 root     root        0 Jul 26 17:32 12
-rw-r--r--  1 root     root        0 Jul 26 17:32 100
-rw-------  1 root     root      119 Jul 26 14:28 history_log
-rw-------. 1 root     root     2633 Jul 25 20:42 anaconda-ks.cfg



cp命令,copy即复制的意思,强大且常用的一条命令,总结一下,可以长来看看

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

SOURCE是文件:
   如果目标不存在:新建DEST,并将SOURCE中内容填充至DEST中

[root@localhost ~]# ls
1  100  12  anaconda-ks.cfg  history.1  history_log  test  text
[root@localhost ~]# cp /etc/issue /root/xiaoshui
[root@localhost ~]# ls
1  100  12  anaconda-ks.cfg  history.1  history_log  test  text  xiaoshui

DEST中
   如果目标存在:
   如果DEST是文件:将SOURCE中的内容覆盖至DEST中:

[root@localhost ~]# cat /root/xiaoshui 
hello,world
[root@localhost ~]# cp /etc/issue /root/xiaoshui 
cp: overwrite ‘/root/xiaoshui’? y
[root@localhost ~]# cat /root/xiaoshui 
\S
Kernel \r on an \m

Mage Education Learning Services
http://www.magedu.com
tty is \l
hostname is \n
current time is \t

    如果DEST是目录:在DEST下新建与原文件同名的文件,并将SOURCE中内容填充至新文件中:

[root@localhost ~]# ls -l /root/test/
total 0
[root@localhost ~]# cp /etc/issue /root/test/
[root@localhost ~]# ls -l /root/test/
total 4
-rw-r--r-- 1 root root 123 Jul 28 20:32 issue

cp SOURCE... DEST
   SOURCE...:多个文件
            DEST必须存在,且为目录,其它情形均会出错;

[root@localhost ~]# ls /root/text/
f1  f2  f3  f4  f5
[root@localhost ~]# cp /root/text/* /root/text1
cp: target ‘/root/text1’ is not a directory
[root@localhost ~]# cp /root/text/* /root/test/issue 
cp: target ‘/root/test/issue’ is not a directory
[root@localhost ~]# mkdir text1
[root@localhost ~]# cp /root/text/* /root/text1
[root@localhost ~]# ls text1/
f1  f2  f3  f4  f5

 如上所示,当SOURCE为多个文件时候,DEST不存在时或者为一个文件时都会报错,创建一个新的text1新的目录,cp成功


cp SOURCE DEST
  SOURCE是目录:此时使用选项:-r
     如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;

[root@localhost ~]# ls
1  100  12  anaconda-ks.cfg  history.1  history_log  test  text  text1  xiaoshui
[root@localhost ~]# ls test
f1  f2  f3  f4  f5  issue
[root@localhost ~]# cp /root/test/ /root/newtest
cp: omitting directory ‘/root/test/’
[root@localhost ~]# cp -r /root/test/ /root/newtest
[root@localhost ~]# ls
1  100  12  anaconda-ks.cfg  history.1  history_log  newtest  test  text  text1  xiaoshui
[root@localhost ~]# ls /root/newtest/
f1  f2  f3  f4  f5  issue

    如果DEST存在:
       如果DEST是文件:报错

[root@localhost ~]# ls
1  100  12  anaconda-ks.cfg  history.1  history_log  newtest  test  text  text1  xiaoshui
[root@localhost ~]# cp -r /root/test/ history.1
cp: cannot overwrite non-directory ‘history.1’ with directory ‘/root/test/’

       如果DEST是目录:复制成功

[root@localhost ~]# cp -r /root/test/  /tmp
[root@localhost ~]# ls /tmp
backup2016-07-28  fstab     ks-script-q9VyUw  nihao      systemd-private-0c68803c30a44dc89ab2a6d7cabf159a-cups.service-Jvhybl  test  yum.log
error.txt         issue.sh  ks-script-SYqw2e  right.txt  systemd-private-ac493ffd025a47b69ca2ea90b081675f-cups.service-uthsZ2  ts

-i:交互式
-r, -R: 递归复制目录及内部的所有内容;
-a: 归档,相当于-dR--preserv=all
-d:--no-dereference --preserv=links 不复制原文件,只复制链接名
--preserv[=ATTR_LIST]
        mode: 权限
        ownership: 属主属组
        timestamp:
        links
        xattr
        context
        all
-p: 等同--preserv=mode,ownership,timestamp
-v: --verbose



在root用户下系统已经定义别名cp=cp -i,可以使用\cp使用命令。在普通用户下cp即cp

[root@localhost test]# alias
alias cp='cp -i'
[root@localhost test]# cp ../1 /root/test/
cp: overwrite ‘/root/test/1’? y
[root@localhost test]#
[xiaoshui@localhost ~]$ cp f1 ./text/f1
[xiaoshui@localhost ~]$ cp f1 ./text/f1
[xiaoshui@localhost ~]$ cp  -i f1 ./text/f1
cp: overwrite ‘./text/f1’? y
[xiaoshui@localhost ~]$

-f  普通用户家目录中出现root的文件时,cp如果不加-f,是无法覆盖目标文件issue的,会报错。如果加上-f ,没有问题,不过这不是覆盖,而是将目标文件删除,在重新创建一个名称相同的文件,如下所示。

[xiaoshui@localhost ~]$ ls -l
total 4
-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:10 f1
-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:11 f2
-rw-r--r-- 1 root     root     123 Jul 29 09:56 issue
drwxrwxr-x 2 xiaoshui xiaoshui  24 Jul 28 21:14 text
drwxrwxr-x 2 xiaoshui xiaoshui  15 Jul 28 21:12 text2
[xiaoshui@localhost ~]$ cp /etc/fstab /home/xiaoshui/issue 
cp: cannot create regular file ‘/home/xiaoshui/issue’: Permission denied
[xiaoshui@localhost ~]$ cp -f /etc/fstab /home/xiaoshui/issue 
[xiaoshui@localhost ~]$ ls
f1  f2  issue  text  text2
[xiaoshui@localhost ~]$ ls -l
total 4
-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:10 f1
-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:11 f2
-rw-r--r-- 1 xiaoshui xiaoshui 595 Jul 29 09:57 issue
drwxrwxr-x 2 xiaoshui xiaoshui  24 Jul 28 21:14 text
drwxrwxr-x 2 xiaoshui xiaoshui  15 Jul 28 21:12 text2

-r, -R: 递归复制目录及内部的所有内容,当复制的内容存在目录时,此时需要加上-r,R选项

[xiaoshui@localhost ~]$ cp /home/xiaoshui/ /tmp/
cp: omitting directory ‘/home/xiaoshui/’
[xiaoshui@localhost ~]$ cp -R /home/xiaoshui/ /tmp
[xiaoshui@localhost ~]$ ls /tmp/
backup2016-07-28  f2     issue.sh          right.txt                                                             test
clear             fstab  ks-script-q9VyUw  systemd-private-0c68803c30a44dc89ab2a6d7cabf159a-cups.service-Jvhybl  ts
error.txt         home   ks-script-SYqw2e  systemd-private-ac493ffd025a47b69ca2ea90b081675f-cups.service-uthsZ2  xiaoshui
f1                issue  nihao             systemd-private-ef1ab9dc007d41718d97c88fdff754b8-cups.service-McXBL9  yum.log

-d:--no-dereference --preserv=links 不复制原文件,只复制链接名,如果不加-d,则复制的内容是软链接指向的原文件,文件名称为软链接的名字,如下所示:

  不加-d前

[root@localhost ~]# ls -l  /etc/redhat-release 
lrwxrwxrwx. 1 root root 14 Jul 25 20:07 /etc/redhat-release -> centos-release
[root@localhost ~]# cp /etc/redhat-release  newtest/
[root@localhost ~]# ls -l newtest/
total 4
-rw-r--r-- 1 root root 38 Jul 29 10:34 redhat-release

 加上-d后

lrwxrwxrwx. 1 root root 14 Jul 25 20:07 /etc/redhat-release -> centos-release
[root@localhost ~]# cp -d /etc/redhat-release new2
[root@localhost ~]# ls new2
redhat-release
[root@localhost ~]# ls -l new2
total 0
lrwxrwxrwx 1 root root 14 Jul 29 10:38 redhat-release -> centos-release
[root@localhost ~]#

--preserv[=ATTR_LIST]
        mode: 权限
        ownership: 属主属组
        timestamp:
        links
        xattr
        context
        all

 目前只学到ownership和timestamp,所以后面的几个参数先不管,--preserv=ownership是指保持属主属组的,timestamp是保持其时间戳的,若想都保留,中间使用“,”隔开

  如果不加--perserv,发现属组属主和时间戳都改变了

[root@localhost ~]# ls /home/xiaoshui/f1 -l
-rw-rw-r-- 1 xiaoshui xiaoshui 0 Jul 29 10:53 /home/xiaoshui/f1
[root@localhost ~]# cp /home/xiaoshui/f1 new2/
[root@localhost new2]# ls -l
total 0
-rw-r--r-- 1 root root  0 Jul 29 10:56 f1

加上--preserv=ownership,timestamp,发现属主属组和时间戳都保留下来了

[root@localhost new2]# ls -l
total 0
-rw-rw-r-- 1 xiaoshui xiaoshui  0 Jul 29 10:53 f1

-p: 等同--preserv=mode,ownership,timestamp

-v 详细信息


多总结多练习,才能达到熟练掌握并应用的程度。