71

I've bunch of folders in folder A. I want to move files from all those folders to A.

Or

I want to cut all files from child folders and paste them in parent folder.

How to do that?

Basharat Sialvi
  • 23,936
  • 8
  • 61
  • 82
Rahul Virpara
  • 11,610
  • 11
  • 38
  • 50

2 Answers2

115

Go to your A directory and run

find . -mindepth 2 -type f -print -exec mv {} . \;

which means "find all files in this directory and its sub-directories and execute mv with target directory . for each file found to move them to current directory.

Tuminoid
  • 3,942
  • 1
  • 21
  • 28
  • 1
    it also tries to move files which are already in parent directory. – Rahul Virpara Jun 05 '12 at 17:57
  • virpara: now this your edition forgets all files in .dot directories. Revised it with -mindepth 2 instead to suppress warnings. – Tuminoid Jun 06 '12 at 07:04
  • 1
    With GNU `find` you can be a little more elegant and not spawn a `mv` process for each file: `find A -mindepth 2 -type f -exec mv -t A \{\} +` – David Foerster Sep 23 '14 at 20:09
  • David, your version works until you hit maximum command line length limit, which isn't that hard to do given how find prints out the paths, especially in case where you invoke it from directory other than A, like in your example. – Tuminoid Sep 24 '14 at 09:24
  • Can someone please explain to me why the `\;` is needed at the end? – Ram Rachum Jul 17 '15 at 07:16
  • thank you so much. This is why i luv linux and the community – Thomas Shera Mar 15 '16 at 11:23
  • 19
    This is very dangerous. if you have files with the same file name in different subfolders, you will overwrite all of these with the last find. Better to use mv with --backup=numbered: `find . -mindepth 2 -type f -print -exec mv --backup=numbered {} . \;` – pLumo May 08 '17 at 09:18
  • This works, but its kind of slow on a windows machine – Vincent Tang Oct 16 '18 at 17:05
  • Thanks! Just what I needed, although I used `--backup=numbered` as suggested by @pLumo. One more suggestion: after having moved all files into the target directory, only empty folders remain. To quickly get rid of them, I used `find . -empty -type d -delete`. – luttztfz May 15 '23 at 13:02
5

Well you could create a file and name it "cutme" (to create a file called cutme in the terminal type nano cutme. To save it press CTRL+X then press ENTER.) for example and paste the following in it assuming that:

  1. You want to do this recursively (In subfolders and subfolders of those subfolders)
  2. You want to skip moving the script file
  3. You have permissions to move the files in that folder
  4. Files may or may not include spaces in their names

find * -type f -print -not -type d -and -not -regex 'cutme' -exec mv {} .. \;

Note the name cutme inside the line. It should be the same as the script you will run.

After creating the file and pasting the above line, run the following in the same folder as the script:

chmod +x cutme. This will give your new file the "Executable" flag so you can execute it like this: ./cutme.

Luis Alvarado
  • 209,003
  • 167
  • 543
  • 707
  • Why should I try to move files those are already in parent directory? – Rahul Virpara Jun 05 '12 at 17:56
  • You would run the script from inside the folder. Every file in it, if inside a subfolder or not would move to the folder's parent folder. The parent folder here is the parent in relation to where the script is. Actually by changing the last 2 dots you can tell where to move all files inside a folder where you run the script. – Luis Alvarado Jun 05 '12 at 18:01