文件目录操作命令

touch

NAME
       touch - change file timestamps  更改文件的时间戳

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update the access and modification times of each FILE to the current time.
       将每个文件的访问和修改时间更新为当前时间。

不存在的文件会被创建

      A FILE argument that does not exist is created empty, unless -c or -h is supplied.
       不存在的文件参数被创建为空,除非提供-c或-h。

在这里插入图片描述
stat 命令可以产看文件的访问时间 修改时间 改变时间
注:

  • access 表示最后一次访问(仅仅是访问,没有改动)文件的时间

  • modify 表示最后一次修改文件的时间

  • change 表示最后一次对文件属性改变的时间,包括权限,大小,属性等等

       A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.
       文件参数字符串的-被特别处理,并导致touch更改与标准输出相关联的文件的时间。
    

不存在的文件不会创建 -c

   -c, --no-create
          do not create any files

改变文件的访问时间 -a

   -a     change only the access time

在这里插入图片描述
两种方式 一种是直接访问文件,另一种是通过 -a命令

修改文件的修改时间 -m

   -m     change only the modification time 指修改Modify(修改)时间,而不修改access(访问)时间

在这里插入图片描述

用指定文件的时间替换当前时间 -r

   -r, --reference=FILE
          use this file's times instead of current time

在这里插入图片描述

将时间stamp修改为参数指定的日期 -t

   -t STAMP
          use [[CC]YY]MMDDhhmm[.ss] instead of current time 
          将时间修改为参数指定的日期,如:07081556代表7月8号15点56分

   Note that the -d and -t options accept different time-date formats.

在这里插入图片描述

将时间string修改为参数指定的日期(常用) -d

   -d, --date=STRING
          parse STRING and use it instead of current time
          
DATE STRING
       The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday".  A date string may contain items indicating calendar date, time of
       day, time zone, day of week, relative time, relative date, and numbers.  An empty string indicates the beginning of the day.  The date string format is more complex than is easily documented here but is fully described  in  the
       info documentation.
       date=STRING是一种基本免费的人类可读日期字符串格式,比如“Sun, Feb 29 2004 16:21:42 -0800”或“2004-02-29 16:21:42”,甚至“next Thursday”。日期字符串可以包含指示日历日期、时间的项
       日、时区、星期、相对时间、相对日期和数字。空字符串表示一天的开始。

在这里插入图片描述

stat

NAME
       stat - display file or file system status
       显示文件或文件系统状态

SYNOPSIS
       stat [OPTION]... FILE...

DESCRIPTION
       Display file or file system status.

在这里插入图片描述
简单的介绍一下stat命令显示出来的文件其他信息:

  • File:显示文件名
  • Size:显示文件大小
  • Blocks:文件使用的数据块总数
  • IO Block:IO块大小
  • regular file:文件类型(常规文件)
  • Device:设备编号
  • Inode:Inode号
  • Links:链接数
  • Access:文件的权限
  • Gid、Uid:文件所有权的Gid和Uid。

Linux下的三个时间:

  1. Access Time:简写为atime,表示文件的访问时间。当文件内容被访问时,更新这个时间
  2. Modify Time:简写为mtime,表示文件内容的修改时间,当文件的数据内容被修改时,更新这个时间。
  3. Change Time:简写为ctime,表示文件的状态时间,当文件的状态被修改时,更新这个时间,例如文件的链接数,大小,权限,Blocks数。

上面最开始的三个时间是一模一样的,原因是我在创建它的同时修改了它的内容,所以它的大小,Blocks也发生变化,也相当于一次访问,所以,我退出保存后三个时间一致。
下面进行一一验证。

1.atime(访问时间)的更新
在这里插入图片描述
2.mtime(修改时间)的更新
在这里插入图片描述

这里我使用echo指令向hello.cpp文件中写入了一行内容,惊讶的发现,不止mtime被更新了,而且ctime也被更新了。这是问什么呢?原因很简单,前面说过,ctime在文件大小,Blocks发生变化时也会被更新,所以仔细观察上图,这两项信息是否发生变化了呢?
3.ctime(状态时间)的更新
在这里插入图片描述
使用chmod指令修改hello.cpp的文件权限后,状态时间再次更新。
那如果不采取这些操作导致系统自己更新这三个时间,有没有办法人为改变这三个时间呢?答案是有 就是上面的touch 命令

  -L, --dereference
              follow links 
              支持符号连接

   -f, --file-system
          display file system status instead of file status
          显示文件系统状态而非文件状态

   -t, --terse
          print the information in terse form
          以简洁方式输出信息

mkdir

   mkdir - make directories 创建目录

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...
       目录:指定要创建的目录列表,多个目录之间用空格隔开。

DESCRIPTION
       Create the DIRECTORY(ies), if they do not already exist.
       创建目录(ies),如果它们还不存在的话。

   Mandatory arguments to long options are mandatory for short options too.

创建目录

在这里插入图片描述

建立目录的同时设置目录的权限

   -m, --mode=MODE 
          set file mode (as in chmod), not a=rwx - umask

在这里插入图片描述

建立多级目录

   -p, --parents
          no error if existing, make parent directories as needed
          若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录

在这里插入图片描述

rm

NAME
       rm - remove files or directories
       删除文件或目录

SYNOPSIS
       rm [OPTION]... [FILE]...

默认不删除目录

       This manual page documents the GNU version of rm.  rm removes each specified file.  By default, it does not remove directories.
       rm删除每个指定的文件。默认情况下,它不会删除目录。

   If  the  -I  or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation.  If the response is not
   affirmative, the entire command is aborted.
   如果给出了-I或 --interactive=once 选项,并且有三个以上的文件,或者 给出了-r、-R或--recursive,那么rm会提示用户是否继续执行整个操作。如果响应不是肯定的,则整个命令将中止。

   Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user  for  whether  to  remove  the  file.   If  the
   response is not affirmative, the file is skipped.
   否则,如果一个文件是不可写的,标准输入就是一个终端,并且没有提供-f或——force选项,或者提供-i或--interactive=always选项,rm会提示用户是否删除该文件。如果响应不是肯定的,则跳过该文件。

OPTIONS
       Remove (unlink) the FILE(s). 
       删除(取消链接)文件。

删除时不提示

   -f, --force
          ignore nonexistent files and arguments, never prompt
          忽略不存在的文件和参数,永远不要提示

删除时提示

   -i     prompt before every removal
          每次删除前提示

   -I     prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
          删除三个以上文件前或递归删除时提示一次;比-i更少的侵入性,同时仍然提供了对大多数错误的保护

   --interactive[=WHEN]
          prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
          根据时间提示:never、once (-I)或always (-I); 没有时间,总是提示

   --one-file-system
          when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument

   --no-preserve-root
          do not treat '/' specially
          不特别对待'/'

   --preserve-root
          do not remove '/' (default)
          不要删除'/'(默认值)

递归删除目录及其内容

   -r, -R, --recursive
          remove directories and their contents recursively
          递归地删除目录及其内容

删除空目录

   -d, --dir
          remove empty directories
          删除空目录

   By default, rm does not remove directories.  Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
   默认情况下,rm不删除目录。还可以使用--recursive (-r或-r)选项删除列出的每个目录及其所有内容。

   To remove a file whose name starts with a '-', for example '-foo', use one of these commands:
   要删除名称以“-”开头的文件,例如“-foo”,请使用以下命令之一:
          rm -- -foo
          rm ./-foo

   Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time.  For greater assurance that the contents are truly unrecoverable, consider using shred.
   请注意,如果您使用rm删除文件,那么只要有足够的专业知识和/或时间,就有可能恢复其中的一些内容。为了更好地确保内容确实不可恢复,可以考虑使用shred。

shred

NAME
       shred - overwrite a file to hide its contents, and optionally delete it
       覆盖文件以隐藏其内容,并选择性地删除它

SYNOPSIS
       shred [OPTION]... FILE...

DESCRIPTION
       Overwrite the specified FILE(s) repeatedly, in order to make it harder for even very expensive hardware probing to recover the data.
       重复覆盖指定的文件,以便使即使非常昂贵的硬件探测也难以恢复数据。
       

   If FILE is -, shred standard output.
   如果文件是-,撕碎标准输出。

   Mandatory arguments to long options are mandatory for short options too.

粉碎(覆盖)文件而不删除

$shred test.txt
粉碎一个名为test.txt的文件命令是:
$shred test.txt
粉碎多个文件的命令是:
$shred test1.txt test2.txt
这两条命令的效果是文件依旧存在,但不再是原来的文件了,对比测试可以通过先编辑一个文本文件后保存,然后使用shred命令覆盖此文件,再打开该文本文件即可看出内容不再是原有内容了。如果想要覆盖的同时删除文件(这才是粉碎的表现么),加上参数 -u,命令变为:
$shred –u test.txt

在这里插入图片描述

覆盖一个挂载分区的文件命令是:
$shred /dev/hda0 //覆盖IDE接口的第一个磁盘设备的第一分区
$shred /dev/sda2 //覆盖SCSI或SATA接口的第一个磁盘设备的第三分区
shred /dev/hda7  将删除第一个HDD的第七个分区上的数据:

覆盖后并删除文件

   -u     truncate and remove file after overwriting
          覆盖后截断并删除文件

在这里插入图片描述
shred会用一些随机内容覆盖文件所在的节点和数据块,并删除文件(-u参数)。
如果想清除的更彻底一点可以加-z 参数,意思是先用随机数据填充,最后再用0填充。

   -z, --zero
          add a final overwrite with zeros to hide shredding
          添加带有零的最终覆盖,以隐藏分解

在这里插入图片描述

   -f, --force
          change permissions to allow writing if necessary
          如果需要,更改权限以允许写入

指定覆盖内容的次数

   -n, --iterations=N
          overwrite N times instead of the default (3)
          覆盖N次而不是默认值(3)

在这里插入图片描述
–random-source=FILE
get random bytes from FILE
从文件中获取随机字节

   -s, --size=N
          shred this many bytes (suffixes like K, M, G accepted)
          分解这么多字节(接受K、M、G等后缀)



   --remove[=HOW]
          like -u but give control on HOW to delete;  See below
          比如-u,但要控制如何删除;见下文

   -v, --verbose
          show progress
          显示进度

   -x, --exact
          do not round file sizes up to the next full block;
          不要将文件大小四舍五入到下一个完整块;

          this is the default for non-regular files
          这是非常规文件的默认设置

注意

   Delete  FILE(s)  if  --remove  (-u) is specified.  The default is not to remove the files because it is common to operate on device files like /dev/hda, and those files usually should not be removed.  The optional HOW parameter
   indicates how to remove a directory entry: 'unlink' => use a standard unlink call.  'wipe' => also first obfuscate bytes in the name.  'wipesync' => also sync each obfuscated byte to disk.  The default mode is  'wipesync',  but
   note it can be expensive.
   如果指定了remove (-u),则删除文件。默认情况下不删除这些文件,因为对/dev/hda这样的设备文件进行操作是很常见的,而且通常不应该删除这些文件。可选HOW参数指示如何删除目录项:'unlink' =>使用标准的unlink调用。'wipe' =>也首先混淆了名称中的字节。'wipesync' =>还将每个混淆的字节同步到磁盘。默认模式是'wipesync',但注意它可能很昂贵。

   Delete  FILE(s)  if  --remove  (-u) is specified.  The default is not to remove the files because it is common to operate on device files like /dev/hda, and those files usually should not be removed.  The optional HOW parameter
   indicates how to remove a directory entry: 'unlink' => use a standard unlink call.  'wipe' => also first obfuscate bytes in the name.  'wipesync' => also sync each obfuscated byte to disk.  The default mode is  'wipesync',  but
   note it can be expensive.

   CAUTION:  Note that shred relies on a very important assumption: that the file system overwrites data in place.  This is the traditional way to do things, but many modern file system designs do not satisfy this assumption.  The
   following are examples of file systems on which shred is not effective, or is not guaranteed to be effective in all file system modes:

   * log-structured or journaled file systems, such as those supplied with AIX and Solaris (and JFS, ReiserFS, XFS, Ext3, etc.)

   * file systems that write redundant data and carry on even if some writes fail, such as RAID-based file systems

   * file systems that make snapshots, such as Network Appliance's NFS server

   * file systems that cache in temporary locations, such as NFS version 3 clients

   * compressed file systems

   In the case of ext3 file systems, the above disclaimer applies (and shred is thus of limited effectiveness) only in data=journal mode, which journals file data in addition to just metadata.  In both the  data=ordered  (default)
   and  data=writeback modes, shred works as usual.  Ext3 journaling modes can be changed by adding the data=something option to the mount options for a particular file system in the /etc/fstab file, as documented in the mount man
   page (man mount).

   In addition, file system backups and remote mirrors may contain copies of the file that cannot be removed, and that will allow a shredded file to be recovered later.

cd

cd命令用来切换工作目录至dirname。 其中dirName表示法可为绝对路径或相对路径。
若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录)。
另外,~也表示为home directory的意思,
. 则是表示目前所在的目录,
则表示目前目录位置的上一层目录。

语法
cd (选项) (参数)

选项

-p 如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录
默认情况下,cd命令会切换到软链接的目录,而不是真正的物理目录。

-L 如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。

实例

cd    进入用户主目录;
cd ~  进入用户主目录;
cd -  返回进入此目录之前所在的目录;
cd ..  返回上级目录(若当前目录为“/“,则执行完后还在“/";".."为上级目录的意思);
cd ../..  返回上两级目录;
cd !$  把上个命令的参数作为cd参数使用。

pwd

NAME
       pwd - print name of current/working directory
       打印当前/工作目录的名称

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

   -L, --logical
          use PWD from environment, even if it contains symlinks
          使用环境中的PWD,即使它包含符号链接

   -P, --physical
          avoid all symlinks
          避免所有符号链接

   If no option is specified, -P is assumed.
   如果没有指定选项,则假设-P。

cp

NAME
       cp - copy files and directories

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

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
       cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。
       它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。
       cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。

   Mandatory arguments to long options are mandatory for short options too.

-a -dpR 拷贝文件或目录并保持属性不变(符号链接也一样)常用

   -a, --archive
          same as -dR --preserve=all  
          此参数的效果和同时指定"-dpR"参数相同;

   -d     same as --no-dereference --preserve=links
              当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录;
              
   -p     same as --preserve=mode,ownership,timestamps
              保留源文件或目录的属性;
              
   --preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
          保存指定的属性(默认值:模式、所有权、时间戳),如果可能的话,保存其他属性:上下文、链接、xattr等等

   --no-preserve=ATTR_LIST
          don't preserve the specified attributes
          不要保存指定的属性
          
   --parents
          use full source file name under DIRECTORY
          使用目录下的完整源文件名

  -R, -r, --recursive
	              copy directories recursively
	              拷贝目录递归地

   --attributes-only
          don't copy the file data, just the attributes

复制时备份

   -b     like --backup but does not accept an argument
   删除、覆盖目的文件先备份,备份的文件或目录亦建立为符号链接,并指向源文件或目录链接的源文件或目录。假如没有加上这个参数,在复制过程中若遇到符号链接,则会直接复制源文件或目录

   --copy-contents
          copy contents of special files when recursive

    -S, --suffix=SUFFIX
          override the usual backup suffix

强行复制文件或目录,不论目标文件或目录是否已存在

       -f, --force
              if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
              如果无法打开现有目标文件,请删除该文件并重试(当还使用-n选项时,此选项将被忽略)
              强行复制文件或目录,不论目标文件或目录是否已存在;

覆盖前提示

   -i, --interactive
          prompt before overwrite (overrides a previous -n option)
          覆盖之前的提示符(覆盖前面的-n选项)

不要覆盖现有文件

  -n, --no-clobber
          do not overwrite an existing file (overrides a previous -i option)
          不要覆盖现有文件(覆盖前面的-i选项)

对源文件建立硬链接,而非复制文件

   -l, --link
          hard link files instead of copying

对源文件建立符号链接,而非复制文件

   -s, --symbolic-link
          make symbolic links instead of copying
          

   -L, --dereference
          always follow symbolic links in SOURCE
          始终遵循源文件中的符号链接

   -P, --no-dereference
          never follow symbolic links in SOURCE
          永远不要跟随源文件中的符号链接

   -t, --target-directory=DIRECTORY
          copy all SOURCE arguments into DIRECTORY
          将所有源参数复制到目录中

   -T, --no-target-directory
          treat DEST as a normal file
          将DEST视为普通文件

更新文件

使用这项参数之后,只会在源文件的修改时间(Modification Time)较目的文件更新时,或是名称相互对应的目的文件并不存在,才复制文件

   -u, --update
          copy only when the SOURCE file is newer than the destination file or when the destination file is missing
          仅当源文件比目标文件更新或目标文件丢失时复制

   -v, --verbose
          explain what is being done

   -x, --one-file-system
          stay on this file system
          继续使用这个文件系统
		 复制的文件或目录存放的文件系统,必须与cp指令执行时所处的文件系统相同,否则不复制,亦不处理位于其他分区的文件

mv

 mv - move (rename) files 移动(重命名)文件

mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。source表示源文件或目录,target表示目标文件或目录。如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆盖。

mv命令可以用来将源文件移至一个目标文件中,或将一组文件移至一个目标目录中。源文件被移至目标文件有两种不同的结果:

  • 如果目标文件不是目录文件,则源文件名(只能有一个)会变为此目标文件名,并覆盖己存在的同名文件。
    如果源文件和目标文件在同一个目录下,mv的作用就是改文件名。

  • 当目标文件是目录文件时,源文件或目录参数可以有多个,则所有的源文件都会被移至目标文件中。所有移到该目录下的文件都将保留以前的文件名。
    注意事项:mv与cp的结果不同,mv好像文件“搬家”,文件个数并未增加。而cp对文件进行复制,文件个数增加了。

      SYNOPSIS
             mv [OPTION]... [-T] SOURCE DEST
             mv [OPTION]... SOURCE... DIRECTORY
             mv [OPTION]... -t DIRECTORY SOURCE...
      
      DESCRIPTION
             Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
             将源代码重命名为DEST,或将源代码移动到目录。
    

文件移动和重命名

mv test.log test1.txt
重命名
mv test.log data/
将test1.log 文件移到目录data中

目录移动和重命名

mv dir1 dir2 
如果目录dir2不存在,将目录dir1改名为dir2;否则,将dir1移动到dir2中。

移动当前文件夹下的所有文件到上一级目录

mv * ../

把当前目录的一个子目录里的文件移动到另一个子目录里

mv test3/*.txt test5

多文件移动到目录中

-t, --target-directory=DIRECTORY
          move all SOURCE arguments into DIRECTORY
          将所有源参数移动到目录中
          即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后
          说明:
			mv log1.txt log2.txt log3.txt test3 命令将log1.txt ,log2.txt, log3.txt 三个文件移到 test3目录中去,
			mv -t  /opt/soft/test/test4/ log1.txt log2.txt log3.txt 命令又将三个文件移动到test4目录中去

   Mandatory arguments to long options are mandatory for short options too.

文件被覆盖前做简单备份,前面加参数-b

   --backup[=CONTROL]
          make a backup of each existing destination file
          备份每个现有的目标文件
          若需覆盖文件,则覆盖前先行备份;

   -b     like --backup but does not accept an argument
           当文件存在时,覆盖前,为其创建一个备份;

说明:
-b 不接受参数,mv会去读取环境变量VERSION_CONTROL来作为备份策略。
–backup该选项指定如果目标文件存在时的动作,共有四种备份策略:

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

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

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

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

    -S, --suffix=SUFFIX
           override the usual backup suffix
    
    
    -f, --force
           do not prompt before overwriting
           重写前不提示
           若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录
    
    -i, --interactive
           prompt before overwrite
           重写前提示
    
    -n, --no-clobber
           do not overwrite an existing file
           不覆盖现有文件
    
    If you specify more than one of -i, -f, -n, only the final one takes effect.
    如果指定-i, -f, -n中的一个以上,只有最后一个有效。
    
    --strip-trailing-slashes
           remove any trailing slashes from each SOURCE argument
           从每个源参数中删除任何尾随斜杠
    
    
    
    
    -T, --no-target-directory
           treat DEST as a normal file
           将DEST视为普通文件
    
    -u, --update
           move only when the SOURCE file is newer than the destination file or when the destination file is missing
           只在源文件比目标文件更新或目标文件丢失时移动
    
    -v, --verbose
           explain what is being done
    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值