I have many empty folders that I do not want to delete but move to a subfolder called "my_empty_folders". How can I do that in a shell script?

  • You can use find command in combination with -type , -empty and -exec options. Give it a try by yourself. – George Vasiliou Oct 23 '18 at 11:00
  • I've already tried it myself (look for the question "moving folder older than 30 days to another folder"), unfortunately that's not working. – Karina Oct 23 '18 at 11:16

You can use find:

find / -empty -type d -exec mv {} /path/to/my_empty_folders \;
  • Thank you. Unfortunately, empty folders that contain more empty folders are not moved. Example: A folder called "1" contains the empty folders "2", "3" and "4". 2,3,4 are being moved, but folder 1 remains at its original location. I can work around the problem of double executing the command, but I do not want that. Any advice? – Karina Oct 23 '18 at 16:10
  • 1
    @Karina Folder that contain another folder is not empty! You can write more advanced script and build list of directories to remove – mrc02_kr Oct 23 '18 at 16:26

You do not need a shell script, but just a single command.

 $ man mv
 $ mv file1 file2 file3 -t <destination/path>

For instance, if your directories are called empty_1, empty_2, empty_3, just do

mv empty* -t <destination/path>

  • And how do you know which directories are empty? – mrc02_kr Oct 23 '18 at 11:00
  • he/she said she had many different empty directories. So he/she already knows. Why the downvote? – llIIIIIllllIIIIlllII Oct 23 '18 at 11:07

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.