Views:
5,782β
Votes: 6β
Tags:
bash
scripts
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1051609
Title:
Running a Bash while loop over all similar files
ID:
/2018/07/02/Running-a-Bash-while-loop-over-all-similar-files
Created:
July 2, 2018
Edited: July 3, 2018
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
From this Stack Overflow answer: List files that only have number in names:
find . -regex '.*/[0-9]+\.tst'
OR
Using find also has advantages when you want to do something with the files, e.g. using the built-in -exec
, -print0
and pipe to xargs -0
or even (using Bash):
while IFS='' read -r -d '' file
do
# ...
done < <(find . -regex '.*/[0-9]+\.tst' -print0)
Note the other answers here my include files that arenβt numbers if the filename starts with a digit. The answer posted here does not though. For example:
$ ls *.tst
12tst.tst 1.tst 2.tst
$ find . -maxdepth 1 -regex '.*/[0-9]+\.tst'
./1.tst
./2.tst
NOTE: Use -maxdepth 1
argument to only list numbered files in the current directory and not in sub-directories.