Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Backing up files script?

Options
  • 23-05-2011 7:20pm
    #1
    Registered Users Posts: 80 ✭✭


    Hey,

    I need help.. I'm trying to figure out a shell script to back up all the pdf's files within my home directory?

    Im new to all this stuff and really wanna learn. Anyone any ideas?

    Cheers in Advance!
    Tagged:


Comments

  • Registered Users Posts: 2,621 ✭✭✭GreenHell


    #!/bin/bash

    find ~/ -name "*.pdf" -exec mv '{}' /path/to/somewhere


    Should do the trick for you


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    GreenHell wrote: »
    #!/bin/bash

    find ~/ -name "*.pdf" -exec mv '{}' /path/to/somewhere


    Should do the trick for you

    That will backup all PDFs in the home directory and recursively through all sub-directories. Is that what you want OP, or do you just want PDFs in your home directory, not in sub-directories?

    Also, where are you backing them up to? GreenHell's script will move them to another directory assuming it is mounted, but perhaps you want to backup over the network? If so, rsync is your man.

    Please give us more information so we can help you better.


  • Registered Users Posts: 80 ✭✭robfullam


    How can i back them up in the current directory and make them .bak files? Im not doing it over a network.

    Cheers


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    So you want to backup all files in the current directory, and give them the extension .bak?
    #!/bin/sh
    for f in *.pdf
    do
      mv "$f" "$f.bak"
    done
    


  • Registered Users Posts: 80 ✭✭robfullam


    How do i combine GreenHalls and Skylops scripts together to backup all the files in and under my home directory including sub directories


  • Advertisement
  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    why dont you tell us how you think you could do it, and we can give you pointers. The way i learnt scripting was reading a bit, then trying a bit, and so on. Read the rules and then trial and error. That way you will learn way more.


  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


    I'd also consider using cp rather than mv as you may want to hang on to the originals


  • Registered Users Posts: 80 ✭✭robfullam


    #!/bin/sh
    
    for f in *.pdf
    
    do
    
    cp "$f" "$f.bak"
    
    done
    

    that works fine, but it wont search sub directories within my home folder too. I've tried various amounts of things including some of GreenHalls scripting. its just over my head!

    HELP!


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    #!/bin/sh
    
    for f in *.pdf
    
    do
    
    cp "$f" "$f.bak"
    
    done
    

    Try to understand what it is doing.

    for f in *.pdf. That basically means take all files ending in .pdf and copy each so it reads file.pdf.bak.

    Try writing pseudo-code to get your head around it. This is english words for each logical step of the script. Once you figure out each step in english, replace the functions with bash keywords.

    So the pseudo-code is:

    take all files that are .pdf in all sub directories
    then for each one, give it the extension .bak.

    the first line of the above pseudo-code you already have from GreenHell's code. The second line you already have from mine. You just need to add them together, and get what the logic is. Do you know what a backtick is?


  • Registered Users Posts: 80 ✭✭robfullam


    A backtick is `.

    Whats its function??

    I know in english what i want to do but i just cant find the command to search sub-directories also. i am only doing this 3 weeks and really want to learn it.


  • Advertisement
  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    robfullam wrote: »
    A backtick is `.

    Whats its function??

    I know in english what i want to do but i just cant find the command to search sub-directories also. i am only doing this 3 weeks and really want to learn it.

    Correct, though if you dont know what a backtick does, I suggest you go and do some more reading. The only way I could solve your problem(albeit with out thinking too hard about it), was using backticks.


  • Registered Users Posts: 80 ✭✭robfullam


    #!/bin/sh
    
    find . -type f -name '*.sh' |
    while read 
    do
     cp "$f" "$f.bak"
    done
    


    GOT IT :)


  • Moderators, Arts Moderators Posts: 35,462 Mod ✭✭✭✭pickarooney


    ^^That will backup .sh files not .pdf files.


Advertisement