开发者

How can I recursively print a list of files with filenames shorter than 25 characters using a one-liner?

I need a one line command for the below requirement.

Search all the files from the root directory and print only those files names whose file name length is less than 25.

I suppose we can do it with find command so开发者_如何学JAVAmething like below:

find / -type f |xargs basename .... I am not sure about furthur command.


My GNU find supports this, unsure whether it's a part of standard find.

find / -type f -regextype posix-extended -regex '.*/.{1,24}$'

Alternatively, use find | grep.

find / -type f | egrep '.*/.{1,24}$'


find / -type f|egrep "/[^/]{0,24}$"

Alternatively if you only want to display the filename without the path:

find / -type f| egrep -o "/[^/]{0,24}$" | cut -c 2-


Using Bash 4+

shopt -s globstar
shopt -s nullglob
for file in **/*
do
   file=${file##*/}
   if (( ${#file} < 25 ));then echo "$file"; fi
done

Ruby(1.9+)

ruby -e 'Dir["**/*"].each {|x| puts x if File.basename(x).size < 25}'


After referring quickly some manuals i got to find awk to be more suitable and easy to understand.please see the below solution which i had came up with.

find / -type f|awk -F'/' '{print $NF}'| awk 'length($0) < 25'

may be there are some syntax errors.please correct me if i am wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜