[英]Linux: How to move files with same name, diff ext. into their own folder?
I have files like this This list is a sample of my files note the actual files are not in sucessive order. 我有这样的文件此列表是我的文件的示例,请注意,实际文件的顺序不是连续的。
file1.a
file2.a
file1.b
file2.b
...
and some have a .c extension but not all 有些扩展名为.c,但并非全部
How would I move these files into their own named folder. 我如何将这些文件移动到它们自己的命名文件夹中。
I have tried this 我已经试过了
find . -type f -print0 | xargs -0 -l sh -c 'mkdir "${1%.*}" && mv "$1" "${1%.*}"' sh
but it doesn't work as intended ie Well it creates the folders but wont put the second file of same name different extension in the same folder. 但是它不能按预期工作,也就是说,它会创建文件夹,但不会将具有相同扩展名的第二个文件放在同一文件夹中。
mkdir: cannot create directory ‘./file1’: File exists
mkdir: cannot create directory ‘./file2’: File exists
mkdir: cannot create directory ‘./file3’: File exists
您应该使用mkdir -p
,如果目录存在,它不会抱怨(并打破&&
)(如果目录不存在,它也会创建父目录)。
for FILE in $(ls file[0-9].[a-z]) do DIRNAME=$(echo $FILE |cut -c1-5) [ -d $DIRNAME ] || mkdir $DIRNAME mv ${FILE}* $DIRNAME done This will give you: $ ls file1 file2 file1: file1.a file1.b file1.c file2: file2.a file2.b file2.c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.