0

I want to list files that contain only seven characters (without extensions) in their filename.

I have these files in a directory, for example:

1234567
bar.12
dog.cat
1234567.txt
tst.new
big.tst
abcdefg
abcd.efg

I want to list files using the command ls to find file names without extensions that have exactly 7 characters. I want the output to look something like this:

$ ls <some kind of code>
1234567
abcdefg

I prefer not to use any globs of any sort, or the find command.

5
  • 1
    you want to not use find, and not use wildcards? o.O
    – Zanna
    Apr 17, 2017 at 19:08
  • yeah...pretty much. is that possible? @Zanna
    – Yooso Lee
    Apr 17, 2017 at 19:11
  • I very much doubt it...
    – Zanna
    Apr 17, 2017 at 19:15
  • Then what would be the answer with globs and the find command included? @Zanna
    – Yooso Lee
    Apr 17, 2017 at 19:17
  • Sorry I'm new to using unix and bash... My professor usually restricts using globs on the exams but thank you so much for the help! :) @Zanna
    – Yooso Lee
    Apr 17, 2017 at 19:19

3 Answers 3

2

Here's a sort-of solution with shell wildcards (exactly 7 characters that are not .)

ls [!.][!.][!.][!.][!.][!.][!.]

And here's a solution with find using regex negation (not literal . = [^\.]) and specifying how many characters {7}

find -maxdepth 1 -regextype posix-extended -regex '\./[^.]{7}'

Not what you asked for, but I doubt it can be done without some kind of wildcards or regex.

4
  • the first solution is actually perfect! Thank you so much for your help! :)
    – Yooso Lee
    Apr 17, 2017 at 20:11
  • Always a pleasure @YoosoLee :)
    – Zanna
    Apr 17, 2017 at 20:25
  • 1
    find ... -name "???????" ! -name "*.*" should also do
    – ilkkachu
    Apr 17, 2017 at 20:58
  • @ilkkachu nice one - you could post it as an answer, if you want to
    – Zanna
    Apr 17, 2017 at 20:59
1

In Bash:

 for f in ??????? ; do 
     [[ $f = *.* ]] && continue; 
     echo $f ;                     # do something useful here
 done

With find:

find -maxdepth 1 -name "???????" ! -name "*.*"

Though since you asked for it without globs or find and used the word "professor", they maybe looking for ls | grep .... But see also a couple of reasons to avoid using ls for this.

1

With extended globs in zsh:

% ls
1234567  1234567.txt  abcdefg  abcd.efg  bar.12  big.tst  dog.cat  tst.new

then

% setopt extended_glob
% ls (?~.)(#c7)
1234567  abcdefg
  • ?~. any character except .
  • (#c7) a globbing flag equivalent to regular expression quantifier {7}

See zsh: 14 Expansion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.