1. Introduction

Finding files based on their filename length in Linux is a common task for administrators, developers, and other users. In many cases, it’s useful to locate files with a specific length, for example, identifying files with very short or long filenames.

In this tutorial, we’ll explore different methods using various built-in commands to find files based on their filename length.

2. Using find

The find command is a powerful Linux tool for finding files and directories and performing subsequent operations on them. It searches for files based on various criteria, including filename length.

To find files based on their filename length, we can use the -name option to specify the pattern to match. We’ll also use the -printf option to print out the desired information about the files.

Let’s first use the Linux tree utility to inspect our current directory structure:

$ tree
.
├── ba.txt
├── file_1.txt
├── filethirteen.txt
├── filethree.txt
├── filetwo.txt
└── t.txt

0 directories, 6 files

The tree command displays all directory hierarchies and keeps an accurate count of all files and directories.

Within our directory structure, we have six different files, each varying in filename length.

Let’s use find to search for files with a filename length of 11 characters:

$ find -name "???????????" -printf "%h/%f\n"
./filetwo.txt

We’re using the -name option to specifically match filenames with a length of 11 characters. This is represented by the 11 question marks that precede it.

The -printf option specifies the filename and directory to print using the %h/%f format specifiers. The \n option creates a new line after every output for easier reading.

3. Using ls and awk

We can also use the combination of the ls and awk commands to find files with a specific filename length.

ls lists all files within a specific directory, while awk manipulates data and generates reports on that data.

We can use find to list all files in our directory, then combine it with awk to extract information about files with a specific filename length.

Let’s search for files with a filename length of six characters:

$ ls -1 | awk 'length($0) == 6 { print $0 }'
ba.txt

We’re using ls -1 to list all the files in the current directory one per line, contrary to the default, which lists the files in a straight line. We then pipe the output to awk, which uses the length function to calculate the length of each filename.

We also have a conditional statement to filter the filenames with a length of six characters. The $0 variable represents the current line being processed.

4. Using grep

The grep command is a powerful tool for searching text and strings in a given file or directory. It uses patterns called regular expressions to search for specific text patterns.

It’s one of the most useful commands on Linux and Unix-like systems for developers and sysadmins.

Let’s use grep to find all filenames with a length of 10 characters:

$ find . -type f | grep -P '/.{10}$'
./file_1.txt

The -type f option limits find to only search for files. The -P option after grep activates Perl Compatible Regular Expressions, which processes the expression that precedes it. The at the end matches the end of the string.

Alternatively, we can also simplify the command:

$ find . -type f | grep -P '/.....$'
./t.txt

We’re replacing the curly braces and variable here with five dots, which representb a filename with five characters. However, this method may be inefficient for relatively large filename lengths.

We can also ignore the filename extension and only match the exact filename:

$ find . -type f | grep -P '/.{2}\.'
./ba.txt

5. Using Shell Globbing

By default, Bash doesn’t support native regular expressions like other standard programming languages. The Bash shell has a different feature known as globbing for matching or expanding specific patterns.

We can use globbing to search for content in a file or match filename patterns. It uses wildcard characters to create the patterns. The question mark  “?” is one of the most commonly used wildcard characters for creating patterns.

Let’s use the echo command in combination with a globbing pattern to output files with only one character on their filename:

$ echo ?.txt
t.txt

We’re specifying the file extension to only search for .txt files. But this method only searches for files in the topmost directory.

We can combine globbing with find to search recursively through all directories and subdirectories:

$ find . -name '??.txt'
./ba.txt

Here, we’re searching for all filenames with only two characters but with a .txt extension.

6. Conclusion

In this article, we’ve explored different methods to search for files from the filename length. The find command is the most common for this task. However, we can use it in combination with other commands for more specific results. We can also search for the filename lengths while excluding or specifying the file extension for more accurate results.

These methods can be useful for moments when we want to search for relatively long or short filenames.

Comments are closed on this article!