3

I have a directory called crmlogs, how do I use rsync -a --delete to delete it?

mkdir emptydir
rsync -a --delete ./emptydir ./crmlogs
5
  • im using find . -name ".gz" -mtime +3 -exec rm {} \;
    – Lilly
    Jan 5 '16 at 14:44
  • this takes a whole lot of time and doesnt delete much
    – Lilly
    Jan 5 '16 at 14:44
  • 6
    This sounds like an XY problem - why do you insist on using rsync when rm is the standard tool for the job?
    – Jeff Schaller
    Jan 5 '16 at 17:36
  • This is more than an XY problem: <code>\</code>, this is the wrong OS.
    – dan
    Jan 10 '16 at 23:43
  • 1
    rsync is significantly faster them rm
    – Andy
    May 12 '20 at 11:48
5

The --delete switch deletes files from the destination that do not exist in the source. If you want to delete crmlogs, you need to be copying from a source that does not contain that directory. Your example shows you using rsync between two directories under the root directory. It is easier to see in this example under /tmp:

/tmp $ cd tmp
/tmp $ mkdir foo
/tmp $ mkdir bar
/tmp $ mkdir bar/crmlogs

/tmp $ rsync -a --delete foo/ bar/

/tmp $ ls -l bar
total 0
drwxr-xr-x  2 mint mint  40 Jan 10 23:19 ./
drwxrwxrwt 10 root root 220 Jan 10 23:19 ../

Note that rsync -a --delete foo bar or rsync -a --delete ./foo ./bar just puts a copy of foo under bar.

Judging by your follow-up comment, I'm not sure whether you really want to delete the crmlogs directory at all, or just flush out certain log files that may no longer exist in the source. If so, the --delete switch should be useful to you once those files have been removed from the source folder.

1

according to linuxnote.net's webarchive is rsync -aP –delete empty/ ./dir the fastest way to delete large directories, about three times faster as with rm -rfv ./dir or with find b/ -type f -delete

since it can take a while, a verbose output with -P is convenient or you gaze at an empty prompt line for ever...

and in order to avoid endless scrolling, I redirected the STDOUT to awk to display the output on a single line (with a line counter) like this:

rsync -aP --delete ./emptyDir/ ./dir/ | awk ' { printf ( "\r #%4d: %-200s", NR, $0 ) } '

for skipping the Permission denied lines, use sudo:

sudo rsync -aP --delete ./emptyDir/ ./dir/ | awk'{printf("\r #%4d: %-200s",NR,$0)}'

explanation: awk because printf alone doesn't catch STDOUT, \r to set the cursor at the begin of the output line, NR for a line# counter and %4d right-pads the line# and %-200s for an output string of 200 chars right padded that overwrites the previous line (200 if terminal window has at least this width)

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.