Search code examples
bashshellshzsh

List all files with length of name between 3 and 6 characters


I have to write a little shell script for class, and since there's only online lectures I do not have the option to ask my professor for help.

The task (should be) is actually very simple:

Write a shell script which lists all files in the current directory, whose name is greater than 3 and smaller than 6 characters

  • 12.txt --> should not be listed

  • 123.txt --> should be not listed

  • 1234567.txt --> should not be listed

  • 12345.txt --> should be listed

  • 123456.txt --> should be listed

The suffix (filename extension) .txt is not given in the question, its just an example by me. The listing of all files should work for any suffix.

I would be happy if anyone could help me understand the correct commands for this, or maybe post a link to a tutorial etc.


Solution

  • shopt -s extglob
    ls ??@(?|??|???|????).* 2>/dev/null