Skip to main content

Move files from one directory to another with cron job

Comments

17 comments

  • 24x7server
    What is the output of following command ?
    ls -ld /home/mysite/public_html/folder/test ls -ld /home/mysite/public_html/folder/test1
    0
  • greektranslator
    Here it is: .vB
    0
  • cPanelPeter cPanel Staff
    Hello, Remove the $ from the first 2 lines... It should be:
    sDirectory="/home/mysite/public_html/folder/test" snew="/home/mysite/public_html/folder/test1"
    0
  • greektranslator
    Thanks! It worked.
    0
  • Nilkesh Patel
    Thanks... I had also the same problem.
    0
  • greektranslator
    How is it possible to adapt the following bit -name "*.htm*" So that it moves all files APART from .htaccess?
    0
  • madmanmachines
    Hi greektranslator, '! -name .htaccess' would be necessary as in
    find $sDirectory -type f -maxdepth 1 -name "*.htm*" ! -name .htaccess -exec mv {} $snew \;
    Thanks,
    0
  • greektranslator
    Thanks, how can I add more extensions to the excluded list? I have this but after a test run I found in destination directory some strange files like .autorespond-loopprotect, .bash_history, .spamassassinenable, .cpanel-ducache, .bash_profile which appear to have been in the root of the specific account? #!/bin/bash sDir1="/home/account/folder1" sDir2="/home/account/folder" snew="/home/1" find $sDir1 -type f -mmin +30 -maxdepth 1 -not -name "*.htac*" -exec mv {} $snew \; find $sDir2 -type f -mmin +30 -maxdepth 1 -not -name "*.htac*" -exec mv {} $snew \;
    0
  • madmanmachines
    Hi, how can I add more extensions to the excluded list?
    This can easily be found by googling... linux find multiple names
    find $sDirectory -type f -maxdepth 1 -name "*.htm*" ! \( -name .htaccess -o -name "other-string-here" \) -exec mv {} $snew \;
    I have this but after a test run I found in destination directory some strange files like .autorespond-loopprotect, .bash_history, .spamassassinenable, .cpanel-ducache, .bash_profile
    It looks like you have moved essential cPanel files out of their necessary locations. Hopefully you made a backup prior to these changes or you are able to put these back properly. Thanks,
    0
  • johnbVegas
    I have a similar issue where as I would like the output of a cron job to be posted in a different directory than where the script resides where as: directory the script resides is: /home/yellowpc2/cm/acct1/codephpscript/script.php directory where I would like output file generated by scrip is: /home/yellowpc2/cm/acct1/newfeed name of script is: script.php name of the output file that script generates is: nameofoutputfile.txt Here is cron job that I entered in commannd line: php -q /home/yellowpc2/cm/acct1/codephpscript/script.php &> /home/yellowpc2/cm/acct1/newfeed/nameofoutputfile.txt the output file still is being generated in the same directory where the script is however the output file in the directory I want is being generated but is empty with o bytes. Alternatively i could create another cron job to simply copy the output file to the other directory but this should be an easy command, but not sure why it is not working correctly. Please let me know what the correct command should be. thank you.
    0
  • cPRex Jurassic Moderator
    @johnbVegas - it's best to create a new thread instead of replying to an older one for maximum visibility. The most likely explanation is that your script is using relative paths, which may not work well with cron. I tested this on my personal machine and found that this basic script would fail:
    #!/bin/bash touch testdir1/touchfile1.txt
    while this worked properly:
    #!/bin/bash touch /home/username/public_html/testdir1/touchfile1.txt
    Cron executes from /home/user so you'd want to make sure to use the full path in that PHP script and then see if that gets things working.
    0
  • johnbVegas
    K thanks cPRex, Yes I tried to start a new thread but i think the system was requesting server information that i did not have. In any case, you should know the cron job works fine when the output file is created in the same directory where the script resides. However when I try to direct the output of to be sent to path designatated by " &> " a file is created on new directory, however it only has 8 bytes, and when I open the output file. its basically empty other than the word "done! " in first cell of spreadsheet, when i should be couple hundred rows of data. I am not a developer so I'm having problems understanding your example not sure what
    #!/bin/bash means Thanks for your help
    0
  • cPRex Jurassic Moderator
    Oh, that was just an example cron that happened to be written in bash. If you're trying to redirect the stream, which is what the &> is doing, that may need to get handled a bit differently. If you enter that cron command directly on the command line, does that work? That may give you more useful errors that would help with troubleshooting.
    0
  • johnbVegas
    Hi cPRex, Yes as mention the cron job works fine and places the output file on the same directory where the script resides. Ok i give, I will just create another cron job to copy the file from the existing directory to the other directory. Can you tell me what the Cron job command is to do a simple copy command is? thanks
    0
  • cPRex Jurassic Moderator
    There isn't a cron command that would perform that work. Cron only knows how to run jobs at a scheduled time, but you'd need to put the copy command in the job itself. For copying a file, I would recommend creating a bash script that cron calls. For example, if you wanted this to run once a minute, you'd create this cron: * * * * * /bin/bash /home/username/bash_script.sh and then the code in that file could perform the copy for you, maybe looking something like this:
    /bin/bash cp /home/username/file1 /home/username/file2
    0
  • johnbVegas
    Thanks, hmm so to make sure I understand you, given I am not a developer/programmer. I should create a new cron job with the following in the command line to run once a day at 10am 0 10 * * * /bin/bash /home/yellowpc2/cm/acct1/codephpscript /bash_script.sh where as: directory the script resides is: /home/yellowpc2/cm/acct1/codephpscript directory where I would like output file generated by scrip is: /home/yellowpc2/cm/acct1/newfeed name of script is: bash_script.sh name of the output file that script generates is: nameofoutputfile.txt not sure where I am supposed to put this part? /bin/bash cp /home/username/file1 /home/username/file2
    0
  • cPRex Jurassic Moderator
    No, that won't work properly. If you have a PHP script that runs and is generating the file, you would just need this: 0 10 * * * php /home/yellowpc2/cm/acct1/codephpscript Cron itself doesn't generated or copy or paste or anything like that - it just executes something. It doesn't care what that something is, so if you have your PHP script writing the output to nameofoutputfile.txt that's all that needs to get called. My bash examples were just in case you wanted Linux to perform the copy command for you, but it sounds like this isn't needed.
    0

Please sign in to leave a comment.