[Linux] command file directory management commands (7)

table of Contents

A, touch command

  Time touch command to create a file or set of (touch command -a, -m, -t, -d parameter can be modified separately atime, ctime, mtime), where you can refer to my previous article "text file editing commands", supplementary part of the eight command are touch modify introduce time https://www.cnblogs.com/HeiDi-BoKe/p/11757961.html#stat .

  The touch command is very simple, more complex reading time is set contents of the file modification time (mtime), the time change file permissions or attributes (ctime) and file (atime).

PS: Create a file is equivalent to modify file attributes the current time based on the time. Create a blank file when the file does not exist; when the file exists and will not overwrite the contents of a file, nor will empty the file already exists.

format:

Touch [options] filename 
Touch [ -acfm] [- D <datetime>] [- r <Reference file or directory>] [-t <datetime>] [- help] [- version] [ File ... ]

parameter:

- A or --time = atime recording or reading time --time = --time = use access or change files.
- m or --time = mtime --time = modify or change the file's modification time recorded.
-c, - NO- the Create if the purpose of the file does not exist, does not create a new file.
- F is not used, but in order to retain compatibility with other unix system.
-r, --reference = FILE file recording time consistent with the reference time can be set with reference file, and - as the effect of the file.
-d, --date = STRING set the time and date, can use a variety of different formats. (Atime and also modify the mtime)
 -t archive, the STAMP set time, the same command format date.

PS: -t parameters: [[CC] YY] MMDDhhmmCC is the number of years in the first two, namely "centuries"; YY is the last two digits of the year, that is, several years in a century. If not given value CC, the number of touch will CCYY defined within the 1969--2068. MM is the number of the month, DD is the day, hh is the number of (several) hours, mm is the number of minutes, SS is the number of seconds. Second setting range is 0--61 here, so leap seconds can be processed. Time for these numbers is a time TZ environment variable specified time zone. Due to system constraints, time is earlier than January 1, 1970 is wrong.

Example:

1) file is not created if the file does not exist


[root@VM_0_10_centos shellScript]# touch -c hello


 2) according to the time file1.txt file will file2.txt the time is updated to the same time file1.txt file.



Timestamp 3) settings file


[root@VM_0_10_centos shellScript]# touch -t 201910310928.10 file1.txt
[root@VM_0_10_centos shellScript]# ll file1.txt
-rw-r--r-- 1 root root 54 Oct 31 09:28 file1.txt


 4)将时间修改为指定时间(和上面的案例一样的效果)

  首先我们先查看下文件信息,然后通过echo修改文件,最后用touch -d指定为原来的时间,这样就能造成文件未被改动的假象(一般黑客很喜欢这样操作)


二、mkdir命令

  mkdir命令用于创建空白目录。与-p参数连用,可以递归创建目录,如果父目录不存在则先创建父目录在创建子目录,如果存在,则不创建父目录,直接创建子目录。

PS:创建已存在的目录会报错,加上-p可以解决

格式:

Usage: mkdir [OPTION]... DIRECTORY...

选项:

-m, --mode=MODE   为目录指定访问权限,与chmod类似。 不是a=rwx - umask
-p, --parents     如果目录已经存在,则不会有错误提示。若父目录不存在,将会创建父目录。该选项常用于创建级联目录。
-v, --verbose     为每个目录显示提示信息。
-Z set SELinux security context of each created directory to the default type --context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX

实例:

1)创建权限为777的的目录(目录默认权限为777-umask)


[root@VM_0_10_centos shellScript]# mkdir -m 777 dir1

[root@VM_0_10_centos shellScript]# ll -d dir1/
drwxrwxrwx 2 root root 4096 Oct 31 10:16 dir1/


 PS:为目录指定权限,指定的权限为mode-umask。例如,mode=rx,umask=2,则最终权限为555-2=554,即rw-rw-r--。

2)创建目录时显示输出信息


[root@VM_0_10_centos shellScript]# mkdir -v dir2
mkdir: created directory ‘dir2’


 3)一条命令创建多个目录(一个项目结构)


[root@VM_0_10_centos shellScript]# mkdir -vp scf/{bin/,doc/{info,prouct},lib/,logs/{info,product},service/deploy/{info,produ
ct}}

 PS:多个目录用大括号"{}"括起来,并用逗号分隔。逗号前后不能有空白。

三、cp命令

  cp命令用于复制文件或目录。

格式:

Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...

PS:

  • 源文件:指定源文件列表。默认情况下,cp命令不能复制目录,如果要复制目录,则必须使用-R/r选项
  • 目标文件:指定目标文件。当“源文件”为多个文件时,要求“目标文件”为指定的目录。
  • 所有目标文件目录必须存在,因为cp命令没有创建目录的权限

复制的三种情况:

  1)如果目标文件是目录,会把源文件复制到该目录中;

  2)如果目标文件也是普通文件,则会询问是否要覆盖它;

  3)如果目标文件不存在,则正常执行复制操作;

选项:

-a:此参数的效果和同时指定"-dpR"参数相同;
-d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录;(对硬链接无效)
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理;
-f:强行复制文件或目录,不论目标文件或目录是否已存在;
-i:覆盖既有文件之前先询问用户;
-l:对源文件建立硬连接,而非复制文件;
-s:对源文件建立符号连接,而非复制文件;
-p:保留源文件或目录的属性;
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;
-b:覆盖已存在的文件目标前将目标文件备份;
-v:详细显示命令执行的操作。

PS:“-l” 和 “-s” 选项

如果使用 “-l” 选项,则目标文件会被建立为源文件的硬链接;如果使用了 “-s” 选项,则目标文件会被建立为源文件的软链接。

这两个选项和 “-d” 选项不同,“d” 选项要求源文件必须是软链接,目标文件才会复制为软链接;而 “-l” 和 “-s” 选项的源文件只需是普通文件,目标文件就可以直接复制为硬链接和软链接。

 实例:

1)复制文件的同时并重命名文件(将score文件复制到当前目录并将名称改为copyScore.txt)


[root@VM_0_10_centos shellScript]# ll score.txt
-rw-r--r-- 1 root root 110 Oct 21 14:53 score.txt
[root@VM_0_10_centos shellScript]# cp score.txt copyScore.txt

[root@VM_0_10_centos shellScript]# ll *core.txt
-rw-r--r-- 1 root root 110 Nov 1 11:08 copyScore.txt
-rw-r--r-- 1 root root 110 Oct 21 14:53 score.txt


 2)交互式的将当前目录下以sh结尾的文件复制到/tmp/sh目录下(首先sh目录要存在)


 [root@VM_0_10_centos shellScript]# mkdir -p /tmp/sh


 3)复制时保留文件属性(在进行备份数据时很重要,因为cp复制是以当前时间和当前用户为为属性) 


 [root@VM_0_10_centos shellScript]# cp -p today.txt copy.txt


 

四、mv命令

  mv命令用于剪切文件或文件的重命名。剪切操作不同于复制操作,他会把源文件删除掉,只保留剪切后的文件。如果在同一目录中对一个文件进行剪切操作,其实就是对文件进行重命名。

格式:

Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

选项:

-i: 若指定目录已有同名文件,则先询问是否覆盖旧文件;
-f: 在mv操作要覆盖某已有的目标文件时不给任何指示;
-b :若需覆盖文件,则覆盖前先行备份。 
-u :若目标文件已经存在,且 source 比较新,才会更新(update)
-t  : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

实例:

1)将info目录放入logs目录中。注意,如果logs目录不存在,则该命令将info改名为logs。


 mv info/ logs


 2)将多个文件移动到指定目录下


 

 PS:可以使用正则匹配,也可以将要剪切的文件一个一个写上,空格隔开即可


 3)将文件file1复制到指定目录,如果指定目录下已经存在file1,则询问是否覆盖


 PS:如果不想有提示,加-f参数,强制覆盖(该操作比较危险,使用该参数前先确认是否要强制覆盖)

五、rm命令

  rm命令用于删除文件或目录。默认为删除文件,如果需要删除目录,加上-r参数。每次删除前会询问是否需要删除。想不出现这些确认信息,加-f参数,这里,我不介意大家加上-f参数哈。我自己也吃过一次亏,也是使用-rf参数,直接将服务器生产环境上/usr/目录下的文件全部删除了,导致命令全部使用不了,ssh,scp命令也使用不了,后面是联系的服务器供应商重装系统解决的。真的是有惊无险。

格式:

rm [选项] 文件

选项:

-i 删除前逐一询问确认(交互式)。
-f 即使原档案属性设为唯读,亦直接删除,无需逐一确认。
-r, -R, --recursive 将目录及以下之档案亦逐一删除。
-I 删除文件超过3个或递归删除时,给与交互式一次(仅提示一次确认)
--no-preserve-root 不特殊处理根目录(慎用)

PS:-I(大写I)或 --interactive=once 选项(表示开启交互一次),则 rm 命令会提示用户是否继续整个删除操作,如果用户回应不是确认(即没有回复 y),则整个命令立刻终止。

实例:

1)删除多个文件,中间使用空格隔开即可。

2)删除不存在的文件,可以使用-f参数强制,忽略错误,如果不使用会报错“没有这个目录”

3)删除空目录可以使用-d参数

六、dd命令

  dd命令用于按照指定大小和个数的数据库来复制或转换文件。/dev/zero是一个设备文件,该文件不会占用系统存储空间,但却能提供无穷无尽的数据。可以将它作为dd的输入文件,来生成指定大小的文件。

格式:

Usage: dd [OPERAND]...
  or:  dd OPTION

选项:

if    输入的文件名
of    输出的文件名
bs    同时设置读入/输出的每个“块”的大小
ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。
cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。
count    设置要复制“块”的个数
skip=blocks:从输入文件开头跳过blocks个块后再开始复制。
seek=blocks:从输出文件开头跳过blocks个块后再开始复制。
注意:通常只用当输出文件是磁盘或磁带时才有效,即备份到磁盘或磁带时才有效。
conv=conversion:用指定的参数转换文件。
    ascii:转换ebcdic为ascii
     ebcdic:转换ascii为ebcdic
    ibm:转换ascii为alternate ebcdic
    block:把每一行转换为长度为cbs,不足部分用空格填充
    unblock:使每一行的长度都为cbs,不足部分用空格填充
    lcase:把大写字符转换为小写字符
    ucase:把小写字符转换为大写字符
    swab:交换输入的每对字节
     noerror:出错时不停止
     notrunc:不截短输出文件
    sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

实例:

1)使用dd命令,从社保文件/devzero文件中取出一个大小为560MB的数据块



 2)如果想从光驱设备中的光盘制作成iso镜像文件,可以使用dd命令来压制出光盘镜像文件,将它变成一个可立即使用的iso镜像。



PS:bs相当于勺子大小,count相当于勺子盛饭的次数;勺子越大,勺子盛饭次数就越少。bs和count都是用于指定容量的大小,只要满足需求,可以任意搭配。 

3)将本地/dev/vda1整盘备份到/dev/vda1_bak(在操作的时候考虑下自己服务器或虚拟机的磁盘大小)


 [root@VM_0_10_centos shellScript]# dd if=/dev/vda1 of=/dev/vda1_bak


 PS:这里你可以在运行这条命令之前查看下磁盘容量,运行完之后再查看下磁盘容量的变化

4)从备份文件恢复到指定盘 


dd if=/dev/vda1_bak of=/dev/vda1 


 5)备份today.txt全盘数据,并利用gzip工具进行压缩,保存到指定路径(dd好像不能备份目录)



 6)将备份的文件恢复到指定的文件或数据盘(-d为解压,-c压缩)



7)备份与恢复MBR(磁盘开始的512个字节大小的MBR信息到指定文件)

 备份:dd if=/dev/hda of=/root/image count=1 bs=512

count=1指仅拷贝一个块;bs=512指块大小为512个字节。

恢复:dd if=/root/image of=/dev/had

8)备份软盘


 


9)拷贝内存内容到硬盘


PS:这里我查了一些资料,但是没有解决掉这个问题,说是内核中限制了复制的容量。


 10)拷贝光盘内容到指定文件夹,并保存为cd.iso文件 


dd if=/dev/cdrom(hdc) of=/root/cd.iso


11)增加swap分区文件的大小 


第一步:创建一个大小为256M的文件:
1
dd  if = /dev/zero  of= /swapfile  bs=1024 count=262144
第二步:把这个文件变成swap文件(设置交换分区文件):
1
mkswap  /swapfile
第三步:启用这个swap文件(立即启用交换分区内容):
1
swapon  /swapfile
PS:这里会出现 insecure permissions 0777, 0600 suggested. 意思是建议把swap设置成644或600权限。 
第四步:编辑/etc/fstab文件,使在每次开机时自动加载swap文件:
1
swapfile    swap    swap    default   0 0

 参考网址:https://www.cnblogs.com/misswangxing/p/10911969.html

       https://blog.csdn.net/zhangxiaoyang0/article/details/82501209

12)销毁磁盘数据


dd if=/dev/urandom of=/dev/hda1


 PS:利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据。

13)测试硬盘的读写速度



PS:通过以上两个命令输出的命令执行时间,可以计算出硬盘的读、写速度。

14)确定硬盘的最佳块大小:


 通过比较以上命令输出中所显示的命令执行时间,即可确定系统最佳的块大小。


 15)修复硬盘 


dd if=/dev/sda of=/dev/sda dd if=/dev/hda of=/dev/hda


 PS:当硬盘较长时间(一年以上)放置不使用后,磁盘上会产生magnetic flux point,当磁头读到这些区域时会遇到困难,并可能导致I/O错误。当这种情况影响到硬盘的第一个扇区时,可能导致硬盘报废。上边的命令有可能使这些数据起死回生。并且这个过程是安全、高效的。

16) use netcat remote backup (not get to know)

Install nc: # yum -y install nc

dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234

Execute this command backup on the source host / dev / hda:netcat -l -p 1234 | dd of=/dev/hdc bs=16065b

This command is performed on a destination host and to receive data written to / dev / hdc

netcat -l -p 1234 | bzip2 > partition.img

netcat -l -p 1234 | gzip > partition.img
PS: more than two instructions are instructions to change the destination host respectively bzip2, gzip compress the data, and the backup file in the current directory.
The value of a large video file into the i-th byte 0x41 (i.e. ASCII value of uppercase A)
echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc

Seven, file command

  file command is used to view files of type.

format:

file [argument] file

parameter:

- When b lists the recognition results, do not display the file name.
- C shown in detail execution instruction, to facilitate troubleshooting or analyzing the case of program execution.
-f <file name>   when specifying the name of the file, the contents of one or more file names, file sequentially so that the identification of these files, a file format for each column name.
- L displayed directly Collections symbolic link points file.
-m <file magic number>   specifies the magic number file.
- v displays the version information.
-z try to interpret the contents of compressed files.

Example:

1) Check the file type

 

Guess you like

Origin www.cnblogs.com/HeiDi-BoKe/p/11771991.html