173
find . -type f -print

prints out

./file1
./file2
./file3

Any way to make it print

file1
file2
file3

?

2
  • 1
    if you used -exec /path/to/myscript.sh {} in your find, the -printf '%P\n' does not work. Instead, I have to do the ./ prefix chomping inside the script by: p=${1#"./"}
    – daparic
    Oct 21, 2018 at 3:50
  • If you are doing it just for a single file: f=`find . -name migration`; echo ${f/.\//}
    – masterxilo
    Jan 23, 2019 at 17:48

8 Answers 8

305

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.

10
  • 22
    +1 Good solution if your version of find supports -printf (not all do).
    – Todd Owen
    Jan 9, 2012 at 8:58
  • 2
    As Todd said, but more precisely: it ain't POSIX 7. Feb 16, 2014 at 12:42
  • 9
    Supported or not, it's definitely safer than the accepted answer. Dec 11, 2015 at 18:08
  • 12
    You can use \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
    – seanf
    Jun 2, 2017 at 3:05
  • 1
    @Quanlong , it works on macOS if you brew install findutils ;)
    – blockloop
    Jan 11, 2018 at 22:53
68

Use sed

find . | sed "s|^\./||"
3
  • 4
    This is my preferred solution; it is POSIX-compatible (works practically everywhere, even embedded systems), very short, and unlikely to result in unexpected behavior. This should be the accepted answer. Nov 25, 2016 at 14:39
  • 7
    How would this be combined with find -exec?
    – Paul Wintz
    Jan 18, 2017 at 22:13
  • find -exec sh -c "echo {} | sed 's|^\./||'" \; or find -exec sh -c "sed 's|^\./||' <<< '{}'" \;
    – xenithorb
    Oct 19, 2017 at 13:34
41

If they're only in the current directory

find * -type f -print

Is that what you want?

12
  • 27
    This will miss files with names that start with a period.
    – Sean
    Apr 7, 2010 at 23:58
  • 4
    Bad, depending on a shell settings * may not evaluate to "hidden" files/directories -- those which begins with dot (.) Try to run this command in your home dir, then just "find -type f" and see the difference.
    – Ilia K.
    Apr 8, 2010 at 0:01
  • 44
    A more relevant concern is that '*' is evaluated by the shell. You effectively have a type of malicious code injection situation. It would be trivial to create a file named ' --exec rm ' and another named 'zzzz ;'. Goodbye files.
    – CWF
    Apr 8, 2010 at 0:39
  • 7
    This is unsafe. If a file name starts with a dash, it will be treated as an option to find, and may fail, or could potentially do damage. Dec 11, 2015 at 17:53
  • 4
    Why is -print required? It gave same result without that flag.
    – user13107
    Apr 29, 2016 at 3:20
9

it can be shorter

find * -type f
2
  • Better with 2> /dev/null to ignore error reported when the command is run in an empty directory.
    – neevek
    Dec 17, 2016 at 6:13
  • 6
    Unsafe, just like the accepted answer. * is expanded by the shell, so any filename starting with - will be interpreted as an option to find.
    – jbg
    May 2, 2021 at 11:24
7

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

find -type f | cut -c3-

Further explanation can be found here

4

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' {} + 
2
  • 1
    Unfortunately this strips the whole path of each found file, including intermediate subdirectories, and just prints the filename itself.
    – PLL
    Jun 24, 2020 at 16:42
  • 2
    You are right. This solution is only good for files in current directory. For nested files we need to use: find . -type f -exec bash -c 'for f; do echo "${f#./}"; done' {} +
    – anubhava
    Jun 24, 2020 at 20:43
2

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)
1

Another way of stripping the ./

find * -type d -maxdepth 0
2
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – David Buck
    Mar 18, 2020 at 11:42
  • 2
    Unsafe, just like the accepted answer. * is expanded by the shell, so any filename starting with - will be interpreted as an option to find.
    – jbg
    May 2, 2021 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.