I'd like to use find to list all files and directories recursively in a given root for a cpio operation. However, I don't want the root directory itself to appear in the paths. For example, I currently get:

$ find diskimg

But, I'd like to get


(note the root is also not in my desired output, but that's easy to get rid of with tail).

I'm on OS X, and I'd prefer not to install any extra tools (e.g. GNU find) if possible, since I'd like to share the script I'm writing with other OS X users.

I'm aware this can be done with cut to cut the root listing off, but that seems like a suboptimal solution. Is there a better solution available?

  • find diskimg/* | cut -d '/' -f2- Commented Jan 2, 2017 at 16:54
  • cut -c9- should be faster
    – milahu
    Commented Mar 15, 2022 at 16:11

4 Answers 4


cd into the directory first:

cd diskimg && find . 

On completion, you will be back in your root directory.

Your files will be prepended with ./ in this case; the only way I see around that would be using cut:

{ cd diskimg && find .; } | tail -n +2 | cut -c 3-

Use a subshell to avoid changing your shell's current directory (this isn't necessary if you're piping the output as the left-hand side of a pipe already runs in a subshell).

(cd diskimg && find .)
  • 11
    to be back in the original dir on completion: add parenthesis: (cd diskimg && find . ; ) . That way the cd diskimg && find . is done in a subshell : when that subshell exits you are back to your own shell, in whatever directory you were. Commented Dec 12, 2013 at 9:45
  • 5
    find * is another way around the useless prefix Commented Jan 2, 2017 at 16:45

Another, more complex but only using find approach from my other answer:

find diskimg -mindepth 1 -printf '%P\n'
  • Not all versions of find support printf.
    – jordanm
    Commented Dec 12, 2013 at 4:51
  • 1
    Surely; My OSX doesn't, it seems. Partly why I offered another solution.
    – Stephan
    Commented Dec 12, 2013 at 5:06
  • 2
    @Stephan - next time you can just combine these types of answers into a single one. No need to make multiple answers for variances such as this.
    – slm
    Commented Dec 12, 2013 at 6:59
  • 3
    Only GNU find supports -printf, while many BSD finds have copied some of the features of GNU find, none has copied -printf yet (AFAIK). Commented Dec 12, 2013 at 13:04
  • 1
    the %P for the relative path was what i needed.
    – Fl0v0
    Commented Mar 5, 2019 at 13:02

Use the realpath utility:

find diskimg -exec realpath --relative-to diskimg {} \;
  • Neat - thanks for showing me --relative-to.
    – nneonneo
    Commented Jan 1, 2019 at 0:43
  • 1
    unknown predicate --relative-to in CentOS 7.8
    – Winand
    Commented May 18, 2022 at 13:35
  • if your files are symlinks this might not give you what you want
    – jan-glx
    Commented Jun 5, 2023 at 16:08

If what you are trying to do is not too complex, you could accomplish this with sed:

find diskimg | sed -n 's|^diskimg/||p'

Or cut:

find diskimg | cut -sd / -f 2-
  • you can choose (almost) any separator for the command, so: find diskimg | grep -v '^diskimg$' | sed -e 's|^diskimg/||' (note I added also '-e', and a ^ as good practice (not necessary here, as you are sure every lines will have this... but still good practice, and saves some cpu ;)) Commented Dec 12, 2013 at 9:38
  • another way without grep: find diskimg | sed -e 's|^diskimg/*||' (the '/*' will match any number of '/', even 0) Commented Dec 12, 2013 at 9:48
  • This cut command is cleaner than the one I used; I like it. (The sed solution is nice too, but the repetition of diskimg/ is a little unfortunate).
    – nneonneo
    Commented Dec 12, 2013 at 20:12
  • cut -c9- should be faster. the prefix is constant
    – milahu
    Commented Mar 15, 2022 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .