简体   繁体   English

Linux:如何移动具有相同名称的文件diff ext。 放入自己的文件夹?

[英]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.

相关问题 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 如何在Linux中根据名称在具有相应名称的文件夹中移动文件? - How to move files in Linux based on its name in a folder with a corresponding name? 如何使用Linux(debian)命令行将所有类型的文件file1.ext,.file2ext和.anotherext从文件夹移动到另一个文件夹? - How to move all the type file file1.ext, .file2ext and .anotherext from folder to another folder with linux (debian) command line? 复制/移动不同文件夹中同名的多个文件 - copy/move same name multiple files in different folder Linux中相同目录结构中文件之间的差异 - Diff between files in same directory structure in linux 如何使用diff命令在目录中查找具有相同名称的文件? - How to find files with same name part in directory using the diff command? 如何在Linux中基于父文件夹移动和重命名文件? - How to move and rename files based on parent folder in Linux? 如何根据 linux 中的名称将文件移动到新目录? - How do I move files to a new directory based on their name in linux? 在Linux中以maxdepth 2查找文件并移至相应的文件夹 - Find files with maxdepth 2 in Linux and move to respective folder linux bash脚本创建文件夹和移动文件 - linux bash script to create folder and move files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM