6

I have a folder containing files and folders.

folder/file1.jpg
folder/file2.jpg
folder/file3.jpg
folder/subfolder1/file1.txt
folder/subfolder1/file2.txt
folder/subfolder2/file1.txt
folder/subfolder3/
destination/

I want to move all folders (and their content) in a new folder, but not the files.

Eg.

folder/file1.jpg
folder/file2.jpg
folder/file3.png
destination/subfolder1/file1.txt
destination/subfolder1/file2.txt
destination/subfolder2/file1.txt
destination/subfolder3/

I know that, if I wanted to select all of my jpeg files (for example), I would do mv folder/*.jpg destination. But what is the command to select all folders?

0

3 Answers 3

12

For that, you just need to include a aditional / at end of * like that:

mv folder/*.jpg destination (match only jpg files) 
mv folder/* destination (match anything found)
mv folder/*/ destination  (match only the folders) 

This will move only the folders inside "folder" to the destination, and not the files inside "folder" (note that the files in subfolders, are moved).

0
0

If the subfolders have uniform names, you may use roaima's answer. If not, you may use a simple shell loop:

mkdir -p destination
for name in folder/*; do
    [ ! -d "$name" ] && continue
    mv "$name" destination
done

This would loop over all directory entries in folder (files and directories alike), test each one for whether it's a directory or not, and move them if they are.

Another possibility is to use find:

mkdir -p destination
find folder -mindepth 1 -maxdepth 1 -type d -exec mv {} destination ';'

This would find all pathnames of all directories in folder (but not below, and not the directory folder itself), and move each found directory to destination.

0

Depending on the actual directory names you can use this

mv folder/subfolder* destination/

If there is no pattern (subfolder*) to match the folder names you could do this

find folder -mindepth 1 -maxdepth 1 -type d -exec mv {} destination/ \;

or even this

find folder -mindepth 1 -maxdepth 1 -type d -exec mv -t destination/ {} +
2
  • Ok thank you ! That is what I was looking for. Can you please explain what \; is for in the end of the first find example ?
    – cdrom
    Commented Apr 24, 2018 at 14:36
  • @cdrom It tells find where the command line to execute with -exec ends. The + in the second find serves the same purpose, but has different semantics. See the find manual.
    – Kusalananda
    Commented Apr 24, 2018 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .