Find the answer to your Linux question:
Results 1 to 4 of 4
I am creating a backup script with the cp command I was piping the output of the command using >> to a log file But my log file grows big ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2

    cp -drufv and keep a count of files copied

    I am creating a backup script with the cp command

    I was piping the output of the command using >> to a log file

    But my log file grows big quickly that way. I would rather just log the # of files copied as a way to ensure the backup is working.

    I'm trying to figure out how to count the # of files copied using the verbose output from cp

    like:
    echo "backup started" >> logfile
    cp -drufv * /backups >> count the number of files here = $count
    echo "back finished" >> logfile
    echo "$count files coped" >> logfile
    exit 0

    thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    do the counting before the copy, using wc.

  3. #3
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2
    Thanks for the suggestion but I've never used wc and couldn't figure out the syntax after a few minutes of tinkering.

    But that got me thinking about grep and here is what I came up with:

    #!/bin/sh
    #
    #####written by lee Aug 2008
    #
    #####clear backups log
    #
    echo " " > /Users/lee/system_backup/backups_log.txt
    #
    #####clear file log
    #
    echo " " > /Users/lee/system_backup/file_log.txt
    #
    #
    #
    #####begin backups
    #
    #####lee backups
    #
    cd /Users/lee
    cp -drufv * /Volumes/backups/lee/ >> /Users/lee/system_backup/file_log.txt
    #
    #####dana backups
    #
    cd /Users/dana
    cp -drufv * /Volumes/backups/dana/ >> /Users/lee/system_backup/file_log.txt
    #
    #####pics backups
    #
    cd /Volumes/storage/pics/
    cp -drufv * /Volumes/backups/pics/ >> /Users/lee/system_backup/file_log.txt
    #
    #
    #####end backups
    #
    #
    #
    #####stamp end time and report number of files copied
    #
    echo "The most recent System Backup completed on" `date` "and" `grep -c -e '->' /Users/lee/system_backup/file_log.txt` "files were copied." >> /Users/lee/system_backup/backups_log.txt
    #
    #####done
    #
    exit 0

    notice that I decided to clear both logs (mandatory for the file log so the count will be meaningful) at the begining. I had been using a running backup log before.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by hleehall View Post
    Thanks for the suggestion but I've never used wc and couldn't figure out the syntax after a few minutes of tinkering.
    the syntax is all in the man page of wc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...