119

This is probably very simple, but I can't figure it out. I have a directory structure like this (dir2 is inside dir1):

/dir1
    /dir2 
       |
        --- file1
       |
        --- file2

What is the best way to 'flatten' this directory structure in such a way as to get file1 and file2 into dir1 not dir2?

Lucas
  • 119
turtle
  • 2,697
  • 5
  • 20
  • 17

7 Answers7

126

You can do this with GNU find and GNU mv:

find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' +

Basically, the way that works if that find goes through the entire directory tree and for each file (-type f) that is not in the top-level directory (-mindepth 2), it runs a mv to move it to the directory you want (-exec mv … +). The -t argument to mv lets you specify the destination directory first, which is needed because the + form of -exec puts all the source locations at the end of the command. The -i makes mv ask before overwriting any duplicates; you can substitute -f to overwrite them without asking (or -n to not ask or overwrite).

As Stephane Chazelas points out, the above only works with GNU tools (which are standard on Linux, but not most other systems). The following is somewhat slower (because it invokes mv multiple times) but much more universal:

find /dir1 -mindepth 2 -type f -exec mv -i '{}' /dir1 ';'

POSIXly, passing more than one argument, but using sh to reorder the list of arguments for mv so the target directory comes last:

LC_ALL=C find /dir1 -path '/dir1/*/*' -type f -exec sh -c '
  exec mv "$@" /dir1' sh {} +
derobert
  • 109,670
65

In zsh:

mv dir1/*/**/*(.D) dir1

**/ traverses subdirectories recursively. The glob qualifier . matches regular files only, and D ensures that dot files are included (by default, files whose name starts with a . are excluded from wildcard matches). To clean up now-empty directories afterwards, run rmdir dir1/**/*(/Dod)/ restricts to directories, and od orders the matches depth first so as to remove dir1/dir2/dir3 before dir1/dir2.

If the total length of the file names is very large, you may run into a limitation on the command line length. Zsh has builtins for mv and rmdir which are not affected by this limitation: run zmodload zsh/files to enable them.

With only POSIX tools:

find dir1 -type f -exec mv {} dir1 \;
find dir1 -depth -exec rmdir {} \;

or (faster because it doesn't have to run a separate process for each file)

find dir1 -type f -exec sh -c 'mv "$@" dir1' _ {} +
find dir1 -depth -exec rmdir {} +
  • 2
    This should be the accepted answer! Especially with the concise zsh version. – Adamski Sep 06 '17 at 10:18
  • 1
    The zsh option also works in bash if you have shopt -s globstar in your .bashrc. So you can just mv ./**/*.* ./ and then rm -r <directories>. – Jolly Llama Aug 19 '20 at 22:34
  • @JollyLlama No, that doesn't do the same thing. **/*(.D) in zsh only matches regular files. There's no equivalent in bash. **/*.* matches directories as well. And for whatever reason you excluded files with no . in their name, which you delete rather than move. – Gilles 'SO- stop being evil' Aug 20 '20 at 07:53
  • You’re right, I didn’t realize I was making the (false) assumption that we were dealing with user files and folders, so I assumed that files will all be of the form file.ext and folders will all be extensionless. What I did works if those assumptions hold, but not in general. – Jolly Llama Aug 20 '20 at 16:43
10

tar and zip both have the ability to incorporate and then strip away a directory structure, so I was able to quickly flatten a nested directory with

tar -cvf all.tar *

followed by moving all.tar to a new location then

tar -xvf all.tar --strip=4

John
  • 101
  • 1
  • 2
9

Expanding on the popular answer for this question, since I had a use-case for flattening a directory containing files of the same name.

dir1/
├── dir2
│   └── file
└── dir3
    └── file

In this case, the -i (--interactive) option passed to mv wouldn't yield the desired result to flatten the directory structure and handle name conflicts. So it's simply replaced with --backup=t (equivalent to --backup=numbered). More documentation on the -b (--backup) option available at https://www.gnu.org/software/coreutils/manual/coreutils.html#Backup-options.

Resulting in:

find dir1/ -mindepth 2 -type f -exec mv -t dir1/ --backup=t '{}' +

Which yields:

dir1/
├── dir2
├── dir3
├── file
└── file.~1~
Yann Eves
  • 191
  • 1
  • 3
  • 1
    is there anyway to make file~1~.extension, otherwise got file.extension.~1~ when duplicated, so had to rename that manually to be able to preview – angularconsulting.au Apr 08 '22 at 08:14
5

Try doing this :

cp /dir1/dir2/file{1,2} /another/place

or for each files matching file[0-9]* in the subdir :

cp /dir1/dir2/file[0-9]* /another/place

See http://mywiki.wooledge.org/glob

4

Staying in dir1 you can just do it with mv dir2/* .. If you have more subfolders on dir2 level you can use mv */* .

2

I wrote two functions you can use together that do just that, you can limit the directory level by adding a -maxdepth $VAL parameter.

# This scripts flattens the file directory
# Run this script with a folder as parameter:
# $ path/to/script path/to/folder

#!/bin/bash

rmEmptyDirs(){
    local DIR="$1"
    for dir in "$DIR"/*/
    do
        [ -d "${dir}" ] || continue # if not a directory, skip
        dir=${dir%*/}
        if [ "$(ls -A "$dir")" ]; then
            rmEmptyDirs "$dir"
        else
            rmdir "$dir"
        fi
    done
    if [ "$(ls -A "$DIR")" ]; then
        rmEmptyDirs "$DIR"
    fi
}

flattenDir(){
    local DIR="$1"
    find "$DIR" -mindepth 2 -type f -exec mv -i '{}' "$DIR" ';'
}

read -p "Do you wish to flatten folder: ${1}? " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    flattenDir "$1" &
    rmEmptyDirs "$1" &
    echo "Done";
fi
Bruno
  • 121
  • 3
  • 2
    Man, I just misused your script by forgetting the path argument, that just really fucked up my server. Ok, I'm the guy who copy paste things and misuse them, but guys, be wise and add checks / confirmations on scripts that delete / move stuffs like that... – Dulgan Aug 01 '18 at 08:32
  • 1
    Whoops! I am sorry to hear that. Hope you have a backup... I added a confirmation for future protection. – Bruno Aug 02 '18 at 10:34
  • 1
    Thanks @Bruno that's much better this way. My server is still running flawlessly, i commented the "flatten" part to just delete empty directories recursively from (and that was my error) root, until I saw an error that made me stop running the script. – Dulgan Aug 02 '18 at 14:05