巴什循环 - 在一个目录中创建为每个现有的TXT文件的文件夹 [英] Bash loop - creating a folder for each existing txt file in a directory

查看:113
本文介绍了巴什循环 - 在一个目录中创建为每个现有的TXT文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个目录下的文本文件,我想组织。我试图通过为每个文件的目录这样做THRU庆典。当我将文件移动到各自新的文件夹,我要修改原来的文件名,并创建一个额外的文本文件labeld 信息.txt 保存原文件名(该文件也放在新的文件夹)。这些文件名称的改变,从去XXX-FILE_NAME-aa1.txt XXX-aa1.txt 。第一个和最后一个之间的任何东西 - 被剥离出来。该文件夹具有相同的名称文件 XXX-AA1

test.sh(sed的我声明后带和whitspaces把一切都为小写除了可最后 -

  FILE_PATH =/媒体/ sf_linux_sandbox / txt_files /
而读-r文件;做
#sed空语句
NEW_NAME = $(回声$文件| sed的'S / ^ \\(* \\)\\( - [^ - ] * \\)$ / \\大号\\ 1 \\ E \\ 2 /; S / * - * / - /(。* \\)S / ^ \\ \\ + - * \\([^ ​​- ] * \\)$ / \\ 1 \\ 2 /; S / / _ / G')
MV$文件,$ NEW_NAME
完成< ≤($找到-maxdepth FILE_PATH 1型的F -name* .TXT)

输入:

  |  -  ./
| | - XXXX-test_file1-aa1.txt
| | - XXXX-test_file2-bb2.txt

所需的输出:

  |  -  ./
| | - XXXX-AA1
| | --xxx-aa1.txt
| | --info.txt //包含名称'test_file1
| | - XXXX-BB2
        | --xxx-bb2.txt
        | --info.txt //包含名称'test_file2

电流输出

  |  -  ./
| | - XXXX-test_file里面-AA1
| | --xxxx-test_file里面,aa1.txt
| | - XXXX-test_file里面,BB2
        | --xxxx-test_file里面,bb2.txt


解决方案

您其实可以做所有这些操作中的Bash本身。

既然你有 -maxdepth 1 它看起来像你可以使用通配符循环,而不是找到所以我重构这一点。

  FILE_PATH =/媒体/ sf_linux_sandbox /视频/
在$ FILE_PATH/ * MP4文件。做
    TMP = $ {#文件* - };头= $ {文件% - $ TMP}
    中期= $ {TMP% - *};尾= $ {TMP#$中旬 - }
    基地=$ {头,,} - $ {尾,,}
    DIR = $ {基地%.MP4}
    MKDIR -p$目录
    MV$文件,$ DIR / $基地
    回声$中旬>中$ DIR/info.txt
DONE

简而言之, $ {VAR#preFIX} 扩展到 VAR 的值与 preFIX 删除, $ {VAR%后缀} 相应地执行与后缀相同的替换。最后, $ {VAR ,,} 产生价值的小写版本。然后,我们简单地组装您从这些部件所需的文件名结构。

I have text files in a directory that I want to organize. I am trying to do so thru bash by creating a directory for each file. As I move the file to its respective new folder, I want to modify the original filename and create an additional text file labeld info.txt that holds the original file name(this file is also place in the new folder). The files name change goes from xxx-file_name-aa1.txt to xxx-aa1.txt. Anything between the first and last - is stripped out. The folder has the same name as the file xxx-aa1.

test.sh (my sed statement strips whitspaces and turns everything to lower case except anything after the last -

FILE_PATH="/media/sf_linux_sandbox/txt_files/"
while read -r file; do
#sed statement empty
new_name=$(echo "$file" | sed 's/^\(.*\)\(-[^-]*\)$/\L\1\E\2/; s/ *- */-/; s/^\(.*\) \+- *\([^-]*\)$/\1-\2/; s/ /_/g')
mv "$file" "$new_name"
done < <(find $FILE_PATH -maxdepth 1 -type f -name '*.txt')

Input:

|-- ./
|   |-- xxxx-test_file1-aa1.txt
|   |-- xxxx-test_file2-bb2.txt

Desired Output:

|-- ./
|   |-- xxxx-aa1
|       |--xxx-aa1.txt
|       |--info.txt // contains name 'test_file1'
|   |-- xxxx-bb2
        |--xxx-bb2.txt
        |--info.txt // contains name 'test_file2'

Current Output

|-- ./
|   |-- xxxx-test_file-aa1
|       |--xxxx-test_file-aa1.txt
|   |-- xxxx-test_file-bb2
        |--xxxx-test_file-bb2.txt

解决方案

You can actually do all these manipulations in Bash itself.

Since you have -maxdepth 1 it looks like you can just use a wildcard loop instead of find so I refactored that, too.

FILE_PATH="/media/sf_linux_sandbox/videos/"
for file in "$FILE_PATH"/*.mp4; do
    tmp=${file#*-}; head=${file%-"$tmp"}
    mid=${tmp%-*}; tail=${tmp#"$mid"-}
    base="${head,,}-${tail,,}"
    dir=${base%.mp4}
    mkdir -p "$dir"
    mv "$file" "$dir/$base"
    echo "$mid" >"$dir"/info.txt
done

Briefly, ${var#prefix} expands to the value of var with prefix removed, and ${var%suffix} correspondingly performs the same substitution with a suffix. Finally, ${var,,} produces the lowercase version of the value. Then we simply assemble the file name structure you want from those parts.

这篇关于巴什循环 - 在一个目录中创建为每个现有的TXT文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆