如何在bash上使用filetype将文件排序到文件夹中(使用'file'命令)?

时间:2022-10-17 15:57:13

I have thousands of files without extensions after recovery (mostly pictures). I need to sort them into separate folders by filetype (folders must be created during sort process). I can determine filetype in linux using "file" command. Does somebody have bash script for it?

恢复后我有数千个没有扩展名的文件(主要是图片)。我需要按文件类型将它们分类到单独的文件夹中(必须在排序过程中创建文件夹)。我可以使用“file”命令在linux中确定filetype。有人有bash脚本吗?

For example: Initial dir contains files: 001, 002, 003, 004. After sorting should be 3 dirs: 'jpeg' contain 001.jpg, 003.jpg; 'tiff' contain 002.tiff and 'others' contain 004.

例如:初始目录包含文件:001,002,003,004。排序后应为3个目录:'jpeg'包含001.jpg,003.jpg; 'tiff'包含002.tiff,'others'包含004。

4 个解决方案

#1


3  

This answer does not execute file command multiple times for each file, which is unnecessary

此答案不会为每个文件多次执行文件命令,这是不必要的

file  -N --mime-type -F"-&-" * | awk -F"-&-" 'BEGIN{q="\047"}
{
  o=$1
  gsub("/","_",$2);sub("^ +","",$2)
  if (!($2  in dir )) {
    dir[$2]
    cmd="mkdir -p "$2
    print cmd
    #system(cmd) #uncomment to use
  }
  files[o]=$2
}
END{
 for(f in files){
    cmd="cp "q f q"  "q files[f]"/"f".jpg" q
    print cmd
    #system(cmd) #uncomment to use
 }
}'

similarly, can be done with bash4+ script using associative arrays.

同样,可以使用关联数组使用bash4 +脚本完成。

#2


3  

How about something like this:

这样的事情怎么样:


mkdir -p `file -b --mime-type *|uniq`
for x in `ls`
do
        cp $x `file -b --mime-type $x`
done

I use cp, it can't work with directories.

我使用cp,它无法使用目录。

#3


2  

Dadam's answer adjustment:

达达姆的答案调整:

#!/bin/bash

file --mime-type -F"&" [YOUR PATH]/* > filetypes.txt
mkdir -p `cut -f2 -d"&" filetypes.txt | sed 's/[ ,:]//g' | sort -u`
for x in `cut -f1 -d"&" filetypes.txt`
do
  mv $x `file -b --mime-type $x | sed 's/[ ,:]//g'`
done 

#4


2  

I use this and it works for me :

我用它,它对我有用:

#!/bin/bash

self_name=`basename "$0"`
for f in *
do
    if [  -f "$f" ] && [ "$f" !=  "$self_name" ]; then
        filename="${f%.*}"
        ext="${f##*.}"
        mkdir $ext

        mv "$f" "$ext/$f"

        htmlfiles="{$f}_files"
        if [ -d "$htmlfiles" ];then
            mv "$htmlfiles" "$ext/$htmlfiles"
        fi

        if [ $? -ne 0 ]
            then
            echo "Error: Failed mv $f"
        fi
    fi

    if [  -d "$f" ];then
        mv "$f" "$ext/$f"
    fi
done

You should put it in your directory and run it and it make a sub directory for extension

您应该将它放在您的目录中并运行它,并为扩展创建一个子目录

#1


3  

This answer does not execute file command multiple times for each file, which is unnecessary

此答案不会为每个文件多次执行文件命令,这是不必要的

file  -N --mime-type -F"-&-" * | awk -F"-&-" 'BEGIN{q="\047"}
{
  o=$1
  gsub("/","_",$2);sub("^ +","",$2)
  if (!($2  in dir )) {
    dir[$2]
    cmd="mkdir -p "$2
    print cmd
    #system(cmd) #uncomment to use
  }
  files[o]=$2
}
END{
 for(f in files){
    cmd="cp "q f q"  "q files[f]"/"f".jpg" q
    print cmd
    #system(cmd) #uncomment to use
 }
}'

similarly, can be done with bash4+ script using associative arrays.

同样,可以使用关联数组使用bash4 +脚本完成。

#2


3  

How about something like this:

这样的事情怎么样:


mkdir -p `file -b --mime-type *|uniq`
for x in `ls`
do
        cp $x `file -b --mime-type $x`
done

I use cp, it can't work with directories.

我使用cp,它无法使用目录。

#3


2  

Dadam's answer adjustment:

达达姆的答案调整:

#!/bin/bash

file --mime-type -F"&" [YOUR PATH]/* > filetypes.txt
mkdir -p `cut -f2 -d"&" filetypes.txt | sed 's/[ ,:]//g' | sort -u`
for x in `cut -f1 -d"&" filetypes.txt`
do
  mv $x `file -b --mime-type $x | sed 's/[ ,:]//g'`
done 

#4


2  

I use this and it works for me :

我用它,它对我有用:

#!/bin/bash

self_name=`basename "$0"`
for f in *
do
    if [  -f "$f" ] && [ "$f" !=  "$self_name" ]; then
        filename="${f%.*}"
        ext="${f##*.}"
        mkdir $ext

        mv "$f" "$ext/$f"

        htmlfiles="{$f}_files"
        if [ -d "$htmlfiles" ];then
            mv "$htmlfiles" "$ext/$htmlfiles"
        fi

        if [ $? -ne 0 ]
            then
            echo "Error: Failed mv $f"
        fi
    fi

    if [  -d "$f" ];then
        mv "$f" "$ext/$f"
    fi
done

You should put it in your directory and run it and it make a sub directory for extension

您应该将它放在您的目录中并运行它,并为扩展创建一个子目录