3

Here I got result of 1 instead two files that should have been found:

mkdir -p mytestdir001
mkdir -p mytestdir002
LIST=`find -maxdepth 1 -name "mytestdir???"`
echo ${#LIST[@]}
1
  • Maybe you need to use a dot to say where to start find . -maxdepth ... Commented Oct 22, 2015 at 8:14

5 Answers 5

2

Make the LIST an array rather than a variable:

LIST=( `find -maxdepth 1 -name "mytestdir???"` )

Also start using $() instead of older ``:

LIST=( $(find -maxdepth 1 -name "mytestdir???") )
Sign up to request clarification or add additional context in comments.

Comments

2

you could use wordcount ( wc ) :

find -maxdepth 1 -name "mytestdir???" | wc

this will give you output like this:

2       2      30

these are:

  • number of lines
  • number of words
  • number of characters

use wc -l to get only the number of lines.

Comments

0

Why don't you use:

find -maxdepth 1 -name "mytestdir???" | wc

if your real goal is to know the number?

Comments

0

You could pipe the output of the find into a wc and count the lines, given each file is represented by a distinct line - which in my experience is the case with find.

http://linux.die.net/man/1/wc

example

[user@machine find]# find . -name "*.file"

./3.file

./1.file

./2.file

[user@machine find]# find . -name "*.file" | wc -l

3

Comments

0
ls | awk '/^mytestdir/{a++}END{print a}' # 2

Breakdown:

ls | awk '
  # Match every folder / file starting with mytestdir
  /^mytestdir/{a++}
  # Print counted matches
  END{print a}' # 2

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.