Using variable in ls command

I am in /home/aaa path and trying to list all the files in different directory - the path to this directory is lengthy. For example: aa/bb/cc/dd/ee/ff/gg/hh/ii/jj... and also the path will change dynamically.

HEnce I capture the directory path in a variable. How to use this variable in ls command.

The variable is $dir_name

I currently have ls $dir_name out_* | wc -l

To list all files starting with "out_" from directory in variable "$dir_name".

This is not working. Please advise.

Thanks!

How about posting the error you get, and how you declared that variable?
"It's not working" doesn't really help.

I will tell you though, if the path is "changing dynamically" your variable is not going to mean anything once the path changes.

I will enter the directory path and capture it in a variable. When I execute this command, i get the message " ls $dir_name out_* | wc -l" command not found.

Thanks1

mydir=$(find /home/aaa -type d)
for i in $mydir;do ls $i;done
ls $dir_name/out_*

Thanks danmero!

But the directory path that I want to list is in the root.
I am currently in /home/aaa
the files that want to list is in /aa/bb/cc/dd/ee/ff/gg....
any help?
Thanks!

This works, no need to know your location.

dirname="/aa/bb/cc/dd/ee/ff/gg"
ls $dirname/out_* | wc -l

I tried that, but got an error "argument list too long".
any other option?

find "$dir_name" -name 'out_*'

Thanks much! This works fine.

But the result shows 1 count more than the actual. If I navigate to that directory and use "ls" command, it gives 1556, but find gives 1557.

any idea?

Thanks again!

find is always recursive.
observe the output carefully and check if it matches any subdir.