Bash: How to List All Files in Folder Without Extension


Often you may want to list all files in a folder without their extensions using Bash.

You can use the following syntax to do so:

ls | sed 's/\.[^.]*$//'

This syntax will list all files in the current folder without their extension.

Note that we use the sed s/ command which is used to “substitute” the last dot and everything after the last dot in a filename with nothing.

It’s worth noting that this method will also list any subfolder names.

To list only file names without their extensions, you can use the following syntax:

ls -p | grep -v / | sed 's/\.[^.]*$//'

This method uses the -p command to add a slash to all directory names, then uses grep to filter for only items that do not contain a slash. Lastly, it displays all file names without their extensions.

The following examples show how to use each of these methods in practice with the following directory on our computer:

  • c:/users/bobbi/data1

Let’s jump in!

Example: List All Files in Folder without Extension Using Bash

First, we can use the ls command to simply list all files and folders in the current directory:

ls

The following screenshot shows how to use this syntax in practice:

From the output we can see that the current directory contains the following folders:

  • current_data
  • new_data
  • old_data

The current directory also contains the following files:

  • coaches.txt
  • conference.txt
  • players.txt
  • teams.txt

Suppose we use the following syntax to display all file names without their extensions:

ls | sed 's/\.[^.]*$//'

The following screenshot shows how to use this syntax in practice:

The output displays all file names without their extensions but it also shows the names of all folders in the directory.

We can use the following syntax to filter for files only and display each file name without the extension:

ls -p | grep -v / | sed 's/\.[^.]*$//'

The following screenshot shows how to use this syntax in practice:

Bash list all files without extensions

Notice that the output now shows all file names without their extensions.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Size
Bash: How to Delete All Files in Directory
Bash: How to Delete All Empty Lines in File

Leave a Reply