1. Introduction

File management is one of the core aspects of Linux systems that involves the creation, organization, storage, retrieval, and maintenance of files and directories. There are various scenarios when we may need to find specific files. In these situations, Linux provides powerful and versatile commands to efficiently search through file systems.

In this tutorial, we’ll learn how to find files without an extension.

The code in this tutorial underwent testing on a Debian 12 (Bookworm) system using GNU Bash 5.1.16.

2. Sample Dataset and Toolset

First, let’s ensure we have all the prerequisites ready, including files without an extension.

$ mkdir dir1 && touch dir1/{file1,file2.txt,file3,file4.csv,file5}

The mkdir command creates a directory dir1, while the touch command uses brace expansion to create five empty files within that directory.

Similarly, let’s create a subdirectory with a few files within the dir1 directory to check the depth of the commands used in this tutorial.

$ mkdir dir1/subdir1 && touch dir1/subdir1/{subfile1.csv,subfile2,subfile3.txt,subfile4}

This command creates a directory named subdir1 within the dir1 directory with four empty files.

A filename generally consists of a basename and a file extension, separated by a dot. For example, in the filename document.txt, document is the basename and txt is the file extension. To find a file without an extension, we can search for the files without a dot in their name. Certain files may have dots in their name and still lack an extension, such as .gitignore or my.file.dot.here, but we will not cover such filenames in this tutorial.

We can find the files without an extension in a given directory using find, locate, and with the combination of ls and grep.

3. Using the find Command

The find command provides several options for finding files and directories on a disk recursively. We can use the -name option to search for files without an extension:

$ find dir1/ -type f ! -name '*.*'
dir1/file1
dir1/subdir1/subfile2
dir1/subdir1/subfile4
dir1/file5
dir1/file3

This command finds all the files within the dir1/ directory that don’t have a dot in their name, i.e., files without an extension.

The find dir1/ construct searches for files within the dir1/ directory, having properties defined by the following parameters:

  • -type f option sets the file type to be searched as regular files
  • ! (negation operator) negates the following expression
  • -name ‘*.*’ option specifies the pattern to match filenames, in this case, files with a dot in their name

Thus, the ! -name ‘*.*’ construct finds files without a dot in their name.

Additionally, we can also use regular expressions to specify the filename pattern with the -regex option:

$ find dir1/ -type f -regex '^[^.]*$'

This command uses a regular expression pattern to match filenames without a dot in their name, i.e., files without an extension.

4. Using the locate Command

We can use the locate command to find the location of files that match specific criteria in a database generated by updatedb.

4.1. Installation and Creation of Index Database

Before we dive into the details, let’s verify that we have the locate command installed on our system:

$ locate --version
plocate 1.1.15
Copyright 2020 Steinar H. Gunderson
License GPLv2+: GNU GPL version 2 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

In case it’s not already available, we can install the locate command from the local manager.

$ sudo apt install mlocate

Additionally, the locate command requires the database containing indexes of files and directories before it can be used. Let’s build the database using updatedb:

$ sudo updatedb

This command updates the database file with a snapshot of the file system.

The configuration file for updatedb, i.e., updatedb.conf, is generally stored in the /etc directory. This updatedb.conf file has several parameters like PRUNEPATHS, PRUNEFS, and PRUNENAMES. The paths included in PRUNEPATHS will not be scanned by the updatedb. Thus, we may need to check and update the /etc/updatedb.conf file if any path is not included in the search.

The updatedb command is typically scheduled to run daily by cron to update the index database. However, if files are modified, moved, or created after the scheduled update, the index database won’t reflect these changes. Thus, it is advisable to update the index database using updatedb immediately before the use of locate.

4.2. Index-Based Searching

The locate command uses the index database to search for files and paths, resulting in the search of the entire system. Thus, it is faster than the recursive searching alternatives like find.

Let’s find the files without an extension using the locate command:

$ locate -w /home/user/Baeldung/dir1/ --regex '^.*/[^.]*$' 

The above command searches for files and directories whose basename does not contain any dots.

The -w option searches the entire path name. The –regex option interprets the specified regular expression patterns, in this case, files without any dots in their names.

Furthermore, we can use the extended arguments (xargs) to filter out any directories from the output, keeping only regular files:

$ locate -w /home/user/Baeldung/dir1/ --regex '^.*/[^.]*$' | xargs -I {} bash -c 'test -f {} && echo {}'
/home/user/Baeldung/dir1/file1
/home/user/Baeldung/dir1/file3
/home/user/Baeldung/dir1/file5
/home/user/Baeldung/dir1/subdir1/subfile2
/home/user/Baeldung/dir1/subdir1/subfile4

This command outputs only the files without a dot in their name, i.e., files without an extension.

Let’s understand the new parameters used in this command:

  • xargs takes each space-separated item from the input and passes it to the following command
  • -I {} sets the placeholder ({}) for the input from xargs
  • bash -c construct invokes a new instance of bash shell to execute the following script enclosed in a single quote
  • test -f {} && echo {}’ is a bash script executed for each item passed by xargs and echoes it if the item is a regular file

Moreover, we can also limit the number of matches using the -l (–limit) option.

5. Using the Combination of ls and grep

We can also find the files without an extension using the combination of ls and grep.

$ ls -RF dir1/ | grep -v "\."

In this command, we perform a recursive listing (-R) of the dir1/ directory and then use grep to filter the output, displaying only files and directories without a dot in their name.

Let’s break down this command to understand each option it employs:

  • ls -R construct lists the directory content at a specified path recursively, in this case, dir1/
  • -F appends certain characters to each entry to indicate its types
  • | (pipe) takes the output of the preceding command and passes it as input to the following command
  • grep filters the output based on the specified pattern
  • -v option selects only the non-matching lines
  • “\.” specifies the pattern to match as a dot (.)

The above command also lists the directories without a dot in their name. We can exclude directories from the output by adding one more pattern with the -e option:

$ ls -RF dir1/ | grep -v -e "\." -e "/$"
dir1/:
file1
file3
file5

dir1/subdir1:
subfile2
subfile4

This command outputs only the files without having a dot in their name from the specified directory, i.e., dir1/.

With the -e option, we can use regular expressions to match certain patterns. The -e “/$” matches the forward slash at the end of the line, filtering out the directories.

6. Conclusion

In this article, we have learned different ways to find files without an extension.

First, we discussed the recursive methods to search for files without extensions using find and the combination of ls and grep. Then, we practiced index-based searching with locate after creating the database with updatedb. locate leverages an index-based search, making it faster than the recursive methods.

Among these options, find is the simplest and most self-contained way to find files without an extension. Thus, it is the most recommended way to find files without an extension.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments