简体   繁体   English

如何检查文件夹中的所有文件是否具有相同的格式

[英]How to check if all the files in a folder are of the same format

I have a folder which contains a list of files. 我有一个包含文件列表的文件夹。 The problem is they dont have extensions. 问题是他们没有扩展名。 I want to check if they are JPEG or JPG format. 我想检查它们是否为JPEG或JPG格式。 I tried this 我试过了

find folder -type f -not -name "*.*"

The output is 输出是

folder/t351
folder/t352
folder/t353
folder/t354
folder/t355

I tried using Regex but since the name of the files have no extensions It din't work out. 我尝试使用Regex,但是由于文件名没有扩展名,因此无法正常工作。 Can someone tell me how I can validate the files and check if they have a certain format. 有人可以告诉我如何验证文件并检查它们是否具有特定格式。

How about this? 这个怎么样?

PROMPT> ls * | sed 's/^.*$/"&"/g' | xargs file
IREffectFilter.m:                                        ASCII C++ program text, with CRLF line terminators
MacBook Air.spx:                                         XML  document text
PGFilteredImageView.m:                                   ASCII C++ program text
Screen Shot 2012-09-27 at 1.33.50 PM.png:                PNG image data, 559 x 152, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-02 at 10.04.37 AM.png:               PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-02 at 2.08.53 PM.png:                PNG image data, 937 x 1158, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-03 at 9.50.16 AM.png:                PNG image data, 651 x 347, 8-bit/color RGB, non-interlaced
Screen Shot 2012-10-08 at 11.14.51 AM.png:               PNG image data, 1343 x 1528, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-08 at 12.09.01 PM.png:               PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-08 at 12.29.16 PM.png:               PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-09 at 8.44.05 AM.png:                PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
Screen Shot 2012-10-11 at 4.17.14 PM.png:                PNG image data, 880 x 1136, 8-bit/color RGBA, non-interlaced
breakpoint1.sh:                                          Bourne-Again shell script text executable
breakpoint2.sh:                                          Bourne-Again shell script text executable
bugtracker.txt:                                          UTF-8 Unicode text
error_svn_rename.txt:                                    UTF-8 Unicode English text
long_text.txt:                                           data

You should use the file command. 您应该使用file命令。 I propose you a quick code that you can refine: 我为您提供了一个可以完善的快速代码:

for f in directory
do
    isJPEG=$(file $f | grep JPEG)
    if [ -n "$isJPEG" ]; then
       echo $f
    fi
done

Try ls | grep -v '\\.' |file -f - 试试ls | grep -v '\\.' |file -f - ls | grep -v '\\.' |file -f - ls | grep -v '\\.' |file -f - . ls | grep -v '\\.' |file -f - For example, in a directory where ls shows 例如,在ls显示的目录中

  2 ga\(\)\    ab cd  date.txt  ga() mo  times  x?gh

and file * shows file *显示

  2 ga\(\)\  : ASCII text, with very long lines
  ab cd:       empty
  date.txt:    ASCII text
  ga() mo:     PDF document, version 1.5
  times:       HTML document, ASCII text
  x?gh:        JPEG image data, JFIF standard 1.01

the command pipe 命令管道
ls | grep -v '\\.' |file -f -
shows 表演

  2 ga\(\)\  :           ASCII text, with very long lines
  ab cd:     empty
  ga() mo:       PDF document, version 1.5
  times:     HTML document, ASCII text
  x?gh:    JPEG image data, JFIF standard 1.01

(Apparently file - is outputting extra tabs or spaces within lines in this example. Note, the first file name has two spaces at its end, as seen by spaces before the colon.) Also note, the command (显然file -在本例中,输出线内多余的制表符或空格注意,第一个文件名在其端部的两个空间,由冒号之前的空间所看到。)还要注意,该命令
ls | grep -v '\\.' |file -f - |grep -i jpeg
produced: 生产:

  x?gh:    JPEG image data, JFIF standard 1.01

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有文件和文件夹移动到同一文件夹的子文件夹中 - How to move all files and folders into a subfolder of the same folder 检查文件中所有行的格式是否相同 - Check if all lines in a file are in the same format 将文件夹中复制的所有文件的权限设置为相同 - Set the permissions of all files copied in a folder the same 将文件夹中的所有文件复制到同一路径下的文件夹中 - copy all files in folder, into a folder within that same path 如何找到各个子文件夹中的所有tar文件,然后将它们解压缩到找到的同一文件夹中? - How to find all tar files in various sub-folders, then extract them in the same folder they were found? 如何重命名 linux 目录中具有不同/随机扩展名的同一文件夹中的所有文件? - How to rename all files in the same folder with different/random extentions in a directory on linux? Bash检查文件夹中的所有文件是否都可被www-data写入 - Bash check if all files in a folder are writable by www-data 在Linux上检查所有文件和文件夹所有者/组以及权限的任何命令 - Any command to check all files and folder owner / group and permission on linux 将所有以相同前缀开头的文件放在linux的文件夹中 - Put all the files starting with same prefix in a folder in linux 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM