rabidfurball rabidfurball - 7 months ago 66
Bash Question

Move all files at one level up one level

I had a problem with a backup on a linux server that somehow moved all folders of files down one level into a new folder with the same name. I would like to return all files to their original location. Many of the files have spaces in their names to complicate things.

Original directory structure:

backup/folder 1/file 1
backup/folder 1/file 2
backup/folder 1/subfolder/file A
backup/folder 2/file 1

Incorrect directory structure:

backup/folder 1/folder 1/file 1
backup/folder 1/folder 1/file 2
backup/folder 1/folder 1/subfolder/file A
backup/folder 2/folder 2/file 1

Note that I do not want the file inside the subfolder to move into folder 1, but I do want the subfolder itself to move up one level.

I've tried a few commands similar to this, based on somewhat related threads on here, but haven't gotten it to work, it instead tries to move the files to the current directory. What am I doing wrong?

find . -mindepth 3 -maxdepth 3 -exec sh -c 'mv -v {} ..' \;


You can adapt these lines :

for file in *
    if [ -d "$file" ]; then
    cd "$file"
    if [ -d "$file" ]; then
        cd "$file"
        mv * ..
        cd ..
        #rm -r "$file"
    cd ..

The idea is simply to detect, at top level, if a directory and if a subdirectory have the same name and then move all files in the parent directory.