I have an array of file names like below, I want to find and learn the sizes in the folder. I wrote the bash script below but it is finding all the files in the folder recursively.

files=( a/ddd/dd ddd/b dfgf/fgdfg/c dfgdfg/dfgdfg/d dsf/e rret/ertert/erter/f)

for u in "${files[@]}"  
    find . "[^\/]+$" $u".js" -exec du -sh {} \;
  • 4
    Can you please edit your question to clarify what you are trying to do? Is dfgf/fgdfg/c.js a file whose size you want to know, or is dfgf/fgdfg/c a directory that contains *.js files whose size you want to know? – steeldriver Jan 31 '19 at 13:24

From man find

-maxdepth levels
          Descend  at  most  levels  (a  non-negative  integer)  levels  of directories below the starting-points.
          -maxdepth 0
           means only apply the tests and actions to the starting-points themselves.


find . -maxdepth 0 -name "[^\/]+$" $u".js" -exec du -sh {} \;

I rarely use find with -exec (because I always forget about it) so I apologize if this is wrong

| improve this answer | |
  • 1
    Please finish your answer by including the entire find command. Even though the question is unclear, you can re-edit your answer if the question is edited. – karel Jan 31 '19 at 13:44
  • The -exec part look fine to me, but I'm having a hard time understanding what -name "[^\/]+$" $u".js" is intended to do here. AFAIK -name takes a single simple shell glob (not a regex) as argument, and matches against only the filename portion (so a -name pattern can't contain slashes) – steeldriver Jan 31 '19 at 15:30
  • it might be the I interpreted the man page wrong :/ The metacharacters (``*', ``?', and ``[]') match a ``.' at the start of the base name – j-money Jan 31 '19 at 15:33

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.