gpt4 book ai didi

regex - linux中目录的半复杂重组

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:45 25 4
gpt4 key购买 nike

我有一堆目录需要重组。它们的格式如下:

./1993-02-22 - The Moon - Tallahassee, FL/**files**
./1993-02-23 - The Moon - Tallahassee, FL/**files**
./1993-02-24 - The Moon - Tallahassee, FL/**files**
./1993-02-25 - The Moon - Tallahassee, FL/**files**
./1993-03-01 - The Test - Null, FL/**files**

我想从每个文件夹的开头提取日期。例如,在正则表达式中:([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2}) 并将目录重新格式化为 ./year/month/day

所以,它应该输出:

./1993/02/22/**files**
./1993/02/23/**files**
./1993/02/24/**files**
./1993/02/25/**files**
./1993/03/01/**files**

我怎样才能从命令行执行此操作?

最佳答案

我对这个问题的理解方式不同于 Kent .我以为您想从每个原始目录创建一个新的目录树并移动它包含的所有文件。您可以尝试关注 脚本,如果那是您正在寻找的:

perl -MFile::Path=make_path -MFile::Copy=move -e '
for ( grep { -d } @ARGV ) {
@date = m/\A(\d{4})-(\d{2})-(\d{2})/;
next unless @date;
$outdir = join q{/}, @date;
make_path( $outdir );
move( $_, $outdir );
}
' *

它从当前目录读取每个文件(* 作为参数传递)并进行两步过滤。第一个是用于无目录文件的 grep,第二个是用于不以它开头的文件的未定义 @date。然后它将日期的组件加入一个路径,如果不存在则创建它,并将旧的及其所有文件移动到新的。

一个测试:

此处 ls -lR 的结果显示初始状态:

.:
total 24
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 1993-02-22 - The Moon - Tallahassee, FL
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 1993-02-23 - The Moon - Tallahassee, FL
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 1993-02-24 - The Moon - Tallahassee, FL
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:57 1993-02-25 - The Moon - Tallahassee, FL
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:57 1993-03-01 - The Test - Null, FL
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:47 dummy_dir
-rw-r--r-- 1 dcg dcg 0 sep 7 00:47 dummy_file

./1993-02-22 - The Moon - Tallahassee, FL:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file1
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file2

./1993-02-23 - The Moon - Tallahassee, FL:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file3

./1993-02-24 - The Moon - Tallahassee, FL:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file4

./1993-02-25 - The Moon - Tallahassee, FL:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file5
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file6

./1993-03-01 - The Test - Null, FL:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file7

./dummy_dir:
total 0

在运行之前的脚本后请注意,基本目录仅保留虚拟文件和创建的树的根目录 (1993)。运行相同的 ls -lR 会产生:

.:
total 8
drwxr-xr-x 4 dcg dcg 4096 sep 7 00:59 1993
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:47 dummy_dir
-rw-r--r-- 1 dcg dcg 0 sep 7 00:47 dummy_file

./1993:
total 8
drwxr-xr-x 6 dcg dcg 4096 sep 7 00:59 02
drwxr-xr-x 3 dcg dcg 4096 sep 7 00:59 03

./1993/02:
total 16
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 22
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 23
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:56 24
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:57 25

./1993/02/22:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file1
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file2

./1993/02/23:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file3

./1993/02/24:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:56 file4

./1993/02/25:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file5
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file6

./1993/03:
total 4
drwxr-xr-x 2 dcg dcg 4096 sep 7 00:57 01

./1993/03/01:
total 0
-rw-r--r-- 1 dcg dcg 0 sep 7 00:57 file7

./dummy_dir:
total 0

关于regex - linux中目录的半复杂重组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com