Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

shell scripting question

  • 04-12-2002 05:30PM
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    just wondering how to back up every file in my home directory. What i mean _exactly_ is to add a .bak extension to each file using bash scripting. I can't for the life of me figure this one out. i can do it to a file one by one but i want to do it to every file at once.

    Thanks in advance!@#


Comments

  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    I don't do much bash scripting, but a cursory examination of the Advanced Bash Scripting Guide gives me something like this:
    #!/bin/sh
    for i in ~/*
    do
            if [ -f "$i" ]
            then
                    cp $i $i.bak
            fi
    done
    exit 0
    
    adam


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Thanks adam!


  • Registered Users, Registered Users 2 Posts: 432 ✭✭Catch_22


    just to be pedantic your better of using ~/.* instead of ~/* so that you get all the .files (i.e. config files) which ~/* wont match.

    Also if you want to get the subdirectories too do:

    #!/bin/sh
    for file in `find ~ -type f`; do
    cp $file $file.bak
    done

    c22


Advertisement