Skip to main content

On a server directory with over 100,000 PDF files, I needed to verify the existence of specific documents. Whether there are a few hundred or whatever the number of files, this article explores the practical use of command-line tools to quickly ascertain whether a file is present. In the area of server management or application development, especially on systems like Ubuntu equipped with Nginx, it's crucial to perform routine checks to ensure that all necessary files are in place. Whether it's deploying updates, verifying configurations, or making sure essential assets are readily available, the ability to swiftly check for file existence is invaluable. This article is about using command-line techniques and options that can help streamline this process.

 

Basic file check with test command

The test command is a simple and effective way to check for the existence of a file. The -f option is used to test whether a specified file exists and is a regular file (not a directory).

if test -f "/var/www/html/project/sites/default/files/products/medium.pdf"; then
   echo "File exists."
else
   echo "File does not exist."
fi

In this example, the script checks for the existence of medium.pdf in the specified directory. If the file exists, it echoes "File exists." If not, it echoes "File does not exist."

 

Using square brackets

An alternative to the test command is using square brackets [], which is more common in shell scripting:

if [ -f "/var/www/html/project/sites/default/files/products/medium.pdf" ]; then
   echo "File exists."
else
   echo "File does not exist."
fi

This does the same thing as the previous example, with a slightly different syntax.

 

Checking for different types of files

Besides -f, there are other options to check for different types of files or file conditions

-d check if a directory exists
-e check if a file or directory exists
-f check if a file is readable
-w check if a file is writable
-x check if a file is executable

To check if a directory exists

if [ -d "/var/www/html/project/sites/default/files/products" ]; then
   echo "Directory exists."
else
   echo "Directory does not exist."
fi

 

Combining multiple conditions

You can combine multiple conditions using logical operators like && (AND) and || (OR). For instance, to check if a file exists and is readable:

if [ -f "/var/www/html/project/sites/default/files/products/medium.pdf" ] && [ -r "/var/www/html/project/sites/default/files/products/medium.pdf" ]; then
   echo "File exists and is readable."
else
   echo "File does not exist or is not readable."
fi

 

The wrap

Checking if a file exists using command-line queries is a handy skill for anyone managing servers. I've used this command to verify error logs' accuracy rather than checking the actual directory, which holds over 400,000 documents. This approach ensures that I'm focusing on the files that matter without getting bogged down by the sheer volume of the directory's contents. Whether you're verifying configuration files, ensuring deployment artifacts are in place, or managing Nginx on Ubuntu, these techniques are invaluable. The test command and its various options provide a flexible way to handle these checks efficiently.

Related articles

Andrew Fletcher16 Jul 2024
How to efficiently use find and replace in VS Code
When it comes to text editing, Visual Studio Code (VS Code) is one of the most popular tools among developers. One of its powerful features is the Find and Replace function, which can save you countless hours of manual editing. In this article, we'll explore how to use this function to replace all...
Andrew Fletcher09 Jun 2024
Overcoming startup challenges with Apache Solr on Ubuntu 24.04
Recently, after upgrading to Ubuntu 24.04, we encountered a significant challenge with our Apache Solr service—it refused to restart. This post documents the steps I took to diagnose and resolve the issues, providing a clear guide for anyone facing similar troubles. Initial troubleshootingThe...