我正试图编写一个脚本,将文件移动到基于文件名创建的文件夹中每个文件有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
我以前把一个文件到文件夹的脚本,但我不知道如何让它把多个文件到一个文件夹
显示文件到文件夹的代码。
#!/bin/bash
dir="/home/user1/Desktop/f2f/"
for file in ${dir}/*
do
mkdir -p "${file/./#}"
mv "${file}" "${file/./#}/"
done
无论如何,任何帮助都将是感激的,命名约定和文件扩展名将总是相同的,如果有帮助的话
我不太确定您的原始脚本的目的是什么,因为您似乎为每个名为<filename>#<extension>
的<file>.<extension>
生成一个文件夹,然后将<file>.<extension>
放在那里。
我猜你要找的版本是这个:
for file in *
do
mkdir -p ${file%.*}
mv $file ${file%.*}/
done
一定要使用带有单个(!)的不贪婪的变体%
,因为您只想从文件名中删除最后一个组件。
想象一个名为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