Linux: find file names with 4 or 5 characters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Linux: find file names with 4 or 5 characters



How can I find file names consisting of either 4 or 5 characters?



For file names with 4 characters, I can use find . -name ????.tgz, but how to I expand this to length either 4 or 5?


find . -name ????.tgz





If you can use find to get all files with 4 character long names, and use it to get all files with 5 character long names... well... it does have an or operator that would let you get both.
– Shawn
Aug 10 at 5:37





Is there a better way of doing than using two finds with an operator in between?
– OpenSourceEnthusiast
Aug 10 at 5:39





It only takes one find invocation...
– Shawn
Aug 10 at 5:42





Please avoid "Give me the codez" questions. Instead show the script you are working on and state where the problem is. Also see How much research effort is expected of Stack Overflow users?
– jww
Aug 10 at 7:23





@jww You didn't read the question carefully. OP did write a find command (it's just not formatted correctly).
– oliv
Aug 10 at 7:41


find




3 Answers
3



Here is one solution:


find . ( -name "????.cpp" -o -name "?????.cpp" )



-o is for logical OR


-o



just replace .cpp with .tgz or whatever you want. There is also this regex version that would do the same thing:


.cpp


.tgz


find . -regextype posix-egrep -regex '^./[a-zA-Z]4,5.cpp$'



in regex ^ is start symbol ^./ means starts with ./. [a-zA-Z]4,5 means followed by 4 to 5 characters, . means . where is escape character .cpp$ means ends with .cpp


^


^./


./


[a-zA-Z]4,5


.



.cpp$


.cpp



If file name contains numbers instead of [a-zA-Z] do [a-zA-Z0-9]. So it will look like this:


[a-zA-Z]


[a-zA-Z0-9]


find . -regextype posix-egrep -regex '^./[a-zA-Z0-9]4,5.cpp$'





This will match files like foo.f.bar
– oliv
Aug 10 at 6:09


foo.f.bar





@oliv it does not batch foo.f.bar, but there is a mistake :(. It matches ./dddd/cpp :(
– Gox
Aug 10 at 6:22



./dddd/cpp


shopt -s extglob globstar
printf '%sn' **/?????(?).tgz


extglob


globstar


**


?


?(pattern-list)



Or simply:


printf '%sn' **/????.tgz **/?????.tgz



You could use this find command:


find


find -type f -regextype egrep -regex ".*/[^./]4,5.[^./]+$"



The regular expression is set catch any basename file with 4 or 5 characters.
Note this regex applies on the full name, including path.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS