tags:

views:

20

answers:

2

How can I chop out the text, returned by find, after the last "/" ?

$ find home/a/misc/ -type f
home/a/misc/6
home/a/misc/5
home/a/misc/2

I can do it by piping cut -d"/" f4 to the find command but problem is that the folder misc folder is not always there in folder a. It can be in like /home/a/b/c/d/e/. In that case it will return the element in folder c

Edit: 6, 5 and 2 are files, not directories.

Solved

using -printf %f with the find command did the magic. Thanks to all of you

+1  A: 

You can use basename or dirname

Drakosha
+2  A: 

basename will do the trick:

$ basename foo/bar
bar
$ find . -type f
./bar/quux/file
./foo/bar
./foo/baz
$ find . -type f -exec basename {} \;
file
bar
baz
Brian Campbell
actually 6, 5 and 2 are files...not directories
baltusaj
Nah..that gives following errorbasename: extra operand `/home/a/misc/2'
baltusaj
Never mind my previous comment; the problem was probably that I used `xargs` in my example, which works differently on Linux and Mac OS X. With `find -exec` this should work a little better.
Brian Campbell