【问题标题】:multiple files to one folder shells script多个文件到一个文件夹 shell 脚本
【发布时间】:2013-03-24 02:13:28
【问题描述】:

我正在尝试编写一个脚本,它将文件移动到基​​于文件名创建的文件夹中 每个文件有 2 个副本,名称完全相同,但文件扩展名不同。

例子

之前

dir1 - one.txt one.rtf two.txt two.rtf other.txt other.rtf

之后

dir1 - one two other

dir1/one - one.txt one.rtf

dir1/two - two.txt two.rtf

dir1/other - other.txt other.rtf

我之前将文件放在文件夹脚本中,但我不确定如何将多个文件放入 1 个文件夹中

这是文件到文件夹的代码。

#!/bin/bash

dir="/home/user1/Desktop/f2f/"

for file in ${dir}/*
do
        mkdir -p "${file/./#}"
        mv "${file}" "${file/./#}/"
done

无论如何,我们将不胜感激,如果有帮助,命名约定和文件扩展名将始终相同

【问题讨论】:

    标签: shell file-io


    【解决方案1】:

    我不太确定您的原始脚本的目的是什么,因为您似乎为每个名为 <filename>#<extension><file>.<extension> 生成一个文件夹,然后将 <file>.<extension> 放置在其中。

    我猜你要找的版本是这个:

    for file in *
    do
        mkdir -p ${file%.*}
        mv $file ${file%.*}/
    done
    

    请务必使用带有单个 (!) %ungreedy 变体,因为您只想从文件名中删除最后一个组件。

    例如,想象一个名为 first.part.second.part.txt 的文件,您只想在其中剥离 .txt

    在 (find dir1) 之前给出以下文件夹布局:

    dir1/
    dir1/two.txt
    dir1/one.rtf
    dir1/two.rtf
    dir1/other.txt
    dir1/other.rtf
    dir1/one.txt
    

    这将导致以下布局(再次find dir1):

    dir1
    dir1/other
    dir1/other/other.txt
    dir1/one
    dir1/one/one.rtf
    dir1/one/one.txt
    dir1/two
    dir1/two/two.txt
    dir1/two/two.rtf
    

    如果那是你要找的。​​p>

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多