Copying files with a script

Posted on 2012-09-20
Last Modified: 2012-09-21
Hi Experts,

I have log files generating every 12 hours a day. I need to copy them with a script everyday  to my home directory.

The logs files are in this format:
Question by:07592161981m
    LVL 23

    Expert Comment

    let's say the logs are in /var/log/

    Here is the script, create a new file called with the following contents.

    cd /var/log/
    cp loga* /home/username/

    Then execute it with a cronjob by running the command "crontab -e" and add the following:

    59 23 * * * /path/to/

    if the logs are protected and only run can see them you will have to do all above as root.  Also you may want to change the permissions so your user can view them.  In that case use this:

    cd /var/log/
    cd loga* /home/username/
    chown username /home/username/loga*

    If you give more information like where the logs are located, etc... I can give you the exact script.  Also if these logs are being written to constantly you might want to stop the service writing to them or only move the logs that are already rotated.

    Author Comment

    If I use loga*, then all the files will repeatedly copied to the destination directory.
    I  need to copy only two files today that were generated yesterday.

    LVL 1

    Expert Comment

    I often use the sfk tool for this, which has flexible file selection. If you strictly want to copy only files changed about 1 day ago, try
    sfk select -since 2d -before 1d mylogs loga +run "cp #qfile /home/username" -yes

    Open in new window

    which selects all files *loga* from mylogs, newer than 2 days, but older than 1 day, and runs a cp command on them.
    LVL 23

    Expert Comment


    Here is your script, change as needed.

    find /var/log/ -mtime -1 -name "loga*" -exec mv {} /home/username/ \;

    This will find any file in /var/log that starts with loga that has been modified in the last 24 hours and move it to the specified directory, in this case /home/username/
    LVL 23

    Accepted Solution

    I just noticed you said copy, not move... Change mv to cp.

    find /var/log/ -mtime -1 -name "loga*" -exec cp {} /home/username/ \;
    LVL 48

    Expert Comment

    rsync would be much more efficient for this purpose.

    rsync -av loga* /home/username

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now