Linux scripting question

qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
I would like to create a shell or python script that will copy multiple directories one after the other and am not sure how to start.

for example
cp -au /home/user /backup
cp -au /home/user /backup
cp -au /tmp/ /backup


Please not that the source and destinations of my actual files will all be in different directories

Comments

  • qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
    Sorry for posting this in the wrong section.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Actually you could just add those lines into a file called

    something.sh

    then make it executable

    chmod +x something.sh

    and execute it

    ./something.sh

    Dirty - but works ;)
    My own knowledge base made public: http://open902.com :p
  • qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
    Gomjaba wrote: »
    Actually you could just add those lines into a file called

    something.sh

    then make it executable

    chmod +x something.sh

    and execute it

    ./something.sh

    Dirty - but works ;)

    Thanks for that. I have another question, In linux how could O script a check? By this I mean check if a directory exist and if not then make directory and continue on to the next step.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Not 100% sure, what I do is for example
    TODAY_DATE=`date +%d%m%Y`
    mkdir /backup/$TODAY_DATE
    

    That creates a folder based on the current date. If the folder exists, it simply says
    mkdir: cannot create directory `25032011': File exists
    

    But continues with the operation .. Not sure if that is what you need though :)
    My own knowledge base made public: http://open902.com :p
  • lordylordy Member Posts: 632 ■■■■□□□□□□
    You should have a look at "man test". There are a bunch of these tests that you can run. Example:
    if [ -d /tmp ]; then
      echo "/tmp exists"
    fi
    
    if [ -x /bin/true ]; then
      echo "/bin/true is executable"
    fi
    

    You can invert these checks by using !.
    if [ ! -z /etc/motd ]; then
      echo "/etc/motd is NOT empty"
    fi
    

    PS: The message filter on this board sukcs. You cannot write "/etc/p a s s w d" in your post. Took 5 minutes of my precious time to figure that out...
    Working on CCNP: [X] SWITCH --- [ ] ROUTE --- [ ] TSHOOT
    Goal for 2014: RHCA
    Goal for 2015: CCDP
  • qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
    lordy wrote: »
    You should have a look at "man test". There are a bunch of these tests that you can run. Example:
    if [ -d /tmp ]; then
      echo "/tmp exists"
    fi
    
    if [ -x /bin/true ]; then
      echo "/bin/true is executable"
    fi
    
    You can invert these checks by using !.
    if [ ! -z /etc/motd ]; then
      echo "/etc/motd is NOT empty"
    fi
    
    PS: The message filter on this board sukcs. You cannot write "/etc/p a s s w d" in your post. Took 5 minutes of my precious time to figure that out...

    Thanks alot for your help, i really appreciate it.
    I hope you can answer me one more question.

    The first portion that you posted about checking if the /tmp exists. Thats exactly what im trying to do but id like it to execute a command if it is true. Is that possible and ifso how?


    If that doesn't make much sense, in batch I would do something like this:
    if exist c:\foo goto next
    :next
    copy c:\foo d:\backup
  • lordylordy Member Posts: 632 ■■■■□□□□□□
    It's easy. You can put anything between if and fi that you need. Example:
    if [ -d /some/directory ]; then
      cp /some/directory/* /some/other/directory/
    fi
    
    Working on CCNP: [X] SWITCH --- [ ] ROUTE --- [ ] TSHOOT
    Goal for 2014: RHCA
    Goal for 2015: CCDP
  • qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
    lordy wrote: »
    It's easy. You can put anything between if and fi that you need. Example:
    if [ -d /some/directory ]; then
      cp /some/directory/* /some/other/directory/
    fi
    


    Am I correct to assume that if the directory above doesn't exist that it will skip to the next line in my script?
  • MentholMooseMentholMoose Senior Member Member Posts: 1,524 ■■■■■■■■□□
    Yes. Check the man page for "test". For "-d" it says:
    FILE exists and is a directory
    MentholMoose
    LFCE - MCITP: EDA7, VA, SA, EA - MCSA:S 2003 - CCA (PVS 5, XD 3 / 4 / 5, XS 5 / 6) - VCP 4 / 5
  • qwertyiopqwertyiop Member Posts: 725 ■■■□□□□□□□
    thanks for your help
Sign In or Register to comment.