how to check if a file is password protected using command line in linux


how I could check if the uploaded file to my server ( Doc , Docx , Xsl , Xslx … ) is protected

This is definitely not an easy job, since the method will depend on the file format. E.g. Microsofts .doc/.xls are binary formats while .odt/.docx are zipped archives of XML files For .odt it would be required to extract META-INF/manifest.xml and to perform an analysis of the XML content and entries containing manifest:salt=… (or to look whether any other of the XML files are just text files or are of type data). I’m not aware of any tool, which could provide a simple linux call like IsProtected -f <file> throwing a “yes” or “no”.

The only - but heuristic - way may be to call libreoffice6.2 --cat <file>.If ist protected the command results in Error: source file could not be loaded message (tested only for .odt files). Since there may be other reasons for that error message, I call it a heuristic method. It does not allow for a definite decision “is protected”.

the libreoffice6.2 --cat <file> would not work with the xslx I have tried it and I have gotten

Error: Please verify input parameters... (SfxBaseModel::impl_store <file:///tmp/brtZFk> failed: 0xc10(Error Area:Io Class:Write Code:16))

As stated above (tested only for .odt files) and fact in my opinion this is not a task for LibreOffice and this forum is not the correct one.

Normally if I used a software to read some files the software will try to read them and if they were protected then the software will throw an error that those data is protected this what I have when I use - as for example - Ghostscript to read and merge pdf files

the problem is when I use LibreOffice to convert the word or excel file to pdf it didn’t say anything when he failed to convert then it just does nothing and I was searching for a way to know what is happening

I fully understand the requirement. I have repeated the test using a protected .xslx and in fact it also throws an Error: source file could not be loaded message. The message from your first comment, though an error message, indicates that the .xlsx is not protected. Otherwise the “could not be loaded” message would result from the call. Thus in a script the heuritisc decision could be

if [[ "$(libreoffice6.2 --cat <file> | grep "Error: source file could not be loaded")" != "" ]]; then

thanx I will try it again

Correction: I forgot 2>&1, thus scriptlet must read:

if [[ "$(libreoffice6.2 --cat <file> 2>&1 |  grep "Error: source file could not be loaded")" != "" ]]; then

actually I am trying to execute this command

/usr/bin/libreoffice6.1 -env:UserInstallation=file:///var/www/aalyan/html/trunk/backend/tmp//soffice_profile_${USER} --cat word.docx 2>&1

using PHP exec function but when I call it, it goes and didn’t return as a usual

did you have any idea about this

Hello - I got no idea (I’m not very familiar with the command line execution of LO) and I consider this a new question. To increase the probability to get an answer, I’d suggest you create a new question