Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using the following script to auto download files from my remote seedbox using lftp:

set ftp:list-options -a
set ftp:ssl-allow no
set mirror:use-pget-n 5
set cmd:fail-exit true
open ftp.myseedbox.com
mirror -c -P5 --Remove-source-files --log=synctorrents.log /completed /media/ExternalHd/
quit

Now, this removes files after successful transfer but leaves empty folders. Is there any method/script code to automatically remove empty folders ?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Linux has an inbuilt tool for this, rmdir:

$ man rmdir
NAME
       rmdir - remove empty directories

SYNOPSIS
       rmdir [OPTION]... DIRECTORY...

DESCRIPTION
       Remove the DIRECTORY(ies), if they are empty.

You can safely run a command like rmdir * since it will ONLY remove empty directories.

share|improve this answer
    
I need to do this as an automated task using scripting after lftp completes its job on remote computer. Any ideas on how to do this using lftp ? –  DeepeshAgarwal Nov 21 '12 at 14:13
    
@DeepeshAgarwal, I don't have any experience with lftp but can't you just add rmdir /completed/* after mirror and before quit? –  terdon Nov 21 '12 at 14:25
1  
@DeepeshAgarwal, I just checked and [linux.about.com/od/commands/l/blcmdl1_lftp.htm](lftp supports the rmdir command). So, you should just be able to add rmdir /remote/path to your script between mirror and quit. –  terdon Nov 22 '12 at 18:52

In my case rmdir -f * didn't work and I had to find another solution.

I had this situation:

mirroring with this command in my script:

lftp -u ${FTPUSER},${FTPPASS} ${FTPHOST} -e "mirror --depth-first --no-empty-dirs --Remove-source-files --verbose . ${MYLOCALFOLDER} ; bye" >> $LOG 2>&1

but this left an empty tree of directories like this on remote:

|-- 693
|   `-- 2014-01-06
|-- 75
|   |-- 2014-01-10
|   |-- 2014-01-11
|   |-- 2014-01-12
|   |-- 2014-01-13
|   |-- 2014-01-14
|   |-- 2014-01-15
|   `-- 2014-01-16
|-- 811
|   |-- 2014-01-07
|   |-- 2014-01-08
|   |-- 2014-01-09
|   |-- 2014-01-10
|   |-- 2014-01-11
|   |-- 2014-01-12
|   |-- 2014-01-13
|   |-- 2014-01-14
|   |-- 2014-01-15
|   `-- 2014-01-16

so I added this to my script:

# create a local mirror with the empty dir structure                                                                   
mkdir /tmp/lftp_emptydirlist
cd /tmp/lftp_emptydirlist
lftp -u ${FTPUSER},${FTPPASS} ${FTPHOST} -e "mirror ;bye"

# sort -r is to reverse and leave the empty parent dirs at the end. 
# egrep will strip the "." directory from the list   
find|sort -r|egrep -v '^.$' > /tmp/emptydirlist.txt

# remove remote empty directories                                                                                      
for i in $(cat /tmp/emptydirlist.txt) do
   lftp -u ${FTPUSER},${FTPPASS} ${FTPHOST} -e "rmdir -f $i; bye"
done

# remove local empty directories                                                                                        
find ${MYLOCALFOLDER} -type d -empty -delete

If you want to test and see what the bash 'for' command is actually doing, you can add an 'echo' before the lftp command, so it will print it instead of actually doing it:

for i in $(cat /tmp/emptydirlist.txt); do echo lftp -u ${FTPUSER},${FTPPASS} ${FTPHOST} -e "rmdir -f $i; bye"; done

The down side of this approach is it will open many ftp sessions, actually one session for every directory to remove ... which is probably not the best way, but ... it's the best way I found so far anyway :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.