shell 打印所有目录名称匹配模式的修改日期

shell 打印所有目录名称匹配模式的修改日期

我想编写一个 shell 程序,它将遍历所有名称符合以下模式的文件夹:

sudo sh  shell.sh   pub 

运行时,脚本将查找名称包含pub并打印其修改日期的所有文件夹。我希望有代码可以打印如下图所示的结果: 在此处输入图片描述 我有这段代码但它没有给我我期望的结果。

echo  'the folder '$1' was modified at ';
 find  -type d -name 'kam*'  -exec stat -c '%y %n' '{}' \;

我必须浏览每个文件夹并输出文本echo 'the file '$1' was modified at ';,然后像图像中一样打印日期。

我想要这样的结果:

netcom@hotspot:~$ bash script.sh testRegex Pub
the folder testRegex was modified on may 15 01:19
 the folder Public was modified on may 19 01:19 
  the folder Pubos was modified on may 19 01:19 

答案1

您可以使用find它本身来打印整个内容:

for pattern
do
    find . -type d -name "*$pattern*" -printf 'The folder %P was modified on %t\n'
done

for pattern; do ... done循环遍历所有参数,并pattern依次将变量设置为每个参数。

使用find%P%t在 中给出文件的路径和修改时间-printf

答案2

您可以像以下脚本一样启用bash该选项:globstar

#!/bin/bash
shopt -s globstar
for i
  do for k in **/"$i"*/
    do stat -c "the folder %n was modified on %y" "$k"
  done
done

将其保存为script,使其可执行chmod +x script,然后根据需要调用它:

bash /path/to/script testRegex Pub

请注意,这将搜索例如Pub*,如果您确实想匹配*Pub*,请更改**/"$i"*/**/*"$i"*/。如果您想缩短时间戳,请尝试添加| sed 's/:[^:]*$//'到行尾do stat。您还应该尝试%N而不是%n,尤其是当涉及带有空格的目录名称时,这种格式是可取的。

如果您想要对日期格式进行更细粒度的控制,可以使用以下内容date替换该行:do stat

do date -d@$(stat -c%Y "$k") +"the folder $k was modified on %b %d %R"

然后,您可以使用date中解释的通常的格式序列man date,此处的格式序列将May 15 01:19按照您的要求进行打印。

最后但同样重要的一点是,为了漂亮地打印上述任何输出,您可以在目录名称后插入一个制表符并使用column如下方法:

#!/bin/bash
shopt -s globstar
for i
  do for k in **/"$i"*/
    do date -d@$(stat -c%Y "$k") +"the folder $k%twas modified on %b %d %R"
  done
done | column -ts$'\t'

示例运行

$ tree
.
├── 1
│   └── 1
│       └── 1
├── 1something
└── 2
    └── 1
$ bash -O globstar -c 'for i; do for k in **/"$i"*/; do stat -c "the folder %n was modified on %y" "$k"; done; done' _ 1
the folder 1/ was modified on 2018-06-07 09:45:28.098693507 +0200
the folder 1/1/ was modified on 2018-06-07 09:45:28.098693507 +0200
the folder 1/1/1/ was modified on 2018-06-07 09:45:28.098693507 +0200
the folder 1something/ was modified on 2018-06-07 09:55:36.066531665 +0200
the folder 2/1/ was modified on 2018-06-07 09:45:28.098693507 +0200
$ bash -O globstar -c 'for i; do for k in **/"$i"*/; do stat -c "the folder %n was modified on %y" "$k" | sed 's/:[^:]*$//'; done; done' _ 1
the folder 1/ was modified on 2018-06-07 09:45
the folder 1/1/ was modified on 2018-06-07 09:45
the folder 1/1/1/ was modified on 2018-06-07 09:45
the folder 1something/ was modified on 2018-06-07 09:55
the folder 2/1/ was modified on 2018-06-07 09:45
$ bash -O globstar -c 'for i; do for k in **/"$i"*/; do date -d@$(stat -c%Y "$k") +"the folder $k was modified on %b %d %R"; done; done' _ 1
the folder 1/ was modified on Jun 07 09:45
the folder 1/1/ was modified on Jun 07 09:45
the folder 1/1/1/ was modified on Jun 07 09:45
the folder 1something/ was modified on Jun 07 09:55
the folder 2/1/ was modified on Jun 07 09:45
$ bash -O globstar -c 'for i; do for k in **/"$i"*/; do date -d@$(stat -c%Y "$k") +"the folder $k%twas modified on %b %d %R"; done; done | column -ts'"$'\t'" _ 1
the folder 1/           was modified on Jun 07 09:45
the folder 1/1/         was modified on Jun 07 09:45
the folder 1/1/1/       was modified on Jun 07 09:45
the folder 1something/  was modified on Jun 07 09:55
the folder 2/1/         was modified on Jun 07 09:45

答案3

find命令可以用一行完成您需要的操作

您可以查看“查看printf”中的操作以find
了解man findprintf 的参数详细信息

例子

find /PATH/TO/SCAN -type d -iname '*pub*' -printf "%p %TY-%Tm-%Td %TH:%TM\n"

-type d        : search for folders
-iname '*pub*' : find the pattern case insensitive
%p             : display path of found folder
%TY            : time Year 
%Tm            : time month
%Td            : time day
%TH            : time hour
%TM            : time minutes
%TS            : time seconds


了解更多信息
GNU find 的官方网页
25 个 find 命令的实例

答案4

这里有一个小小的变化,它利用了-regex而不是-names

find . -type d -regex ".*\($1\).*$" -printf 'The folder %P was modified on %Tb %Td %TH:%TM\n'

这可以是单行脚本,或者更好的是 - 一个函数。调用方式如下:

./finder.sh 'Vid\|Doc'

这使得grep方法更加地道、更加类似。

相关内容