WEB LINUX USEFULL

|^^|

http://www.kossboss.com/linux---bytes-to-human-readable-command

http://www.kossboss.com/linuxbegginer_timestamp

LINUX - Beginner - Bash/Script with Date for Copy Backup - Adding Timestamp

BASH/SCRIPT EXAMPLE TO ADD TIME STAMP TO FILE

#############################################

Try this Command - 2 ways - sometimes better to use commands full name just incase another exists somewhere:

# date --help

(or)

# /bin/date --help

TRY THESE COMMANDS LIKE THIS SO YOU SEE WHY ITS SO LONG IN THE END:

# date +%s

1362732681 - time since unix epoch (1970-01-01 UTC) - its a universal time we can all measure from

# date +%T

00:52:35

# date +%D

03/08/13

# date +s%s

s1362732770 <-- would do as a good timestamp even without the "s" in the front

# date +s%s-D%D

s1362732786-D03/08/13

# date +s%s-D%D-T%T-%S

s1362732791-D03/08/13-T00:53:11-11

# date +s%s-D%D-T%T-%S | tr "/" "_"

s1362732796-D03_08_13-T00:53:16-16

# date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"

s1362732801-D03_08_13-T00_53_21-21 <-- Good long timestamp

WE CAN PREFIX IT WITH /bin JUST TO BE SAFE

# date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"

(or)

# /bin/date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"

SO PUT THAT INTO A VARIABLE - those ` are backticks they make commands inside em run then you can use the text as the variable (or part of the variable):

# DATEnow=`/bin/date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"`

VERIFY - LOTS OF WAYS TO CALL VARIABLE:

# echo $DATEnow

SET ANOTHER VARIABLE WITH IT:

# FILENAME=/tmp/somefile-${DATEnow}

(or)

# FILENAME=/tmp/somefile-$DATEnow

(or without using DATEnow)

# FILENAME=/tmp/somefile-`/bin/date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"`

Type "echo $FILENAME" to confirm at anytime.

NOW YOU CAN USE FILENAME AS VARIABLE IN ANY FUNCTION LIKE MAYBE A COPY SCRIPT:

REMEMBER EACH SCRIPT STARTS WITH A SHABANG(#!/bin/bash) TELLS LINUX WHICH INTERPRETER TO USE FOR THE SCRIPT- ITS A COMMENT SO IT DOESNT AFFECT THE CODE - JUST WHO RUNS THE CODE

# cd /tmp

# touch script.sh

# chmod 777 script.sh

(or WE JUST NEED EXECUTION BIT ON IT TO RUN [without execution bit you can still run script in bash like so "source scriptfilename")

nano is a simple file editor - i dont like it but its simple so its good for beginners - i just think beginners should start with vi though to get better at linux typing

# nano script.sh

(or my personal favorite vi)

# chmod +x script.sh

When you start vi you start in normal mode - you cant type just yet. Then key the letter "i" to start TYPING - insert mode. When done use "ESC" key to get get out of insert mode. When out of insert mode your in normal mode - just as when you entered vi - to SAVE Type the following ":w" Without the quotes (SHIFT+; to get the : and then key the letter w and then hit enter) - to SAVE AND EXIT ":wq!" - to EXIT AND NOT SAVE ":q!" - note the ! forces the commands without it you would be fine too, google "how to use vi" also google "how to install vim" and then use vim instead of vi - its just like vi but with more features - vi is just on every system so thats why I talk about it here

# vi script.sh

PUT THE FOLLOWING IN YOUR SCRIPT (when done can execute script like this "./script.sh" when in tmp folder - if out of tmp folder do "./tmp/script.sh" - both without quotes -- also make sure that you dont put # anywhere besides the shebang - it actually doesnt hurt at all to put # its just a comment line thus it will ignore commands so thats why Im not including it with the actual commands):

#!/bin/bash

DATEnow=`/bin/date +s%s-D%D-T%T-%S | tr "/" "_" | tr ":" "_"`

SOURCEFILE=/tmp/somefile

DESTINATIONFILE=/tmp/somefolder/newfile-${DATEnow}

echo "THE FOLLOWING COMMAND JUST RAN: " cp "$SOURCEFILE" "$DESTINATIONFILE"

cp "$SOURCEFILE" "$DESTINATIONFILE"

EOF