-2

I have a folder named folder1. It has a bunch of files and two subfolders subfolder1 and subfolder2. I want to move all the files in folder1 to subfolder2 except the subfolder1.

How can I do this?

Andy Dalton
  • 13,654
  • 1
  • 25
  • 45
physu
  • 199
  • 1
  • 10

3 Answers3

1

You can use find and mv:

Here's my folder setup:

$ find folder1
folder1
folder1/subfolder2
folder1/subfolder2/i
folder1/subfolder2/h
folder1/subfolder2/g
folder1/subfolder1
folder1/subfolder1/f
folder1/subfolder1/e
folder1/subfolder1/d
folder1/c
folder1/b
folder1/a

To model your case, I want to move a, b, and c to subfolder2:

$ find folder1 -maxdepth 1 -type f -exec mv "{}" folder1/subfolder2 \;

Now if I look at the content of folder1:

$ find folder1
folder1
folder1/subfolder2
folder1/subfolder2/a
folder1/subfolder2/b
folder1/subfolder2/c
folder1/subfolder2/i
folder1/subfolder2/h
folder1/subfolder2/g
folder1/subfolder1
folder1/subfolder1/f
folder1/subfolder1/e
folder1/subfolder1/d
Andy Dalton
  • 13,654
  • 1
  • 25
  • 45
1

To move the non-directory files in folder1 (hidden ones excluded) to folder1/subfolder2, in the zsh shell, you'd do:

mv folder1/*(^/) folder1/subfolder2/

To move all files regardless of their type except subfolder1 (and obviously subfolder2 as well):

set -o extendedglob # best in ~/.zshrc
mv folder1/^(subfolder1|subfolder2) folder1/subfolder2

To also move hidden files, add the D glob qualifier.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
-1

Use find and xargs. Read man find xargs. Since you didn't say what the filenames look like (embedded spaces, other funny characters ), I'll use -print0.

find folder1 -maxdepth 1 -type f -print0 | \
    xargs -0 -r mv --target-directory=subfolder2
waltinator
  • 4,439
  • 1
  • 16
  • 21