开发者

How to strip leading "./" in unix "find"?

find . -type f -print

prints out

./file1
./file2
./fi开发者_JAVA百科le3

Any way to make it print

file1
file2
file3

?


Find only regular files under current directory, and print them without "./" prefix:

find -type f -printf '%P\n'

From man find, description of -printf format:

%P     File's name with the name of the command line argument under which it was found removed.


Use sed

find . | sed "s|^\./||"


If they're only in the current directory

find * -type f -print

Is that what you want?


it can be shorter

find * -type f


Another way of stripping the ./ is by using cut like:

find -type f | cut -c3-

Further explanation can be found here


Since -printf option is not available on OSX find here is one command that works on OSX find, just in case if someone doesn't want to install gnu find using brew etc:

find . -type f -execdir printf '%s\n' {} + 


Another way of stripping the ./

find * -type d -maxdepth 0


For files in current directory:

find . -maxdepth 1 -type f | xargs basename -a

-maxdepth 1 -> basically this means don't look in subdirectories, just current dir
-type f     -> find only regular files (including hidden ones)
basename    -> strip everything in front of actual file name (remove directory part)
-a          -> 'basename' tool with '-a' option will accept more than one file (batch mode)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜