[SATLUG] Help making a bash cp script

Daniel J. Givens daniel at rugmonster.org
Tue Dec 28 12:38:56 CST 2010

On 12/26/2010 6:00 PM, Seth Sanchez wrote:
> I have to say I'm terrible at coding, so I thought I might ask here. All I
> want to do is copy a large group of mp3's to an external drive using the cp
> command. The total size is about 28GB~. In copying over all of the files
> though, I want to remove any empty directories. Old empty albums for
> example. I've been googling a little bit, trying to find a similar script to
> butcher apart and use for my own devices, but haven't had any luck. Does
> anyone have any ideas on where I should go with this?

If you want to remove empty directories (as in directories with no 
files), you can use something like this.

for SUBDIR in $( find $TOPDIR -mindepth 1 -maxdepth 1 -type d )
	if [[ $( find $SUBDIR -type f | wc -l ) == 0 ]]
		rm -rvf $SUBDIR

Save it as any other bash script and run it with the top directory of 
the directory tree as the argument.

Example: $ ./dirpurge.sh /path/to/top/dir
          removed directory: `/path/to/top/dir/emptydir'

If you've already done an rsync once, you can run it again with the 
--delete option to clean up your backup location, assuming you haven't 
added anything to the destination that isn't in the source location.

More information about the SATLUG mailing list