简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM