Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 down vote accepted

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-
share|improve this answer
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 ;)) – Olivier Dulac Dec 12 '13 at 9:38
another way without grep: find diskimg | sed -e 's|^diskimg/*||' (the '/*' will match any number of '/', even 0) – Olivier Dulac Dec 12 '13 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 '13 at 20:12

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 .)
share|improve this answer
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. – Olivier Dulac Dec 12 '13 at 9:45

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

find diskimg -mindepth 1 -printf '%P\n'
share|improve this answer
Not all versions of find support printf. – jordanm Dec 12 '13 at 4:51
Surely; My OSX doesn't, it seems. Partly why I offered another solution. – Stephan Dec 12 '13 at 5:06
@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 '13 at 6:59
Only GNU find supports -printf, while many BSD finds have copied some of the features of GNU find, none has copied -printf yet (AFAIK). – Stéphane Chazelas Dec 12 '13 at 13:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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