How do I return only file names from the find command?

Solution 1:

With basename:

find . -type f -exec basename {} \;

Solution 2:

Evilsoup mentioned that what was posted doesn't work for spaced file names. So instead you could use:

find . -type f -print0 | while IFS= read -r -d '' filename; do echo ${filename##*/}; done

Solution 3:

With GNU find, you can do:

 find ~/tmp/ -printf "%f\n"

This is probably worth trying in OS X too.

Solution 4:

There is a better way to strip everything but the last portion of a file path; with awk. It is better because awk is not executed once for every file. In some cases this matters.

find ~/tmp/ -type f  | awk -F/ '{ print $NF }'

We look only for files in ~/tmp and we get a list where every entry is separated by slashes. Hence, we use a slash as the field separator (-F/) and print the field parameter ($1..$9) that corresponds to the last field ($NF).

Solution 5:

EDIT:

Using sed:

$ find . -type f | sed 's/.*\///'

Using the xargs command, as mentioned in the response of @nerdwaller

$ find . -type f -print0 | xargs --null -n1 basename