liunx作业一

  

一、linux发行版


  linux发行版是以linux为内核,包含了系统软件和应用软件、简化系统安装的工具、软件安装升级的集成管理器。
  典型的linux发行版包括:linux内核,一些GNU程序库和工具,命令行shell,图形界面的X Window系统和相应的桌面环境,办公软件,编译器等。

   


二、linux基础命令(一)

ifconfig (network interfaces configuring)

描述:


  ifconfig 用于配置常驻内核的网络接口。它用于在引导成功时设定网络接口。此后,只在需要调试及系统调整时才使用。
  ifconfig命令在centos7中取消了,如果要使用ifconfig,需要安装net-tools.x86_64包就可以使用了,命令是yum install net-tools.x86_64


用途:


  用于显示或配置网络设备


语法格式:


  ifconfig [-v] [-a] [-s] [interface]
  ifconfig [-v] interface [aftype] options | address ...

命令选项详解:

  -a:显示所有网络接口的信息
  -s:显示摘要信息(类似于netstat -i)
  up:激活网络接口
  down:关闭网络接口
  -arp:允许或禁止在接口上使用ARP协议
  -promisc:允许或禁止接口置于混合模式。如果选用,则接口可以接收网络上的所有分组。
  -allmulti:允许或禁止组播模式(all-multicast)。如果选用,则可以接受网络上的所有组播分组。
  metric #:将接口度量值设置为#。度量值表示在这个路径上发送一个分组的成本,就是通过多少个路由
  mtu #:设置接口的最大传输单元为#
  netmask addr:为接口设置掩码
  del addr/prefixlen:为接口删除一个IPv6地址
  add addr/prefixlen:为接口添加一个IPv6地址
  address addr:为接口设置IPv4地址
  hw class address:设置接口的硬件地址。hw是hardware(硬件)的缩写cless表示硬件类型,包括ether(以太网)、ax25(AMPR AX.25)、ARCnet和netrom(AMPR NET/ROM)

  注:ifconfig没有给出参数,则显示所有有效接口的状态。
  如果跟在接口名称后的第一个参数是它支持地址族的名称,那么这个地址族被用于翻译和显示所有的协议地址。当前支持的地址族包括 inet ( TCP/IP ,缺省), inet6 ( IPv6 ) , ax25 ( AMPR 无线分组 ), ddp ( Appletalk 2 代), ipx ( Novell IPX ) 和 netrom ( AMPR 无线分       组)。


示例:

  ifconfig ens32 192.168.10.91 netmask 255.255.255.0 设置ens32网卡的IP和掩码
  ifconfig ens32:0 hw ether 00:50:56:9b:29:af   设置ens32的MAC地址
  ifconfig ens32 down  禁用ens32网卡

  ifconfig显示所有网络接口的详细信息

  



echo

描述:

  内置bash命令,用于输出文字和变量的值


语法格式:

  echo [SHORT-OPTION]... [STRING]...
  echo LONG-OPTION

命令选项详解:

  -n:do not output the trailing newline(不输出换行)
  -e:enable interpretation of backslash escapes(使用反斜杠转义)
  -E:disable interpretation of backslash escapes (default)(禁用反斜杠转义)
  --help:display this help and exit(显示帮助信息和退出)
  --version:output version information and exit(显示版本信息和退出)
  a:alert (BEL)(提示警报)
  :backspace(退格)
   :new line(换行)
   : horizontal tab(在空格间加上水平制表符)
  v:vertical tab(垂直制表符)
   :carriage return(不输出 之前的字符)

示例:

  # echo -e "This  is Li  Yang's computer"
  # echo -e " This is Li Yang's computer"
  # echo -e " vThis vis vLi vYang's vcomputer"
  # echo -e "This is Li Yang's computer"

  

  # echo $A   打印变量

  

tty

描述:

  print the file name of the terminal connected to standard input(打印连接到标准输入端的文件名)


语法格式:

  tty [OPTION]...

命令选项详解:

  -s, --silent, --quiet:
  print nothing, only return an exit status(什么也不打印,只是返回退出状态码)
  不加任何参数,将显示当前连接的终端对应的文件描述符号

示例:

  # tty    查看一个终端的文件名

  

  # echo "this is liyang's computer" > /dev/pts/1  在另一个终端中输入此条命令,pts/1的终端将显示如下

  


startx

描述:

  startx 命令简化了启动 X 会话的过程。

  该命令执行下列操作:
  设置用户的 DISPLAY 环境变量以标识 X 服务器到 X 客户机
  当从工作站运行时,启动 X 服务器
  启动 X 客户机。

  startx 命令将 X 服务器和 X 客户机的错误消息重定向至用户的 XERRORS 环境变量指定的文件。该处理对调试很有用,并且使 X 服务器在工作站上有一个清洁的启动和关机外观。

  如果 startup 脚本文件名没有用 -x 选项在命令行中给出,那么 startx 命令就会搜索一个由用户的 XINITRC 环境变量指定的文件。如果未设置 XINITRC 环境变量,startx 命令就会在用户的主目录中分别搜索名为 .Xinit、.xinit、 .Xinitrc、.xinitrc 或 .xsession 的文件来开始 X 客户机程序。

  如果未找到启动文件,startx 命令运行在命令行上由 -m 选项指定的窗口管理器,或在找到相关的配置文件(.mwmrc、.twmrc、.awmrc 或 .uwmrc, 分别地)之后,调用窗口管理器 mwm、twm、awm 或 uwm。如果没有在用户主目录中找到窗口管理器配置       文件,startx 将启动 Xterm 客户机和 mwm 窗口管理器。

  当启动文件找不到时,startx 命令也将在命令行上使用 -r 选项指示装入资源文件,或分别装入用户主目录中的名为 .Xdefaults、.xdefaults、.Xresources 或 .xresources 的文件。如果未找到 X 资源文件,将不个性化 X 会话。

  如果工作站中存在一个启动文件并且用户未装入任何资源,则 startx 命令中的 xinit 命令将尝试装入 .Xdefaults 文件。

  当 X 会话从 /dev/lft* 启动时,就假定使用工作站。如果不是这种情况,就必须使用 -t 或 -w 选项。

  在CentOS7上要让 startx 生效至少需要底下这几件事情的配合:
     (1)并没有在其他tty下启动图形界面
     (2)你必须要已经安装了 X Window system,且 X server 是能够顺利启劢的;
     (3)你最好要有窗口管理员,例如 GNOME/KDE 或者是阳春的 TWM 等;

语法格式:

  startx [ -d Display:0 ] [ -t | -w ] [ -x Startup | [ -r Resources ] [ -m Window_Manager ] ] [ -             wait ]

命令选项详解:

  -d Display :0 指定在启动过程中传递给 X 客户机的 X 服务器的显示名称。
  -m Window_Manager 当未找到 startup 脚本时,启动窗口管理器。
  -r Resources 当未找到 startup 脚本时,装入资源文件。
  -t 为一个 X 终端启动 X 客户机。
  -w 为工作站上的 X 窗口会话启动 X 服务器和 X 客户机。
  -wait 当 xdm 命令调用 startx 时,防止重新启动 X 会话。
  -x Startup 使用 startup 脚本启动 X 窗口会话。

  注:您可以单独或同时使用 -m 和 -r 选项, 或者使用 -x 选项,但是您不能在使用 -x 选项的同时使用 -m 和 -r 选项。在启动脚本中,启动窗口管理器会话、装入 X 资源以及产生 X 客户机是用户的责任。

示例:

  要在工作站上或 X 终端上启动 X 会话,请输入:

  startx
  要在工作站上强制启动 X 会话,请输入:

  startx -w
  要为 X 终端启动 X 会话,并注销用户的 Telnet 会话,请输入:

  startx; kill -9 $$
  要使用 .xinitrc 脚本启动 X 会话,请输入:

  startx -x .xinitrc
  要使用 mwm 窗口管理器启动 X 会话,请输入:

  startx -m mwm

export

描述:

  设置或显示环境变量。(比如我们要用一个命令,但这个命令的执行文件不在当前目录,这样我们每次用的时候必须指定执行文件的目录,麻烦,在代码中先执行export,这个相当于告诉程序,执行某某东西时,需要的文件或什么东东在这些目录里)
  在shell中执行程序时,shell会提供一组环境变量。 export可新增,修改或删除环境变量,供后续执行的程序使用。export的效力仅及于该此登陆操作。


语法格式:

  export [-fnp] [name]=[word]] ...   (name表示变量名,word表示变量值)


命令选项详解:

  -f:代表【name】中为函数名称
  -n:删除指定的变量。变量实际上并未删除,只是不会输出到后续指定的执行环境中
  -p:列出所有的shell赋予程序的环境变量

 

示例:

  # export

  

pwd (Print Working Directory的缩写)

描述:

  打印当前工作目录

 

语法格式:

  pwd [OPTION]...

 

命令选项详解:

  -L,--logical:
  use PWD from environment, even if it contains symlinks (用PWD环境,即使它包含符号链接。)
  如果是目录连接链接时,输出连接路径
  -P, --physical:
  avoid all symlinks (显示完整的原始路径)

 

示例:

  #cd /etc/init.d
  #pwd
  #pwd -L
  #pwd -P

  

拓展:

  内建命令pwd和外部命令/bin/pwd。
  内建命令pwd和外部命令/bin/pwd是两个不同的命令,通过以下的示例,看下其中的区别:

  www.jb51.net:/var/www# pwd -P
  /var/www
  www.jb51.net:/var/www# /bin/pwd -P
  /bin/pwd: invalid option -- P
  Try `/bin/pwd --help' for more information.

  可见内建命令pwd和外部命令/bin/pwd压根不是同一个命令,那问题就来了,这两个命令的联系跟区别是什么呢?
  来看这些例子:

  www.jb51.net:/var/www# cd //
  www.jb51.net://# pwd
  //
  www.jb51.net://# /bin/pwd
  /
  www.jb51.net://#

  由此可见,二者的基本功能是一致的,但是在显示当前目录时,/bin/pwd显示得更准确些,而pwd容易被操作者糊弄。但是差别不大。

 

history

描述:

  显示或操作历史命令列表

 

语法格式:

  history [-c] [-d 偏移量] [n]
  history -anrw [文件名]
  history -ps 参数 [参数...]

 

命令选项详解:

  -c:删除所有条目,清空历史列表
  -d 偏移量:从指定位置删除历史列表
  -a:将当前的历史记录追加到命令历史文件中。默认当前的历史命令是缓存在内存中的,退出当前shell后保存到命令历史文件中的,默认保存到~/.bash_history中
  -n:从指定文件中读取所有未被读取的命令
  -r:读取指定的文件内容并将内容追加到历史列表中
  -w:将当前的命令历史写入到指定的文件中,并追加到命令历史列表中
  -p:对每一个ARG参数展开历史并显示结果,而不存储到历史列表中
  -s:以单条记录追加ARG到历史列表中

 

历史命令使用技巧:

(1)!的使用   

  ○  !!:重复前一个命令
  ○  !字符:重复前一个以“字符”开头的命令(中间没有空格,此方法不会记录到命令历史列表中。)

  ○  !字符:#    从命令历史列表中获取前一个以“字符”开头的命令的第#个参数

  ○  !字符:$     从命令历史列表中获取前一个以“字符”开头的命令的最后一个参数 

  ○  !!:$   为当前命令获得上一条命令的参数

  ○  !^:为当前命令获取上一条命令的第一个参数
  ○  !num:按history命令输出的序列号重复对应的命令,(num代表的是命令列表中的序列号)
  ○  !?con:重复前一个包含“con”的命令,“con”可以是命令中包含的,也可以是参数中包含的。
  ○  !-n:重复n个命令之前的那个命令

 (2)按键组合

   ○   使用“pageup"和"pagedown"键、方向键的"↑"和”↓“键浏览之前执行过的命令

   ○   使用ctrl+r搜索执行过的命令,按左右方向键可以对选中的命令进行调整,ctrl+c退出命令搜索

   ○   ctrl+p可以重复上一条执行过的命令

   ○   调用前一条命令的参数使用Esc+.(点击Esc键,再点击.键,不是同时按的)。  

(3)export+变量的使用

   ○   HISTTIMEFORMAT='<%F %T>:'    给每条命令添加时间戳

   ○   HISTCONTROL=erasedups   清除整个历史命令列表中的重复条目

   ○   HISTCONTROL=ignoredups   清除历史命令列表中连续重复的条目

   ○   HISTCONTROL= ignorespace   执行此操作后,在命令前面加一个空格,该命令就不会被记录到历史命令列表中了

   ○   HISTIGNORE=pwd:ls:ll    不记录pwd、ls、ll命令

   ○   HISTCONTROL=          此项等于空,即为取消HISTCONTROL之前的设置。其他变量同理是一样的

(4)~/.bash_profile文件中的相关操作

   HISTSIZE=1000    命令历史列表中条数,此处为0,将不记录命令历史

   HISTFILESIZE=1000   命令历史文件中的条数 

   HISTFILE=~/.bash_history   命令历史文件的存储路径

注意:
History保存在每个用户自己的历史记录中,位于用户的家目录中。
用户登录后,执行命令存放在内存中,只有登录后才能看到。

 

示例

  

  

  

  

  

shutdown、poweroff、halt、reboot

对比:

  

   通过上面的表格可以看出,四条命令的使用基本是一样的,那是因为它们都是systemctl的软链接,如下图

  

hwclock

描述

  显示和设定硬件时钟(Real Time Clock,简称RTC)和系统时钟(System Clock),硬件时钟是指BIOS的时钟(或CMOS时钟),系统时钟是指kernel中的时钟(是从1969年末开始算起的)。当liunx启动时,系统时钟会读取硬件时钟的设定,之后系统时钟独立运作。所有linux相关的指令与函数都是读取系统时钟的设定。linux系统提供硬件时钟时,提供两种时区选择,一种是本地时区,此时的硬件时钟与系统时钟相同;另外一种是UTC时区,此时硬件时间存的是UTC时间,系统初始化时会转换为本地时间后再设置为系统时钟的时间。

语法格式

  hwclock [功能] [选项...]

命令选项详解

  功能:

    -h, --help :显示此帮助并退出
    -r, --show :读取硬件时钟并打印结果
    --set :将 RTC 设置为 --date 指定的时间
    -s, --hctosys :从硬件时钟设置系统时间(hctosys是hardware clock to system clock的简写)
    -w, --systohc :从当前系统时间设置硬件时钟(systohc是system clock to hardware clock的简写)
    --systz :基于当前时区设置系统时间
    --adjust:         根据自上次时钟设置或调整后的系统漂移来调整 RTC
    -c, --compare :定期将系统时钟与 CMOS 时钟相比较
    --getepoch:       打印内核的硬件时钟纪元(epoch)值
    --setepoch:       将内核的硬件时钟纪元(epoch)值设置为--epoch 选项指定的值
    --predict :预测 --date 选项所指定时刻读取到的 RTC 值
    -V, --version :显示版本信息并退出

  选项:
    -u, --utc :硬件时钟保持为 UTC 时间
    --localtime :硬件时钟保持为本地时间
    -f, --rtc <文件> :代替默认文件的特殊 /dev/... 文件
    --directisa :直接访问 ISA 总线,而非 /dev/rtc
    --badyear :忽略 RTC 年份(由于 BIOS 损坏)
    --date <时间> :指定要设置的硬件时钟时间
    --epoch <年> :指定作为硬件纪元(epoch)值起始的年份
    --noadjfile :不访问 /etc/adjtime;需要使用 --utc 或 --localtime 选项
    --adjfile <文件> :指定调整文件的路径;默认为 /etc/adjtime
    --test :不更新,只显示将进行什么操作
    -D, --debug :调试模式

 注:hwclock与clock命令操作基本是一样的。

 示例

  

date

描述

  查看指定格式的系统日期和时间,设定系统日期和时间

语法格式

  date [选项]... [+格式]

    date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]     (MM月;DD日;HH时;mm分;YY两位年份;CCYY四位年份;.ss秒)

 

命令详解

  -d, --date=STRING   :  显示指定的时间

  -f, --file=DATEFILE   :  显示 DATEFILE 中每一行指定的时间, 如同将 DATEFILE 中的每行作为 --date 的参数一样

  -I[TIMESPEC], --iso-8601[=TIMESPEC]   :  按照 ISO-8601 的日期/时间格式输出时间

  -r, --reference=文件  : 显示指定文件的最后修改时间

  -R, --rfc-2822  : 以RFC 2822格式输出日期和时间。例如:2006年8月7日,星期一 12:34:56 -0600

  -s, --set=STRING   : 设置当前系统时间,只有root权限才能设置,其他用户只能查看

  -u, --utc, --universal   :  显示或设置全球时间

拓展

FORMAT变量

  1.在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中可用的标记列表如下: % :  打印出 %

    %n : 下一行
    %t : 跳格
    %H : 小时(00..23)
    %I : 小时(01..12)
    %k : 小时(0..23)
    %l : 小时(1..12)
    %M : 分钟(00..59)
    %p : 显示本地 AM 或 PM
    %r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
    %s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
    %S : 秒(00..61)
    %T : 直接显示时间 (24 小时制)
    %X : 相当于 %H:%M:%S
    %Z : 显示时区 %a : 星期几 (Sun..Sat)
    %A : 星期几 (Sunday..Saturday)
    %b : 月份 (Jan..Dec)
    %B : 月份 (January..December)
    %c : 直接显示日期与时间
    %d : 日 (01..31)
    %D : 直接显示日期 (mm/dd/yy)
    %h : 同 %b
    %j : 一年中的第几天 (001..366)
    %m : 月份 (01..12)
    %U : 一年中的第几周 (00..53) (以 Sunday 为一周的第一天的情形)
    %w : 一周中的第几天 (0..6)
    %W : 一年中的第几周 (00..53) (以 Monday 为一周的第一天的情形)
    %x : 直接显示日期 (mm/dd/yy)
    %y : 年份的最后两位数字 (00.99)
    %Y : 完整年份 (0000..9999)

  2.在设定时间方面
    date -s //设置当前时间,只有root权限才能设置,其他只能查看。
    date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
    date -s 01:01:01 //设置具体时间,不会对日期做更改
    date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
    date -s “01:01:01 20080523″ //这样可以设置全部时间
    date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
    date -s “20080523 01:01:01″ //这样可以设置全部时间
  3.加减
    date +%Y%m%d         //显示现在天年月日
    date +%Y%m%d --date="+1 day"  //显示后一天的日期
    date +%Y%m%d --date="-1 day"  //显示前一天的日期
    date +%Y%m%d --date="-1 month"  //显示上一月的日期
    date +%Y%m%d --date="+1 month"  //显示下一月的日期
    date +%Y%m%d --date="-1 year"  //显示前一年的日期
    date +%Y%m%d --date="+1 year"  //显示下一年的日期

或者更简单点的  date=`date -d -${t}day '+%Y%m%d'` //为t为前几天

三、linux中获取命令帮助信息的方法

(1)help

描述

  简单阐述命令(command)的使用方法,包括命令的功能、选项的解释、命令格式

使用方法

  内部命令:help command    

  外部命令:command --help

(2)info

描述

  有超链接稳文档,info是信息页,提供作者、版本,什么时候发布等更详细信息

使用方法

  info command

(3)man

描述  

  man就是manual的缩写,用来查看系统中自带的各种参考手册,但是手册页分为9个章节,分别是: 

    1 Executable programs or shell commands     查看命令的帮助
    2 System calls (functions provided by the kernel)    查看可被内核调用的函数的帮助
    3 Library calls (functions within program libraries)    查看函数和函数库的帮助
    4 Special files (usually found in /dev)     查看设备文件及特殊文件的帮助(主要是/dev目录下的设备文件)
    5 File formats and conventions eg /etc/passwd      查看配置文件的帮助
    6 Games     查看游戏的帮助
    7 Miscellaneous (including macro packages and conventions), e.g. man(7),groff(7)      查看其它杂项的帮助
    8 System administration commands (usually only for root)     查看系统管理员可用命令的帮助
    9 Kernel routines [Non standard]     查看和内核相关文件的帮助

使用方法

  man [num]  [OPTION] command   (mum指的是章节号)  

    Space, ^V, ^f, ^F: 向文件尾翻屏;
    b, ^B: 向文件首部翻屏;
    d, ^D: 向文件尾部翻半屏;
    u, ^U: 向文件首部翻半屏;
    RETURN, ^N, e, ^E or j or ^J: 向文件尾部翻一行;
    y or ^Y or ^P or k or ^K:向文件首部翻一行;
    q: 退出;

    #:跳转至第#行;
    1G: 回到文件首部
    G:翻至文件尾部

    /KEYWORD:
      以KEYWORD指定的字符串为关键字,从当前位置向文件尾部搜索;不区分字符大小写;
        n: 下一个
        N:上一个
    ?KEYWORD:
      以KEYWORD指定的字符串为关键字,从当前位置向文件首部搜索;不区分字符大小写;
        n: 跟搜索命令同方向,下一个
        N:跟搜索命令反方向,上一个

 (4)其他方法

  阅读软件自带的readme文件;使用google搜索相关文档

四、文件类命令

   查看类命令:ls、cat、tac、more、less、tail、stat、head

   操作类命令:cp、mv、rm、touch

   权限类命令:chmod、chown、chgrp

   文本处理类:grep、cut、join、sed、awk

   目录类命令:cd、pwd、rmdir、mkdir、tree

   其他类:file、install、nano、vi

(此次先介绍操作类命令,其他命令后续添加)

CP

描述

  文件或目录的复制命令

语法格式

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

  cp [OPTION]... SOURCE... DIRECTORY

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

  cp SRC DEST
    SRC是文件:
      如果目标不存在:新建DEST,并将SRC中内容填充至DEST中;
      如果目录存在:
        如果DEST是文件:将SRC中的内容覆盖至DEST中;
          此时建议为cp命令使用-i选项;
        如果DEST是目录:在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中;

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

  cp SRC DEST

    SRC是目录:
      此时使用选项:-r

      如果DEST不存在:则创建指定目录,复制SRC目录中所有文件至DEST中;
      如果DEST存在:
        如果DEST是文件:报错
        如果DEST是目录:复制SRC目录中所有文件至DEST中

命令选项详解

  -a,--archive   :   归档,等同于-dR --preserve=all.(主要用于备份文件时备份文件的属性,这样在还原文件后不妨碍使用)

  --attributes-only   :   仅复制属性,不复制数据

  --backup=CONTROL   :   为每个已存在的目标文件创建备份

       none, off:从不备份(即使给出 --backup)

       numbered, t:用编号备份

       existing, nil :如果编号备份存在则使用编号备份,否者用简易备份[译注:也就是用波浪号]

       simple, never :总是使用简易备份

  -b   :   类似于--backup但不接受参数

  --copy-contents   :   在递归处理时复制特殊文件的内容

  -d   :   等同于--no-dereference --preserve-links

  -f,--force   :   强制复制,目标文件存在将覆盖,不提示错误信息、

  -i   :  交互式,复制时询问是否复制

  -l ,--link  :   创建硬链接文件(可理解为windows的快捷方式,此方式只适用于文件不适用于目录)

  -L,--dereference   :   在源代码中始终遵循符号链接 (复制文件中的符号链接)

  -n,--no-clobber   :   不覆盖已存在的文件,使前面的-i选项失效

  -P,--no-dereference   :   不复制文件中的符号链接

  -p   :   等同于--preserve=模式、所有权、时间戳

  --preserve=属性列表   :   复制指定的属性,默认的是模式(mode)、所有权(ownership)、时间戳(timestamp),还有环境(context)、链接(links)、xattr等,all表示所有属性

  -R,-r,--recursive   :   递归复制目录及其子目录内的所有内容

  -s,--symbolic-link   :只创建符号链接而不复制文件

  -S,--suffix=后缀   :   复制时定义目标文件的后缀

  --remove-destination   :   尝试打开目标文件前先删除已存在的目标文件(相当于--force)

  -u,--update   :   只在源文件比目标文件新,或目标文件不存在时才进行复制

  -v,--verbose   :   显示详细的复制步骤

  -t, --target-directory=DIRECTORY   :   再将多个文件复制到同一个目录时,使用此选项可以将目标文件在前,所有源文件在后

示例

  

CP的基本命令格式
    [root@localhost tmp]# cp /tmp/test2.txt /home/temp/test2.txt
    [root@localhost tmp]# ll /home/temp
    总用量 0
    drwxr-xr-x. 2 root root 54 12月 30 19:18 file
    drwxr-xr-x. 2 root root  6 12月 30 19:17 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    -rw-r--r--. 1 root root  0 12月 30 19:24 test2.txt

同时拷贝多个文件
    [root@localhost tmp]# cp /tmp/test{2,3}.txt /tmp/test4.txt /home/temp
    cp:是否覆盖"/home/temp/test2.txt"? y
    [root@localhost tmp]# ll /home/temp
    总用量 0
    drwxr-xr-x. 2 root root 54 12月 30 19:18 file
    drwxr-xr-x. 2 root root  6 12月 30 19:17 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test4.txt

拷贝目录
    [root@localhost tmp]# cp -r /tmp/test /home/temp
    [root@localhost tmp]# ll /home/temp
    总用量 0
    drwxr-xr-x. 2 root root 54 12月 30 19:18 file
    drwxr-xr-x. 2 root root  6 12月 30 19:17 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root 54 12月 30 19:33 test
    -rw-r--r--. 1 root root  0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test4.txt

创建文件的硬链接,只创建源文件的快捷方式,而不拷贝源文件
    [root@localhost tmp]# cp -l /tmp/test2.txt /home/temp/file1
    [root@localhost tmp]# ll /home/temp/file1
    总用量 4
    -rw-r--r--. 2 root root 402 12月 30 19:41 test2.txt
    [root@localhost tmp]# ls -lvi /home/temp/file1
    总用量 4
    201922516 -rw-r--r--. 2 root root 402 12月 30 19:41 test2.txt
    [root@localhost tmp]# ls -lvi 
    总用量 4
       467779 drwxr-xr-x. 2 root root  54 12月 30 19:14 test
    134722314 drwxr-xr-x. 2 root root   6 12月 30 19:15 test1
    201922516 -rw-r--r--. 2 root root 402 12月 30 19:41 test2.txt
    201922525 -rw-r--r--. 1 root root   0 12月 30 19:15 test3.txt
    201922536 -rw-r--r--. 1 root root   0 12月 30 19:15 test4.txt

创建源文件的符号链接(软连接)
    [root@localhost tmp]# cp -s /tmp/test2.txt /home/temp/file/text2
    [root@localhost tmp]# ll /home/temp/file
    总用量 0
    -rw-r--r--. 1 root root  0 12月 30 19:18 filea.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 fileb.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 filec.txt
    lrwxrwxrwx. 1 root root 14 12月 30 19:48 text2 -> /tmp/test2.txt

只拷贝符号链接文件,即源文件和目的文件都是符号链接文件
    [root@localhost tmp]# cp -P /home/temp/file/text2 /home/temp
    [root@localhost tmp]# ll /home/temp
    总用量 0
    drwxr-xr-x. 2 root root 66 12月 30 19:48 file
    drwxr-xr-x. 2 root root 22 12月 30 19:42 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root 54 12月 30 19:33 test
    -rw-r--r--. 1 root root  0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test4.txt
    lrwxrwxrwx. 1 root root 14 12月 30 19:52 text2 -> /tmp/test2.txt

复制符号链接文件的原始文件
    [root@localhost tmp]# cp -L /home/temp/text2 /tmp/test1
    [root@localhost tmp]# ll /tmp/test1
    总用量 4
    -rw-r--r--. 1 root root 402 12月 30 19:55 text2
    [root@localhost tmp]# ll /home/temp
    总用量 0
    drwxr-xr-x. 2 root root 66 12月 30 19:48 file
    drwxr-xr-x. 2 root root 22 12月 30 19:42 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root 54 12月 30 19:33 test
    -rw-r--r--. 1 root root  0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test4.txt
    lrwxrwxrwx. 1 root root 14 12月 30 19:52 text2 -> /tmp/test2.txt

文件归档,复制文件的所有属性,如下text2还是一个符号链接文件
    [root@localhost tmp]# cp -a /home/temp /tmp/test5
    [root@localhost tmp]# ll
    总用量 8
    drwxr-xr-x. 2 root root   54 12月 30 19:14 test
    drwxr-xr-x. 2 root root   18 12月 30 19:55 test1
    -rw-r--r--. 2 root root  402 12月 30 19:41 test2.txt
    -rw-r--r--. 1 root root    0 12月 30 19:15 test3.txt
    -rw-r--r--. 1 root root    0 12月 30 19:15 test4.txt
    drwxr-xr-x. 5 root root 4096 12月 30 19:52 test5
    [root@localhost tmp]# ll test5
    总用量 0
    drwxr-xr-x. 2 root root 66 12月 30 19:48 file
    drwxr-xr-x. 2 root root 22 12月 30 19:42 file1
    -rw-r--r--. 1 root root  0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:18 file4.txt
    -rw-r--r--. 1 root root  0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root 54 12月 30 19:33 test
    -rw-r--r--. 1 root root  0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root  0 12月 30 19:26 test4.txt
    lrwxrwxrwx. 1 root root 14 12月 30 19:52 text2 -> /tmp/test2.txt

文件更新,如下:复制目录后通过日期可以观察到只有file4.txt文件更新了
    [root@localhost tmp]# vi /home/temp/file4.txt
    [root@localhost tmp]# ll /home/temp/file4.txt
    -rw-r--r--. 1 root root 217 12月 30 20:12 /home/temp/file4.txt
    [root@localhost tmp]# ll /tmp/test5/file4.txt
    -rw-r--r--. 1 root root 77 12月 30 20:08 /tmp/test5/file4.txt
    [root@localhost tmp]#  cp -u /home/temp/*.txt /tmp/test5
    cp:是否覆盖"/tmp/test5/file4.txt"? y
    [root@localhost tmp]# ll /tmp/test5
    总用量 8
    drwxr-xr-x. 2 root root  66 12月 30 19:48 file
    drwxr-xr-x. 2 root root  22 12月 30 19:42 file1
    -rw-r--r--. 1 root root   0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root 217 12月 30 20:15 file4.txt
    -rw-r--r--. 1 root root   0 12月 30 19:23 file5.txt
    -rw-r--r--. 1 root root  77 12月 30 20:09 file6.txt
    drwxr-xr-x. 2 root root  54 12月 30 19:33 test
    -rw-r--r--. 1 root root   0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test4.txt
    lrwxrwxrwx. 1 root root  14 12月 30 19:52 text2 -> /tmp/test2.txt
 
将目标目录下与源文件同名的文件进行备份,文件名后有“~”波浪符号的即为备份文件
    [root@localhost tmp]# ll
    总用量 8
    drwxr-xr-x. 2 root root   54 12月 30 19:14 test
    drwxr-xr-x. 2 root root   18 12月 30 19:55 test1
    -rw-r--r--. 2 root root  402 12月 30 19:41 test2.txt
    -rw-r--r--. 1 root root    0 12月 30 19:15 test3.txt
    -rw-r--r--. 1 root root    0 12月 30 19:15 test4.txt
    drwxr-xr-x. 5 root root 4096 12月 30 20:09 test5
    [root@localhost tmp]# ll /home/temp
    总用量 4
    drwxr-xr-x. 2 root root  66 12月 30 19:48 file
    drwxr-xr-x. 2 root root  22 12月 30 19:42 file1
    -rw-r--r--. 1 root root   0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root 217 12月 30 20:12 file4.txt
    -rw-r--r--. 1 root root   0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root  54 12月 30 19:33 test
    -rw-r--r--. 1 root root   0 12月 30 19:26 test2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test3.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test4.txt
    lrwxrwxrwx. 1 root root  14 12月 30 19:52 text2 -> /tmp/test2.txt
    [root@localhost tmp]# cp --backup=simple -v /tmp/test{2,3,4}.txt /home/temp
    cp:是否覆盖"/home/temp/test2.txt"? y
    cp:是否覆盖"/home/temp/test3.txt"? y
    cp:是否覆盖"/home/temp/test4.txt"? y
    [root@localhost tmp]# ll /home/temp
    总用量 8
    drwxr-xr-x. 2 root root  66 12月 30 19:48 file
    drwxr-xr-x. 2 root root  22 12月 30 19:42 file1
    -rw-r--r--. 1 root root   0 12月 30 19:18 file2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:18 file3.txt
    -rw-r--r--. 1 root root 217 12月 30 20:12 file4.txt
    -rw-r--r--. 1 root root   0 12月 30 19:23 file5.txt
    drwxr-xr-x. 2 root root  54 12月 30 19:33 test
    -rw-r--r--. 1 root root 402 12月 30 20:27 test2.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test2.txt~
    -rw-r--r--. 1 root root   0 12月 30 20:27 test3.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test3.txt~
    -rw-r--r--. 1 root root   0 12月 30 20:27 test4.txt
    -rw-r--r--. 1 root root   0 12月 30 19:26 test4.txt~
    lrwxrwxrwx. 1 root root  14 12月 30 19:52 text2 -> /tmp/test2.txt
cp示例

touch

描述

  用于创建空文件和修改文件的时间

语法格式 

  touch [选项]... 文件名...

命令选项详解

  -a   :   只更改访问时间

  -c,--no-create   :   不创建任何文件

  -d,--date=字符串   :   使用指定的字符串(时间)表示时间,而非当前时间

  -h,--no-dereference   :   会影响符号链接本身,而非符号链接所指向的文件(当系统支持更改符号链接的所有者时,此选项才有用)

  -m   :   只更变指定文件的修改时间

  -r,--reference=文件名   :   使用指定文件的时间属性而非当前时间

  -t STAMP   :   指定时间,格式是[[CC]YY]MMDDhhmm[.ss]

  -time=WORD   :   使用WORD指定的时间:access、atime、use都等于-a选项的效果,而modify、mtime等于-m选项的效果

文件的时间戳 (查看文件的时间戳命令是stat)

  最近访问时间:access time,缩写atime。最后一次读取文件内容的时间

  最近更改时间:modify time,缩写mtime。最后一次更改文件内容(数据)的时间

  最近改动时间:change time,缩写ctime。最后一次更改文件元数据的时间

  创建时间:文件创建的时间

示例

   

使用touch命令,如果文件不存在,就创建这个文件,如果有此文件,将更新文件的时间戳。

[root@localhost ~]# ll /tmp
总用量 16
-rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
-rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
-rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
-rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
[root@localhost ~]# touch /tmp/file.txt
[root@localhost ~]# ll /tmp
总用量 16
-rw-r--r--. 1 root root    0 1月   5 09:50 file.txt
-rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
-rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
-rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
-rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:50:52.074083036 +0800
最近更改:2017-01-05 09:50:52.074083036 +0800
最近改动:2017-01-05 09:50:52.074083036 +0800
创建时间:-
[root@localhost ~]# touch /tmp/file.txt
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:52:10.233086763 +0800
最近更改:2017-01-05 09:52:10.233086763 +0800
最近改动:2017-01-05 09:52:10.233086763 +0800
创建时间:-



更改文件的atime和mtime时间为指定的时间

[root@localhost ~]# touch -t 201701050900 /tmp/file.txt
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:00:00.000000000 +0800
最近更改:2017-01-05 09:00:00.000000000 +0800
最近改动:2017-01-05 09:53:57.937091899 +0800
创建时间:-



文件被编辑后,所有文件的时间戳变更为当时的系统时间(atime为打开文件的时间,mtime和ctime是文件保存的时间)

[root@localhost ~]# vi /tmp/file.txt
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:54:25.556093216 +0800
最近更改:2017-01-05 09:54:39.300093871 +0800
最近改动:2017-01-05 09:54:39.301093871 +0800
创建时间:-



将文件/tmp/file.txt文件的atime和mtime更改为/tmp/menu.txt文件的atime和mtime

[root@localhost ~]# stat /tmp/menu.txt 
  文件:"/tmp/menu.txt"
  大小:968           块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:202047640   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-02 15:52:56.192759304 +0800
最近更改:2017-01-02 15:51:54.153756346 +0800
最近改动:2017-01-02 15:51:54.154756346 +0800
创建时间:-
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:54:25.556093216 +0800
最近更改:2017-01-05 09:54:39.300093871 +0800
最近改动:2017-01-05 09:54:39.301093871 +0800
创建时间:-
[root@localhost ~]# touch -r /tmp/menu.txt /tmp/file.txt 
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-02 15:52:56.192759304 +0800
最近更改:2017-01-02 15:51:54.153756346 +0800
最近改动:2017-01-05 10:00:57.101111886 +0800
创建时间:-



单独更改文件的atime和mtime

[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 10:10:35.377139460 +0800
最近更改:2017-01-05 10:10:35.377139460 +0800
最近改动:2017-01-05 10:10:35.377139460 +0800
创建时间:-
[root@localhost ~]# touch -at 201701050955 /tmp/file.txt
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:55:00.000000000 +0800
最近更改:2017-01-05 10:10:35.377139460 +0800
最近改动:2017-01-05 10:10:47.305140029 +0800
创建时间:-
[root@localhost ~]# touch -mt 201701050900 /tmp/file.txt
[root@localhost ~]# stat /tmp/file.txt 
  文件:"/tmp/file.txt"
  大小:93            块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:201922525   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-01-05 09:55:00.000000000 +0800
最近更改:2017-01-05 09:00:00.000000000 +0800
最近改动:2017-01-05 10:11:05.743140908 +0800
创建时间:-
touch示例

mv(move)

描述

  移动文件或目录,更改文件或目录的名字

命令格式

      mv [选项]... [-T] 源文件 目标文件

   或:mv [选项]... 源文件... 目录
   或:mv [选项]... -t 目录 源文件...

命令选项详解

  --backup[=CONTROL]   :   为每个已存在的目标文件创建备份

       --backup该选项指定如果目标文件存在时的动作,共有四种备份策略:

       1.CONTROL=none或off : 不备份。

       2.CONTROL=numbered或t:数字编号的备份

       3.CONTROL=existing或nil:如果存在以数字编号的备份,则继续编号备份m+1...n:执行mv操作前已存在以数字编号的文件log2.txt.~1~,那么再次执行将产生log2.txt~2~,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。

       4.CONTROL=simple或never:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

  -b   :   类似--backup,但是不接受参数

  -f,--force   :   覆盖前不询问,潜质覆盖

  -i,--interactive   :   覆盖前询问

  -n,--no-clobber   :   不覆盖已存在的文件

  -S,--suffix=后缀   :   复制时定义目标文件的后缀

  -u,--update   :   只在源文件比目标文件新,或目标文件不存在时才进行移动覆盖

  -v,--verbose   :   显示详细的移动步骤

  -t, --target-directory=DIRECTORY   :   在将多个文件移动到同一个目录时,使用此选项可以将目标文件在前,所有源文件在后

示例

重命名文件
    [root@localhost ~]# ll /tmp
    总用量 20
    -rw-r--r--. 1 root root   93 1月   5 09:00 file.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# mv /tmp/file.txt /tmp/mvtest.txt
    [root@localhost ~]# ll /tmp
    总用量 20
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    
            
移动文件
    [root@localhost ~]# ll
    总用量 20108
    -rw-r--r--. 1 root root        0 1月   5 10:03 201701050900
    -rw-r--r--. 1 root root        0 1月   5 10:03 201701050955
    -rw-------. 1 root root     1049 11月 28 16:48 anaconda-ks.cfg
    -rw-r--r--. 1 root root        0 1月   6 11:35 filetest.txt
    -rw-r--r--. 1 root root      275 12月 22 17:18 history-file.txt
    -rw-r--r--. 1 root root     2561 1月   5 01:02 menu2.py
    -rw-r--r--. 1 root root     1728 1月   5 23:17 menu3.py
    -rwxr-xr-x. 1 root root     1049 1月   5 22:54 menufile.py
    -rw-r--r--. 1 root root 20566643 11月 12 19:48 Python-3.5.2.tgz
    [root@localhost ~]# mv /root/filetest.txt /tmp
    [root@localhost ~]# ll /tmp
    总用量 20
    -rw-r--r--. 1 root root    0 1月   6 11:35 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# ll
    总用量 20108
    -rw-r--r--. 1 root root        0 1月   5 10:03 201701050900
    -rw-r--r--. 1 root root        0 1月   5 10:03 201701050955
    -rw-------. 1 root root     1049 11月 28 16:48 anaconda-ks.cfg
    -rw-r--r--. 1 root root      275 12月 22 17:18 history-file.txt
    -rw-r--r--. 1 root root     2561 1月   5 01:02 menu2.py
    -rw-r--r--. 1 root root     1728 1月   5 23:17 menu3.py
    -rwxr-xr-x. 1 root root     1049 1月   5 22:54 menufile.py
    -rw-r--r--. 1 root root 20566643 11月 12 19:48 Python-3.5.2.tgz
            
            
文件覆盖并改名(删除已存在的目标文件,重命名源文件)
    [root@localhost ~]# vi /tmp/filetest2.txt
    [root@localhost ~]# cat /tmp/filetest2.txt
    sdfwefngfgjajfgkjasdfjgi
    [root@localhost ~]# cat /tmp/filetest.txt
    [root@localhost ~]# mv -i /tmp/filetest2.txt /tmp/filetest.txt
    mv:是否覆盖"/tmp/filetest.txt"? y
    [root@localhost ~]# cat /tmp/filetest.txt
    sdfwefngfgjajfgkjasdfjgi
    [root@localhost ~]# ll /tmp
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    
    
移动目录(如果目的目录不存在将目的目录重命名为源目录名,否则将移动)
    [root@localhost ~]# ll /tmp
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# mv testdir1 testdir2
    mv: 无法获取"testdir1" 的文件状态(stat): 没有那个文件或目录
    [root@localhost ~]# mv /tmp/testdir1 /tmp/testdir2
    [root@localhost ~]# ll /tmp
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 3 root root   21 1月   6 12:00 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# ll /tmp/testdir2/
    总用量 0
    drwxr-xr-x. 2 root root 6 1月   6 11:56 testdir1

            
移动多个文件到同一个目录            
    [root@localhost ~]# ll /tmp/testdir2/
    总用量 0
    drwxr-xr-x. 2 root root 6 1月   6 11:56 testdir1
    [root@localhost ~]# ll /tmp
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 3 root root   21 1月   6 12:00 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# mv -t /tmp/testdir2 /tmp/mvtest.txt /tmp/menu.txt 
    [root@localhost ~]# ll /tmp
    总用量 16
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    drwxr-xr-x. 3 root root   53 1月   6 15:33 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# ll /tmp/testdir2/
    总用量 8
    -rw-r--r--. 1 root root 968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root  93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 2 root root   6 1月   6 11:56 testdir1
            
                        
移动当前目录下的所有文件到上一级目录            
    [root@localhost ~]# cd /tmp/testdir2/
    [root@localhost testdir2]# mv * ../
    [root@localhost testdir2]# ll
    总用量 0
    [root@localhost testdir2]# cd ..
    [root@localhost tmp]# ll
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    drwxr-xr-x. 2 root root    6 1月   6 15:36 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
            
                        
移动覆盖前备份与目标文件同名的文件            
    [root@localhost tmp]# ll
    总用量 24
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    -rw-r--r--. 1 root root   93 1月   5 09:00 mvtest.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    drwxr-xr-x. 2 root root    6 1月   6 15:36 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost tmp]# mv mvtest.txt -b filetest.txt 
    mv:是否覆盖"filetest.txt"? y
    [root@localhost tmp]# ll
    总用量 24
    -rw-r--r--. 1 root root   93 1月   5 09:00 filetest.txt
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt~
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    drwxr-xr-x. 2 root root    6 1月   6 15:36 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
mv示例

rm

描述

  删除文件或目录(可以将删除的文件恢复),对于链接文件,只是断开了链接,原文件保持不变。

命令格式

  rm [选项]... 文件...

  rm [选项]... 目录...

命令选项详解

  -f,--force   :   强制删除,忽略不存在的文件,不给出提示

  -i   :   交互模式删除,提示用户是否删除

  -r,-R,--recursive   :   递归删除目录及其下面的子目录和文件

  -d,--dir   :   删除非空目录(只限超级管理员)

  -v,--verbose   :   删除时显示详细信息

示例

交互式强制递归删除目录,并显示删除的详细信息
    [root@localhost ~]# ll /tmp
    总用量 24
    -rw-r--r--. 1 root root   93 1月   5 09:00 filetest.txt
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt~
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    drwxr-xr-x. 2 root root    6 1月   6 15:36 testdir2
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
    [root@localhost ~]# mv /tmp/filetest.txt /tmp/testdir2
    [root@localhost ~]# ll /tmp/testdir2
    总用量 4
    -rw-r--r--. 1 root root 93 1月   5 09:00 filetest.txt
    [root@localhost ~]# rm -dfriv /tmp/testdir2
    rm:是否进入目录"/tmp/testdir2"? y
    rm:是否删除普通文件 "/tmp/testdir2/filetest.txt"?y
    已删除"/tmp/testdir2/filetest.txt"
    rm:是否删除目录 "/tmp/testdir2"?y
    已删除目录:"/tmp/testdir2"
    [root@localhost ~]# ll /tmp
    总用量 20
    -rw-r--r--. 1 root root   25 1月   6 11:48 filetest.txt~
    -rw-r--r--. 1 root root  968 1月   2 15:51 menu.txt
    drwxr-xr-x. 2 root root    6 1月   6 11:56 testdir1
    -rw-------. 1 root root 1143 1月   2 11:39 yum_save_tx.2017-01-02.11-39.GZiDTF.yumtx
    -rw-------. 1 root root  496 1月   2 11:49 yum_save_tx.2017-01-02.11-49.a0Emah.yumtx
    -rw-------. 1 root root  252 1月   2 11:55 yum_save_tx.2017-01-02.11-55.AJ8RxN.yumtx
rm示例

五、

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

命令执行状态返回值

  

 

  

未完待续。。。。。。
























原文地址:https://www.cnblogs.com/liyang0519/p/6198449.html