An icon of a file folder with files coming out of it with text beside that reads, filename length.

Count The Number of Characters per Filename

  • Adam Douglas

There can be many reasons as to why one would want to know the length of a filename or multiple files within a directory. In my case I had no direct need other than my curiosity getting the better of me and wanting to know how I could accomplish this using the command line. Just thinking about this task brings to mind many possible solutions, so if you notice a possible issue or have another way to accomplish the task please feel free to provide some feedback. Now allow me to clarify what I mean by the length of a filename, as it can get confusing. I’m referring to the total amount of characters used for the filename including the filename extension. So for example a filename of “my-filename.txt” would have 15 total characters (length). In my use case I’m not including the full path for the filename length.

There is a lot to unpack here, especially for those dear readers that are not familiar with shell scripting. Do not allow this to stop you from playing around with this. I will do my best to illustrate what each solution does by showing the command example followed by the command output.

Environment

Applicable commands were tested using the following.

  • Bash v5.1.16
  • BusyBox v1.35.0
  • PowerShell v7.3.3

Command Examples

The examples are sorted below by shell with a few variations. All examples achieve the following.

  • Iterate through each file found
  • Counts each character used in the filename
  • Outputs the results with the filename then filename length (total characters)

For the purpose of testing and output examples, the current directory has a group of files and is illustrated below containing 5 regular files and one subdirectory. The subdirectory contains one regular file.

├── a-deep-dive-into-the-wayland-protocol-for-linux.md
├── hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md
├── inodes-and-the-linux-filesystem.md
├── test/
│  └── hello-world.md
├── this is-a test.txt
└── xorg-x11-wayland-linux-display-servers-and-protocols-explained.md

Note

In some example commands, a star or period is used which each represent as a special character. The star in "for file in *" represents all files within the current directory. The period in "find ." represents the current directory.

Bash/BusyBox

Look at all file types within the current directory and then use parameter expansion to count each character (${#file}).

$ for file in *; do echo "$file ${#file}"; done
a-deep-dive-into-the-wayland-protocol-for-linux.md 50
hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md 63
inodes-and-the-linux-filesystem.md 34
test 4
this is-a text.txt 18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md 65

Look at all file types within the current directory and then use wc command to count each character.

$ for file in *; do echo "$file $(echo -n $file | wc -m)"; done
a-deep-dive-into-the-wayland-protocol-for-linux.md 50
hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md 63
inodes-and-the-linux-filesystem.md 34
test 4
this is-a text.txt 18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md 65

Look at all regular files (no directories, symbolic links, etc.) using the find command recursively through the current directory with a max depth (-maxdepth) of 1. Count each filename character without directory path using parameter expansion (${#f}).

While this solution may seem more complex, it is still quite simple and offers more flexible with options to change the depth of recursion (how many subdirectories to go into), filter by file type (-type), and ensures only the filename is counted for characters. All being displayed in a beautiful table with title headings making it clear what each value represents.

$ find . -maxdepth 1 -type f -print0 | while IFS= read -d '' file; do f=$(basename "$file"); printf "%s: %s\n" "$file" "${#f}"; done | column -s : -t -N Filename,Length
Filename                                                             Length
./hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md     63
./test/hello-world.md                                                 14
./inodes-and-the-linux-filesystem.md                                  34
./this is-a text.txt                                                  18
./a-deep-dive-into-the-wayland-protocol-for-linux.md                  50
./xorg-x11-wayland-linux-display-servers-and-protocols-explained.md   65

Fish

Look at all file types within the current directory and then use the string length command to count each character.

$ for file in *; echo "$file $(string length $file)"; end
a-deep-dive-into-the-wayland-protocol-for-linux.md 50
hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md 63
inodes-and-the-linux-filesystem.md 34
test 4
this is-a text.txt 18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md 65

Look at all file types within the current directory and then use wc command to count each character.

$ for file in *; echo "$file $(echo -n $file | wc -m)"; end
a-deep-dive-into-the-wayland-protocol-for-linux.md 50
hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md 63
inodes-and-the-linux-filesystem.md 34
test 4
this is-a text.txt 18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md 65

Look at all regular files (no directories, symbolic links, etc.) using the find command recursively through the current directory with a max depth (-maxdepth) of 1. Count each filename character without directory path using string length command.

While this solution may seem more complex, it is still quite simple and offers more flexible with options to change the depth of recursion (how many subdirectories to go into), filter by file type (-type), and ensures only the filename is counted for characters. All being displayed in a beautiful table with title headings making it clear what each value represents.

$ for file in $(find . -maxdepth 1 -type f); set basename "$(basename $file)"; echo "$file $(string length $basename)"; end | column -t -N Filename,Length
Filename                                                             Length
./hard-links-and-soft-links-in-linux-explained-enable-sysadmin.md    63
./inodes-and-the-linux-filesystem.md                                 34
./this is-a text.txt                                                 18
./a-deep-dive-into-the-wayland-protocol-for-linux.md                 50
./xorg-x11-wayland-linux-display-servers-and-protocols-explained.md  65

PowerShell

Look at all files types within the current directory and then use object length ($_.Name.Length) to count each character.

PS C:\> get-childitem | select-object Name, @{N="Length";E={$_.Name.Length}}
Name                                                              Length
----                                                              ------
test                                                                   4
a-deep-dive-into-the-wayland-protocol-for-linux.md                    50
hard-links-and-soft-links-in-linux-explained.md                       47
inodes-and-the-linux-filesystem.md                                    34
this is-a text.txt                                                    18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md     65

Look at files (not directories) recursively within the current directory and then use object length ($_.Name.Length) to count each character.

PS C:\> get-childitem -File -Recurse | select-object Name, @{N="Length";E={$_.Name.Length}}
Name                                                              Length
----                                                              ------
a-deep-dive-into-the-wayland-protocol-for-linux.md                    50
hard-links-and-soft-links-in-linux-explained.md                       47
inodes-and-the-linux-filesystem.md                                    34
this is-a text.txt                                                    18
xorg-x11-wayland-linux-display-servers-and-protocols-explained.md     65
hello-world.md                                                        14

Tip

To work outside the current directory add the parameter "-Path" and then the desired directory path to "get-childitem" (e.g. "get-childitem -File -Recurse -Path C:\dir\example").