1. Overview

In this tutorial, we’re going to look at how to add a number to a filename while creating it. We’ll look at the solution using Linux commands and shell scripting.

2. Introduction to the Problem

Our problem is creating a file with a unique filename by adding a number. The main objective is to avoid overwriting existing files and ensure each new file has a distinct name. To achieve this, we need to check if the desired filename already exists.

If it does, we increment a number and append it to the filename until we find a filename that is not already taken. Once a unique filename is determined, a new file is created with that name. This process allows us to systematically generate filenames with appended numbers to ensure uniqueness and avoid conflicts when creating files.

3. Using the touch Command

The touch command in Unix-like operating systems is used to update the access and modification timestamps of a file. If the file doesn’t exist, touch creates an empty file with the specified name. Let’s look at the basic usage of the touch command:

touch [OPTION...]  FILE...

Here [OPTION] represents any additional options that can be passed to the command, and FILE… refers to one or more filenames or paths of the files to be created or updated.

Some common options that can be used with touch include:

  • -a: Only update the access timestamp of the file
  • -c: Do not create the file if it doesn’t exist
  • -m: Only update the modification timestamp of the file
  • -r FILE: Use the timestamps of the reference file to update the specified file
  • -t STAMP: Use a specific timestamp instead of the current time

Overall, the touch command is a tool for updating file timestamps and creating files when needed.

4. Using -e and -L Options

So, now we know how to create files. Still, we need to figure out whether a filename exists or not somehow to construct the overall solution to our problem. To do this, we use -e and -L flags in conditional statements.

The -e flag is used to check if the file exists. It evaluates to true if the file exists, regardless of its type:

if [[ -e $filename]]; then

   echo "$filename exists"

fi

In the above, the condition checks if the file specified by the variable $filename exists. If it does, the script prints a message indicating the file exists.

The -L flag checks if a file is a symbolic link. It evaluates to true if the file is a symbolic link:

if [[ -e $filename]]; then

   echo "$filename is a symbolic link"

fi

In the above code snippet, the condition checks if the file specified by the variable $filename is a symbolic link. If it is, the script prints a message indicating that it’s a symbolic link.

5. Overall Picture

Now, let’s look at the implementation of the overall solution.

filename=example
if [[ -e $filename.ext || -L $filename.ext ]] ; then
    i=0
    while [[ -e $filename-$i.ext || -L $filename-$i.ext ]] ; do
        let i++
    done
    filename=$filename-$i
fi
touch -- "$filename".ext

The above code snippet efficiently adds a number to the filename while creating it using the touch command. The script first checks if a file with the given name and extension exists using -e and -L conditions. If it does, it enters a loop where it increments the number i and checks if a file with the numbered filename exists.

Once it finds a filename that doesn’t exists, it updates the name variable by appending the number with a hyphen and assigns the updated filename to it.

Finally, the touch command is used to create the file with the updated filename and extension.

This script effectively creates a file with a unique filename, ensuring that existing files are not overwritten.

Below is the example execution of the script:

script execution

6. Conclusion

In this article, we’ve analyzed touch command and -e and -L flags in conditions in shell scripting. Using them, we’ve looked at how to create new files while adding a number. By doing it, we can create new files with unique filenames and guarantee files are not overwritten.

Comments are closed on this article!