57

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?

  • 1
    I haven't really tested this to be foolproof or anything, but have you tried simply entering this command from Folder A in a terminal: mv */* .? – Marty Fried Jun 5 '12 at 6:06
  • Are you sure there are no duplicate file names? – jippie Jun 5 '12 at 6:46
  • mv */* . won't move files from subfolders' subfolder. – Rahul Virpara Jun 5 '12 at 18:03
  • @jippie yep, there are no duplicate names – Rahul Virpara Jun 5 '12 at 18:03
95

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.

  • 1
    it also tries to move files which are already in parent directory. – Rahul Virpara Jun 5 '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 6 '12 at 7: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 9:24
  • 14
    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 8 '17 at 9:18
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.

  • Why should I try to move files those are already in parent directory? – Rahul Virpara Jun 5 '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 5 '12 at 18:01

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.