Linux命令——cp、rm、mv、touch、file、dir

cp

copy 拷贝文件html

拷贝过程不指定目标文件名 则目标文件名和源文件名同样node

[root@WebServer ~]# cp /91xueit/teacher.txt 51cto/
View Code

拷贝过程指定目标文件名称linux

[root@WebServer ~]# cp /91xueit/teacher.txt 51cto/teacher1.txt
[root@WebServer ~]# cp 51cto/teacher.txt 51cto/teacher1.txt /tmp
View Code

51cto文件夹中扩展名是txt的文件拷贝到当前目录windows

[root@WebServer ~]# cp 51cto/*.txt .
View Code

51cto文件夹拷贝到/tmp文件夹,-R  或 -r 递归复制目录及其子目录的全部内容bash

[root@WebServer ~]# cp -R 51cto/ /tmp
View Code

默认状况下,拷贝文件时文件权限会变化。只有管理员可以将文件拷贝 权限不变,使用-pide

[root@51cto ~]# cp /home/wangyan/wangyan.txt ./
[root@51cto ~]# ll
total 20
-rw-------. 1 root root 1041 May 25 05:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 9714 May 25 05:25 install.log
-rw-r--r--. 1 root root 3161 May 25 05:25 install.log.syslog
-rw-r--r--. 1 root root    0 May 30 03:34 wangyan.txt
[root@51cto ~]# cp /home/wangyan/wangyan.txt ./ -p
cp: overwrite `./wangyan.txt'? y
[root@51cto ~]# ll
total 20
-rw-------. 1 root    root    1041 May 25 05:25 anaconda-ks.cfg
-rw-r--r--. 1 root    root    9714 May 25 05:25 install.log
-rw-r--r--. 1 root    root    3161 May 25 05:25 install.log.syslog
-rw-rw-r--. 1 wangyan wangyan    0 May 30 03:33 wangyan.txt
View Code

-d 保留文件的软连接属性ui

-a 保留全部属性this

你们知道linux下复制目录能够经过,cp -r dirname destdir,可是这样复制的目录属性会发生变化,想要使得复制以后的目录和原目录彻底同样,可使用cp -a dirname destdir spa

-p(小P)复制时保留mode、ownership、timestamps3d

-i :  覆盖时提示

-n   不覆盖已存在文件

-f:覆盖已经存在的目标文件而不给出提示

-u:只有源文件较目标文件新时复制

命令格式为:cp -u 源文件 目标文件

这个命令很实用,尤为是在更新文件时。以下图所示,只有源文件比目标文件新时,才会将源文件复制给目标文件,不然,及时执行了命令,也不会执行复制。

-s :建立文件的软连接

命令格式为:cp -s 源文件 目标文件

也能够用ln命令实现一样的功能。当一个文件路径太深(以下述的a/b/c/d/e/orginalFile.txt),访问起来十分不方便时,就会建立这个文件的软连接,使之访问起来更方便些。软连接就至关于windows上的快捷方式。

-l:建立文件的硬连接

rm

remove 删除

删除单个文件

[root@WebServer ~]# rm 51cto/teacher.txt
View Code

-r 递归删除51cto目录中的文件和文件夹,51cto目录不删

[root@WebServer ~]# rm -r 51cto/*
View Code

删除51cto目录

[root@WebServer ~]# rm -r 51cto
View Code

以上删除,会有提示,由于alias里面rm时rm -i的别名。-f 强制删除 没有提示

[root@WebServer ~]# rm -f /tmp/123
View Code

-i:删除前逐一询问确认。一般Linux对rm进行了从命名,默认就把-i加上了

[root@localhost ~]# alias rm
alias rm='rm -i'
View Code

 

mv

move 移动文件 或 文件夹

http://www.noobyard.com/article/p-kldmajtc-bg.html

-b:当文件存在时,覆盖前,为其建立一个备份。备份文件以~结尾

[root@localhost Document]# cat >myword <<EOF
> this is my word!
> EOF
[root@localhost Document]# cat >text <<EOF
> this is my text!
> EOF
[root@localhost Document]# mv -b myword text                                            //在一个文件即将覆盖另外一个文件时,默认是提醒的,因此加上-i参数和不加是同样的
mv:是否覆盖"text"? y
[root@localhost Document]# cat myword
cat: myword: 没有那个文件或目录
[root@localhost Document]# cat text
this is my word!
[root@localhost Document]# ll
总用量 8
drwxr-xr-x. 2 root      root      23 5月   5 22:35 mytext                      //这里text里存的是前面myword的内容,text的内容备份到text~中,须要特殊软件才能查看
-rw-r--r--. 1 root      root      17 5月   5 22:41 text
-rw-rw-r--. 1 sunjimeng sunjimeng 17 5月   5 22:41 text~
View Code

-i:交互式操做,覆盖前先行询问用户,若是源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。这样能够避免误将文件覆盖。默认是以alias别名的方式带-i参数

[root@localhost ~]# alias mv
alias mv='mv -i'
View Code

-n  不覆盖已存在文件

-f:强制的意思,若是目标文件已经存在,不会询问而直接覆盖

-u:若目标文件已存在需移动的同名文件,且源文件比较新,才会更新文件

-t :  移动多个源文件到一个目录的状况,此时target在前,source在后。

mv  dir/  anaconda-ks.cfg

-v:verbose

touch

可以改变文件的时间戳,touch文件一下,文件的3个时间都会改变。

最近一次访问时间

最近一次修改时间  改变文件的内容。 内容变化——>文件大小变化——>元数据变化

最近一次改变时间  元数据的改变 eg:文件名 大小 权限

单独更改access时间,-a,change时间也会跟着变

单独更改modify时间,-m,change时间也会跟着变

所以,也就没有参数单独修改change时间。

使用指定时间,而非当前时间更改modify时间。-t 或者 -d

[root@51cto ~]# touch -m -t 199312312359.59 wangyan.txt 
[root@51cto ~]# stat wangyan.txt 
  File: `wangyan.txt'
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 783368      Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/ wangyan)   Gid: (  500/ wangyan)
Access: 2018-05-30 03:34:36.776992594 +0800
Modify: 1993-12-31 23:59:59.000000000 +0800
Change: 2018-05-30 04:07:58.569981915 +0800
View Code

touch file时file不存在默认建立,file存在则修改3个时间。使用-c能够取消这种默认行为,即文件不存在也不建立

file

查看文件类型

http://www.noobyard.com/article/p-scbmaxyj-bc.html

一般file能够告诉咱们目标文件是ELF文件仍是脚本文件(得知是脚本文件是经过脚本内部#!后面的信息判断的)

-b:列出文件辨识结果时,不显示文件名称。

[root@localhost ~]# file /usr/bin/sh
/usr/bin/sh: symbolic link to `bash'
[root@localhost ~]# file /usr/bin/sh -b
symbolic link to `bash'
View Code

-f :列出文件中文件名的文件类型

[root@localhost ~]# cat haha.txt 
/usr/bin/cp
/usr/bin/gzip
[root@localhost ~]# file -f haha.txt 
/usr/bin/cp:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=47c2259e084c64fb00ec01bda8a57c005e3516d5, stripped
/usr/bin/gzip: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=526d77ff7164870f948d8f97aaf0a888cc561b30, stripped
View Code

-L:查看对应软连接指向文件的文件类型

[root@localhost ~]# ll /usr/bin/sh
lrwxrwxrwx. 1 root root 4 Oct  5 17:14 /usr/bin/sh -> bash
[root@localhost ~]# file /usr/bin/sh
/usr/bin/sh: symbolic link to `bash'
[root@localhost ~]# file /usr/bin/sh -L
/usr/bin/sh: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=765c505bb8a234fcd64ede405fa7fcb25734f06a, stripped
View Code

-z:尝试去解读压缩文件的内容

--help:显示命令在线帮助

-version:显示命令版本信息

[root@51cto ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@51cto ~]# file /bin/cat 
/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
View Code

dir

功能和ls同样,知道就行,没啥人用

Linux dir command for beginners (10 examples)

What's the difference between “dir” and “ls”?