0

I'm trying to list all files with 8-character names and with no extension.

My problems begin with files such as: "test.txt", where the "." is in the middle.

I've already tried, doing ls and redirecting the output to a file, then using grep to get the files I need, but I was wondering:

Is this possible to do this in 1 line using ls/grep/redirection/extglobs etc?

1

You can use grep to match for any 8 characters except .:

 $ ls | grep '^[^.]\{8\}$'

Or if piping is a problem:

 $ find . -regex '.*/[^.][^.][^.][^.][^.][^.][^.][^.]'

Or in many shells simply:

 $ ls -d [^.][^.][^.][^.][^.][^.][^.][^.]
  • that involves piping, do you think its impossible without it? – user110210121 Feb 6 '17 at 0:50
  • Piping is pretty common, what is the concern? – Stephen Rauch Feb 6 '17 at 0:51
  • it wasn't taught to my class when this question was asked of us, so I'm wondering if it can be done without it. – user110210121 Feb 6 '17 at 0:52
  • 1
    Shouldn't ls -d [^.][^.][^.][^.][^.][^.][^.][^.] work in most shells? – steeldriver Feb 6 '17 at 1:30
  • @steeldriver, sadly not in the brain dead shell I am currently using, but that is a sad tale for some other time. However, duly noted and edited accordingly. Thanks. – Stephen Rauch Feb 6 '17 at 1:44
0

You can use ? to match any one character in most shells:

$ GLOBIGNORE='*.*' eval "ls -d ????????"

The GLOBIGNORE variable guides the globbing to, in this case, avoid file names with . in them, and the -d flag avoids listing contents when matching to directory names. Use man bash for more details.

  • yes, but that will include "." – user110210121 Feb 6 '17 at 0:55
  • True. You're happy with the pipe answer above, but I'll edit mine for the sake of it. – Ralph Rönnquist Feb 6 '17 at 1:14
0
ls [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9] 

all filenames that are 8 characters long and alphanumeric only (no hyphens, periods, etc)

0

Since parsing ls is generally a bad idea , here's two alternative solutions - one in python as one-liner and another in shell loop:

$ python -c "import os,sys;[sys.stdout.write(f+'\n') for f in os.listdir('.') if len(f) == 8 and '.' not in f]"
TEXTFILO
deadline
FILENAME
diskman2
Pictures
emptydir
ud_indie
strategy
EET 4330

$ for f in * ; do ! [[ $f =~ '.'  ]] && [[ ${#f}  -eq 8  ]] && echo "$f" ; done
deadline
diskman2
EET 4330
emptydir
FILENAME
Pictures
strategy
TEXTFILO
ud_indie

Syntax of both is the same - we itereate over files in current working directory, and using two logic checks for whether there is . in filename and whether filename is equal to 8 characters, determine which filename to print.

Note that the [[ syntax can be used with bash,zsh, and ksh

-1

Thanks for all the input guys, after some hours I figured out what I was trying to do.

$ ls -d !(!(???????)|*(*.*))

your extglob has to be set for this to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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