find . -type f -print
prints out
./file1
./file2
./file3
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.
find supports -printf (not all do).
Jan 9, 2012 at 8:58
\0 instead of \n if you want to pipe it to xargs -0 (eg in case filenames contain whitespace), eg find -type f -printf '%P\0' | xargs -0 head
Use sed
find . | sed "s|^\./||"
find -exec sh -c "echo {} | sed 's|^\./||'" \; or find -exec sh -c "sed 's|^\./||' <<< '{}'" \;
Oct 19, 2017 at 13:34
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' {} +
find . -type f -exec bash -c 'for f; do echo "${f#./}"; done' {} +
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)
Another way of stripping the ./
find * -type d -maxdepth 0
* is expanded by the shell, so any filename starting with - will be interpreted as an option to find.
-exec /path/to/myscript.sh {}in yourfind, the-printf '%P\n'does not work. Instead, I have to do the./prefix chomping inside the script by:p=${1#"./"}f=`find . -name migration`; echo ${f/.\//}