Home > Back-end >  Glob to files with suffixes within a numeric range
Glob to files with suffixes within a numeric range

Time:06-24

I have a list of files with the following names:

document1
document2
document3
..
document41
document42
document43

To check all the all document space, I could use du -sh file*. I am trying to access documents from document1 to document 10. I tried the following:

du -sh document[1-10]

But it returns document 28 and document 15. I would like to know how could I access a range of files with document name from document1 to document10?

CodePudding user response:

In bash:

shopt -s nullglob
du -sh document[1-9] document1[0]

Only existing files numbered 1-10 will be passed to du.

Regular sh doesn't have nullglob. If a pattern doesn't match, it's passed literally. This causes either a file not found error, or importantly, a non matching file to be passed (ie. an existing file literally named document1[0]).

CodePudding user response:

If dan's solution is too much to type or remember, you could use bash's brace expansion feature to create the ten filenames, then drop du's potential error messages about missing files by redirecting stderr to /dev/null:

du -sk document{1..10} 2>/dev/null
  • Related