Linux 修改文件/文件夹时间 & 找到n天前修改的文件/文件夹

引言

今天的任务让我做数据备份,并且删除过期的备份文件,但是刚开始,怎么知道那些是过期的文件呢?在“生产环境”下,怎么测试呢?难道要改时间?这样会影响其他业务的,只能自己构造一个。

测试通过了,此时通过虚拟机进行演示。

知己知彼

mkdir

Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

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  简单来说就是根据你输入的路径进行创建目录,并且如果中间目录不存在的话,也会创建上。
  -v, --verbose     print a message for each created directory
  -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
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

touch

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ touch --help
Usage: touch [OPTION]... FILE...
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.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time  修改访问时间
  -c, --no-create        do not create any files  不创建任何文件
  -d, --date=STRING      parse STRING and use it instead of current time 匹配字符串并且使用它
  -f                     (ignored)  忽略错误
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time  改变修改时间
  -r, --reference=FILE   use this file's times instead of current time 使用这个文件的时间,而不是当前时间
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

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

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/touch>
or available locally via: info '(coreutils) touch invocation'

find

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ find --help 
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]      -context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Valid arguments for -D:
exec, opt, rates, search, stat, time, tree, all, help
Use '-D help' for a description of the options, or see find(1)

Please see also the documentation at http://www.gnu.org/software/findutils/.
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at
https://savannah.gnu.org/bugs/?group=findutils or, if
you have no web access, by sending email to <bug-findutils@gnu.org>.

初顾茅庐

通过 mkdir zzzzzz 进行创建目录,stat zzzzzz进行时间查看

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ stat zzzzzz                                                                                                                                                                           1 ⨯
  File: zzzzzz
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d      Inode: 425231      Links: 2
Access: (0755/drwxr-xr-x)  Uid: ( 1000/   zunmx)   Gid: ( 1000/   zunmx)
Access: 2021-08-26 08:35:38.628278414 -0400
Modify: 2021-08-26 08:35:38.628278414 -0400
Change: 2021-08-26 08:35:38.628278414 -0400
 Birth: 2021-08-26 08:35:38.628278414 -0400

通过touch zzzzzz -a -d "2020-08-08 00:00:00"修改访问时间

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ touch zzzzzz  -a -d "2020-08-08 00:00:00"

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ stat zzzzzz
  File: zzzzzz
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d      Inode: 425231      Links: 2
Access: (0755/drwxr-xr-x)  Uid: ( 1000/   zunmx)   Gid: ( 1000/   zunmx)
Access: 2020-08-08 00:00:00.000000000 -0400
Modify: 2021-08-26 08:35:38.628278414 -0400
Change: 2021-08-26 08:45:28.280305406 -0400
 Birth: 2021-08-26 08:35:38.628278414 -0400

通过touch zzzzzz -m -d "2020-08-09 00:00:00" 改变修改时间

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ touch zzzzzz  -m -d "2020-08-09 00:00:00"

┌──(zunmx㉿zunmx-kali)-[/tmp]
└─$ stat zzzzzz                              
  File: zzzzzz
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d      Inode: 425231      Links: 2
Access: (0755/drwxr-xr-x)  Uid: ( 1000/   zunmx)   Gid: ( 1000/   zunmx)
Access: 2020-08-08 00:00:00.000000000 -0400
Modify: 2020-08-09 00:00:00.000000000 -0400
Change: 2021-08-26 08:46:34.644308444 -0400
 Birth: 2021-08-26 08:35:38.628278414 -0400

获取N天前修改的文件/文件夹

输出7天前修改的文件

find ./ -mtime +7 -type f

输出7天前修改的文件夹

find ./ -mtime +7 -type d

如果想删除,加上-delete参数就好了。

其中周期判断

+7   > 7×24 小时前
-7   < 7×24 小时内
7    正好 7~8 天之间

详细的还是去查find的用法吧。

评论 0

还没有评论,来说点什么吧。

提交后需要审核才会公开。