Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
告诉bash中的文件是否为PDF_Bash_Pdf_File Type - Fatal编程技术网

告诉bash中的文件是否为PDF

告诉bash中的文件是否为PDF,bash,pdf,file-type,Bash,Pdf,File Type,我需要编写一个bash脚本来判断一个文件是否是pdf文件。但是,我不能简单地使用文件名或扩展名 例如: test.pdf.encrypt-不会打开,因为文件本身已加密,并且文件类型未知,计算机无法识别 test.pdf.decrypt-即使扩展名为.decrypt,也将打开 查看扩展并没有帮助,加密和解密的文件在名称中间都有.pdf,有没有方法进行系统测试,看看文件是否甚至可以用PDF读取器读取? 我只需要可以输入bash中if语句的命令 if [this file is a working p

我需要编写一个bash脚本来判断一个文件是否是pdf文件。但是,我不能简单地使用文件名或扩展名

例如:

test.pdf.encrypt-不会打开,因为文件本身已加密,并且文件类型未知,计算机无法识别

test.pdf.decrypt-即使扩展名为.decrypt,也将打开

查看扩展并没有帮助,加密和解密的文件在名称中间都有.pdf,有没有方法进行系统测试,看看文件是否甚至可以用PDF读取器读取? 我只需要可以输入bash中if语句的命令

if [this file is a working pdf file]; do
   echo "$file is a working pdf file."
fi

每个PDF文件都以
%PDF
开头。您可以比较指定文件的前4个字符,以确保它是PDF

FILE="/Users/Tim/Documents/My File.pdf"
if [ $(head -c 4 "$FILE") = "%PDF" ]; then
    echo "It's a PDF!"
fi

每个PDF文件都以
%PDF
开头。您可以比较指定文件的前4个字符,以确保它是PDF

FILE="/Users/Tim/Documents/My File.pdf"
if [ $(head -c 4 "$FILE") = "%PDF" ]; then
    echo "It's a PDF!"
fi

另一个选项是对文件使用
文件

type="$(file -b $file)"
if [ "${type%%,*}" == "PDF document" ]; then
  echo "$file is a PDF file."
fi

另一个选项是对文件使用
文件

type="$(file -b $file)"
if [ "${type%%,*}" == "PDF document" ]; then
  echo "$file is a PDF file."
fi

在Linux和Solaris上,file命令将标识一种文件类型;具体来说,PDF文档是多种类型中的一种

file filename.xxx | grep -q 'PDF' && echo 'is pdf file'  || echo 'is not pdf'

无论文件扩展名是什么。

在Linux和Solaris上,file命令将标识一种文件类型;具体来说,PDF文档是多种类型中的一种

file filename.xxx | grep -q 'PDF' && echo 'is pdf file'  || echo 'is not pdf'
不考虑文件扩展名