0

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

./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**

我想从每个文件夹的开头提取日期。例如,在 regex: 中([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**

我该如何从命令行执行此操作?

4

4 回答 4

1

我理解这个问题的方式与肯特不同。我认为您想从每个原始目录创建一个新的目录树并移动它包含的所有文件。如果这是您正在寻找的内容,您可以尝试遵循

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
于 2013-09-06T23:01:30.363 回答
1

假设您的文件存储在“旧”文件夹中,那么您可以编写一个 shell 脚本(避免使用文件名包含空格的“for”循环):

mkdir -p new
ls -d -1 old/*/* | while read oldfile; do
    newfile=`echo "$oldfile" | sed -r 's#^old/([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2})(.*)$#new/\1/\2/\3/\4#'`
    newdir=` echo $newfile | sed 's#/[^/]*$##'`
    echo "Creating \"$newdir\""
    mkdir -p "$newdir"
    echo "Moving files from \"$oldfile\" to \"$newfile\""
    cp -r "$oldfile" "$newfile"
done

脚本输出:

Creating "new/1993/02/22/ - The Moon - Tallahassee, FL"
Moving files from "old/1993-02-22 - The Moon - Tallahassee, FL/test" to "new/1993/02/22/ - The Moon - Tallahassee, FL/test"
Creating "new/1993/02/23/ - The Moon - Tallahassee, FL"
Moving files from "old/1993-02-23 - The Moon - Tallahassee, FL/test" to "new/1993/02/23/ - The Moon - Tallahassee, FL/test"
Creating "new/1993/02/24/ - The Moon - Tallahassee, FL"
Moving files from "old/1993-02-24 - The Moon - Tallahassee, FL/test" to "new/1993/02/24/ - The Moon - Tallahassee, FL/test"
Creating "new/1993/02/25/ - The Moon - Tallahassee, FL"
Moving files from "old/1993-02-25 - The Moon - Tallahassee, FL/test" to "new/1993/02/25/ - The Moon - Tallahassee, FL/test"
Creating "new/1993/03/01/ - The Tes - Null, FL"
Moving files from "old/1993-03-01 - The Tes - Null, FL/test2" to "new/1993/03/01/ - The Tes - Null, FL/test2"

你会在“新”文件夹中找到你的新树:

$ tree old new
old
├── 1993-02-22 - The Moon - Tallahassee, FL
│   └── test
├── 1993-02-23 - The Moon - Tallahassee, FL
│   └── test
├── 1993-02-24 - The Moon - Tallahassee, FL
│   └── test
├── 1993-02-25 - The Moon - Tallahassee, FL
│   └── test
└── 1993-03-01 - The Tes - Null, FL
    └── test2
new
└── 1993
    ├── 02
    │   ├── 22
    │   │   └──  - The Moon - Tallahassee, FL
    │   │       └── test
    │   ├── 23
    │   │   └──  - The Moon - Tallahassee, FL
    │   │       └── test
    │   ├── 24
    │   │   └──  - The Moon - Tallahassee, FL
    │   │       └── test
    │   └── 25
    │       └──  - The Moon - Tallahassee, FL
    │           └── test
    └── 03
        └── 01
            └──  - The Tes - Null, FL
                └── test2
于 2013-09-07T14:50:14.440 回答
0

像这样?

kent$  awk '{gsub(/-/,"/",$1);sub(/^[^/]*\//,"/",$NF);print $1$NF}' file
./1993/02/22/**files**
./1993/02/23/**files**
./1993/02/24/**files**
./1993/02/25/**files**
./1993/03/01/**files**
于 2013-09-06T22:44:44.790 回答
0

这是sed+awk经过这么多评论后:

awk 'BEGIN{FS="-"}{print $1"/"$2"/"$3}' file | awk 'BEGIN{FS="**files**"}{print $1"/"FS}' | sed -e 's/ \//\//g'

./1993/02/22/**files**
./1993/02/23/**files**
./1993/02/24/**files**
./1993/02/25/**files**
./1993/03/01/**files**
于 2013-09-06T22:58:47.783 回答