I have a tree of directories, I need to remove some of them and move all files from these directories one level up.

I know how to do it step by step, but there are thousands of them, so I need to optimize the process.

I look for directories find . -type d -name 'src' and then I need to move all files from found directories one level up (../).

- one
-- two
--- src
- three
-- four
--- five
---- src

contents of first src should be moved to two, second src to five

How can I do it with one script?

3 Answers 3


Something like:

find . -type d -name src -exec sh -c 'cd "$1"; for f in *; do mv -i "$f" .. ; done' find-sh {} \;

Note the -i flag to mv. If it asks you for confirmation about overwriting something, you should say "no" unless you're expecting it.

Since you said you also want to remove the src directories afterward, use:

find . -type d -name src -exec sh -c 'cd "$1"; for f in *; do mv -i "$f" .. ; done' find-sh {} \; -exec rmdir {} \;

Make a backup first.


Assuming there is no nested src as its sub-directories in each level and the src is the latest directory level, then below should do the job.

find . -path '*/src/*' -type f -execdir echo mv -t ../ {} +

The safest way I know is to:

  1. cd "where your directories are"
  2. Now use tar to save them somewhere: tar -cvf "file name to save to such as /tmp/mysavfil.tar" "name of all the directories such as: foo bar and so on" so you get: tar -cvf /tmp/mysavfil.tar foo bar
  3. Then do: cd ..
  4. Then: tar xvf /tmp/mysavfil.tar
  5. Optionally if you wish delete the no longer needed files as follows: cd "the original place where your files were"; rm -rf foo bar; rm /tmp/mysavefiltar
  6. You are done

A much simpler way is to do:

  • mv foo bar ../.

You must log in to answer this question.