Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
mirror -c -P5 --Remove-source-files --log=synctorrents.log /completed /media/ExternalHd/

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
up vote 2 down vote accepted

Linux has an inbuilt tool for this, rmdir:

$ man rmdir
       rmdir - remove empty directories

       rmdir [OPTION]... DIRECTORY...

       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
@DeepeshAgarwal, I just checked and [](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"

# 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

rmdir * does not work properly in lftp as it does not support wildcard expansion. One could overcome this by using the below, some brute force is required to remove nested folders:

glob -a rmdir /remote/directory/*/*/*
glob -a rmdir /remote/directory/*/*
glob -a rmdir /remote/directory/*

I do not recommend using rm -rf as any files added while the mirror is being performed (and any files that failed to transfer) will be removed without mirroring.

Here is code that will remove empty folders (including nested and folders containing spaces), preserving files that were not mirrored and does everything in one Lftp session:

lftp -p $port -u $login,$pass sftp://$host <<-EOF
cd "$remote_dir"
find . | grep [[:alnum:]] | sed -e 's~.~rmdir" "-f" "\".~' -e 's~$~\"~' | tac > /tmp/delete
mirror -v --no-empty-dirs --Remove-source-files -c -P3 $remote_dir $local_dir
source /tmp/delete
share|improve this answer

Your Answer


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.