1

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 ?

3 Answers 3

2

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.

3
  • 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 ? Nov 21, 2012 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, 2012 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, 2012 at 18:52
1

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 :)

1

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
quit
EOF

You must log in to answer this question.

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