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- Jan 2, 2017 at 16:54
  • cut -c9- should be faster
    – milahu
    Mar 15 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 .)
  • 8
    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. Dec 12, 2013 at 9:45
  • 2
    find * is another way around the useless prefix 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
    Dec 12, 2013 at 4:51
  • 1
    Surely; My OSX doesn't, it seems. Partly why I offered another solution.
    – Stephan
    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
    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). Dec 12, 2013 at 13:04
  • the %P for the relative path was what i needed.
    – Fl0v0
    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
    Jan 1, 2019 at 0:43
  • unknown predicate --relative-to in CentOS 7.8
    – Winand
    May 18 at 13:35

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 ;)) Dec 12, 2013 at 9:38
  • another way without grep: find diskimg | sed -e 's|^diskimg/*||' (the '/*' will match any number of '/', even 0) 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
    Dec 12, 2013 at 20:12
  • cut -c9- should be faster. the prefix is constant
    – milahu
    Mar 15 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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