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

二次信任 提交于 2019-12-25 03:11:48

问题


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 something like below:

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


回答1:


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}$'



回答2:


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-



回答3:


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}'



回答4:


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.



来源:https://stackoverflow.com/questions/5050088/how-can-i-recursively-print-a-list-of-files-with-filenames-shorter-than-25-chara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!