How to Understand Epoch & Unix Timestamp | Epoch Converter

epoch-unix-timestamp

What is epoch time?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but ‘epoch’ is often used as a synonym for ‘Unix time’. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

Human readable time Seconds
1 hour 3600 seconds
1 day 86400 seconds
1 week 604800 seconds
1 month (30.44 days) 2629743 seconds
1 year (365.24 days) 31556926 seconds

How to get the current epoch time in …

Perl time
PHP time()
Ruby Time.now (or Time.new). To display the epoch: Time.now.to_i
Python import time first, then time.time()
Java long epoch = System.currentTimeMillis()/1000;
Microsoft .NET C# epoch = (DateTime.Now.ToUniversalTime().Ticks – 621355968000000000) / 10000000;
VBScript/ASP DateDiff(“s”, “01/01/1970 00:00:00”, Now())
R as.numeric(Sys.time())
Erlang calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time( now()))-719528*24*3600.
MySQL SELECT unix_timestamp(now()) More info (+ negative epochs)
PostgreSQL SELECT extract(epoch FROM now());
Oracle PL/SQL SELECT (SYSDATE – TO_DATE(’01/01/1970 00:00:00′, ‘MM-DD-YYYY HH24:MI:SS’)) *
24 * 60 * 60 FROM DUAL
SQL Server SELECT DATEDIFF(s, ‘1970-01-01 00:00:00’, GETUTCDATE())
JavaScript Math.round(new Date().getTime()/1000.0) getTime() returns time in milliseconds.
Tcl/Tk clock seconds
Unix/Linux Shell date +%s
PowerShell Get-Date -UFormat “%s” Produces: 1279152364.63599
Other OS’s Command line: perl -e “print time” (If Perl is installed on your system)

Convert from human readable date to epoch

Perl Use the Perl Epoch routines
PHP mktime(hour, minute, second, month, day, year) More info
Ruby Time.local(year, month, day, hour, minute, second, usec ) (or Time.gm for GMT/UTC input). To display add .to_i
Python import time first, then int(time.mktime(time.strptime(‘2000-01-01 12:34:00’, ‘%Y-%m-%d %H:%M:%S’))) – time.timezone
Java long epoch = new java.text.SimpleDateFormat (“MM/dd/yyyy HH:mm:ss”).parse(“01/01/1970 01:00:00”).getTime();
VBScript/ASP DateDiff(“s”, “01/01/1970 00:00:00”, time field) More info
C Use the C Epoch Converter routines
R as.numeric(as.POSIXct(“MM/dd/yyyy HH:mm:ss”, origin=”1970-01-01″))
MySQL SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD
More on using Epoch timestamps with MySQL
PostgreSQL SELECT extract(epoch FROM date(‘2000-01-01 12:34’));
With timestamp: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE ‘2001-02-16 20:38:40-08’);
With interval: SELECT EXTRACT(EPOCH FROM INTERVAL ‘5 days 3 hours’);
SQL Server SELECT DATEDIFF(s, ‘1970-01-01 00:00:00’, time field)
JavaScript Use the JavaScript Date object
Unix/Linux Shell date +%s -d”Jan 1, 1980 00:00:01″ Replace ‘-d’ with ‘-ud’ to input in GMT/UTC time.

Convert from epoch to human readable date

Perl Use the Perl Epoch routines
PHP date(output format, epoch); Output format example: ‘r’ = RFC 2822 date More info
Ruby Time.at(epoch)
Python import time first, then time.strftime(“%a, %d %b %Y %H:%M:%S +0000”, time.localtime(epoch)) Replace time.localtime with time.gmtime for GMT time. More info
Java String date = new java.text.SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”).format(new java.util.Date (epoch*1000));
VBScript/ASP DateAdd(“s”, epoch, “01/01/1970 00:00:00”) More info
C Use the C Epoch Converter routines
R as.POSIXct(epoch, origin=”1970-01-01″)
MySQL from_unixtime(epoch, optional output format) The default output format is YYY-MM-DD HH:MM:SS more …
PostgreSQL PostgreSQL version 8.1 and higher: SELECT to_timestamp(epoch); More info Older versions: SELECT TIMESTAMP WITH TIME ZONE ‘epoch’ + epoch * INTERVAL ‘1 second’;
Oracle PL/SQL SELECT to_date(’01-JAN-1970′,’dd-mon-yyyy’)+(1326357743/60/60/24) from dual
Replace 1326357743 with epoch.
SQL Server DATEADD(s, epoch, ‘1970-01-01 00:00:00’)
Microsoft Excel =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other time zones: =((A1 +/- time zone adjustment) / 86400) + 25569.
Crystal Reports DateAdd(“s”, {EpochTimeStampField}-14400, #1/1/1970 00:00:00#) -14400 used for Eastern Standard Time. See Time Zones.
JavaScript Use the JavaScript Date object
Tcl/Tk clock format 1325376000 More info
Unix/Linux Shell date -d @1190000000 Replace 1190000000 with your epoch, needs recent version of ‘date’. Replace ‘-d’ with ‘-ud’ for GMT/UTC time.
PowerShell Function get-epochDate ($epochDate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]’1/1/1970′).AddSeconds($epochDate)) }, then use: get-epochDate 1279152364. Works for Windows PowerShell v1 and v2
Other OS’s Command line: perl -e “print scalar(localtime(epoch))” (If Perl is installed) Replace ‘localtime’ with ‘gmtime’ for GMT/UTC time.

Reference

http://www.epochconverter.com/

Tagged : / / / / / / / / / / / / / / /

Common unix commands and utilities | unix commands and utilities Guide

unix-commands-and-utilities

To find a program not on this list (and there are hundreds), try the man program with the -k option followed by a keyword, for example,
man -k tape
to find all on-line manual entries that refer to programs that work with tapes.

Terminal control characters for C-shell (csh)

^h, backspace erase previously typed character
^u erase entire line of input so far typed
^d end-of-input for programs reading from terminal
^s suspend writing to terminal (freezes terminal)
^q continue writing to terminal
^z suspend currently running job; restart with bg or fg
^c kill currently running program and allow clean-up before exiting
^\ emergency kill of currently running program with no chance of cleanup

Also see a list of special characters that should not be used in filenames.

Login and authentication

login access computer; start interactive session
logout disconnect terminal session
passwd change local login password; you must set a strong password that is not easily guessed
kinit obtain kerberos ticket for connections to other kerberized computers
kdestroy destroy kerberos tickets (authorizations)

Information

date show date and time
history list of previously executed commands
man show online documentation by program name
info online documentation for GNU programs
w, who who is on the system and what they are doing
whoami who is logged onto this terminal
top show system stats and top CPU using processes
uptime show one line summary of system status

File management

cat combine files
cp copy files
ls list files in a directory and their attributes
mv change file name or directory location
rm remove files
ln create another link (name) to a file
chmod set file permissions
crypt encode/decode a file with a private key
gzip, gunzip compress/decompress a file
find find files that match specific criteria

Display contents of files

cat copy files to display device
more show text file on display terminal with paging control
head show first few lines of a file(s)
tail show last few lines of a file; or reverse line order
vi full-featured screen editor for modifying text files
pico simple screen editor for modifying text files
grep display lines that match a pattern
lpr send file to printer
pr format file with page headers, multiple columns, etc.
diff compare two files and show differences
cmp compare two binary files and report if different
comm compare two files; show common or unique lines
od display binary files as eqivalent octal/hex codes
strings show printable text embedded in binary files
file examine file(s) and guess type: text, data, program, etc.
wc count characters, words, and lines in a file

Directories

cd change to new directory
mkdir create new directory
rmdir remove empty directory (you must remove files first)
mv change name of directory
pwd show current directory

Disks

df summarize free space on disk filesystems
du show disk space used by files or directories

Special character handling for C-shell (See man csh)

* match any characters in a file name
~user shorthand for home directory of user
$name substitute value of variable name
\ turn off special meaning of character that follows
in pairs, quote string with special chars, except !
in pairs, quote string with special chars, except !, $
` in pairs, substitute output from enclosed command

Controlling program execution for C-shell (See man csh)

& run job in background
^c kill job in foreground
^z suspend job in foreground
fg restart suspended job in foreground
bg run suspended job in background
; delimit commands on same line
() group commands on same line
! re-run earlier commands from history list
jobs list current jobs
ps show process information
kill kill background job or previous process
nice run program at lower priority
at run program at a later time
crontab run program at specified intervals
limit see or set resource limits for programs
alias create alias name for program (normally used in .login file)
sh, csh execute command file

Controlling program input/output for C-shell (See man csh)

| pipe output to input
> redirect output to a storage file
< redirect input from a storage file
>> append redirected output to a storage file
tee copy input to both file and next program in pipe
script make file record of all terminal activity

Email and communication

msgs read system bulletin board messages
mailx send/read email; can be run by other programs to send exisiting files via email
uuencode
uudecode
encode/decode a binary file for transmission via email
rn read USENET news groups

Editors and formatting utilities

sed programmable text editor for data streams
vi full-featured editor for character terminals
emacs GNU emacs editor for character terminals
xemacs GNU emacs editor for X Window terminals
pico very simple text editor
fmt fill and break lines to make all same length
fold break long lines to specified length

X Window client programs (output to X terminal or server)

xterm provide login shell window
xauth manipulate authorization files
xload show system load
xman full screen online manual viewer
xemacs GNU emacs editor
gv interface to contol gs to display PostScript or PDF files on screen
xdvi display DVI files on X Window (screen preview)
gnuplot interactive data plotting on screen

TeX typesetting system

tex process TeX files to DVI (device independent) output
latex process LaTeX files to DVI
dvips print DVI files on Postscript laser printer
xdvi display DVI files on X Window (screen preview)
latex2html translate LaTeX files to HTML (for web pages)

Printing (BSD based)

lpr send file to print queue
lpq examine status of files in print queue
lprm remove a file from print queue
enscript convert text files to PostScript format for printing

Interpreted languages and data manipulation utilities

sed programmable text editor for data streams
awk pattern scanning and processing language
perl Practical Extraction and Report Language
sort sort or merge lines in a file(s) by specified fields
tr translate characters
cut cut out columns from a file
paste paste columns into a file
dd copy data between devices; reblock; convert EBCDIC

Graphics and mapping

gnuplot interactive data plotting; outputs to PostScript or X Window
gs “ghostscript” converter displays PostScript files on X Window displays or other devices

Networking/communications

ssh remote login/command execution; encrypted
scp remote non-interactive file copy; encrypted
sftp remote interactive file copy; encrypted
telnet remote network login; plain text password – not recommended
ftp network file transfer program; plain text password – not recommended
host find IP address for given host name, or vice versa
lynx web browser for character based (text-only) terminals
gzip, gunzip compress/decompress a file
tar combine multiple files/dirs into single archive
uuencode
uudecode
encode/decode a binary file for transmission via email

Compilers, interpreters and programming tools

csh command language interpreter (C-shell scripts)
ksh command language interpreter (Korn-shell scripts)
sh command language interpreter (Borne-shell scripts)
f77 Fortran 77 compiler
f2c convert fortran source code to C source code
gcc GNU C compiler
g++ GNU C++ compiler
dbx command-line symbolic debugger for compiled C or Fortran
make recompile programs from modified source
cflow generate C flow graph

Programming libraries (see man library_name)

lapack Fortran 77 routines for numerical linear algebra (supersedes LINPACK and EISPACK)
X routines to interface with X window system (no man page — get the X Toolkit book)
dbm database routines
xdr library routines for external data representation
netcdf routines for machine independent data representation

Tape manipulation and archiving

mt manipulate tape drive and position tape
dd unformatted tape read and write; file conversion
tar archive disk files on tape or disk
ltf read/write ANSI standard label tapes
Tagged : / / / / / / / / / / / / / / / /

Unix Sed Quick Reference

unix-sed-quick-reference

UNIX SED Introduction

· Sed is a “non-interactive” stream-oriented editor. Since its an “non-interactive” it can be used to automate editing.

· This allows you to edit multiple files, or to perform common editing operations without opening VI or emacs.

· Sed reads from a file or from its standard input and outputs to its standard output.

· Sed has two buffers which are called pattern buffer and hold buffer. Both are initially empty.

UNIX Sed Working Method

This is called as one execution cycle. Cycle continues till end of file/input is reached.

1. Read a entire line from stdin/file.

2. Removes any trailing newline.

3. Places the line, in its pattern buffer.

4. Modify the pattern buffer according to the supplied commands

5. Print the pattern buffer to stdout.

Printing Operation in Sed

Linux Sed command allows you to print only specific lines based on the line number or pattern matches. “p” is a command for printing the data from the pattern buffer.

Syntax:

# sed -n ‘ADDRESS’p filename

# sed -n ‘/PATTERN/p’ filename

Create the txt file sample.txt that will be used in all examples below.

# cat sample.txt

1. Linux – Sysadmin, Scripting etc.

2. Databases – Oracle, mySQL etc.

3. Hardware

4. Security (Firewall, Network, Online Security etc)

5. Storage

6. Cool gadgets and websites

7. Productivity (Too many technologies to explore)

8. Website Design

9. Software Development

10.Windows- Sysadmin, reboot etc.

Sed Address Format 1: NUMBER

This will only match Nth line in the input.

#sed –n ‘N’p filename

For example, 3p prints 3rd line of the input file sample.txt as below

# sed -n ‘3’p thegeekstuff.txt

3. Hardware

Sed Address Format 2: NUMBER1~NUMBER2

M~N with “p” command prints every Nth line starting from line M.

# sed -n ‘M~N’p filename

For example, 3~2p prints every 2nd line starting from 3rd line as shown below.

# sed -n '3~2'p thegeekstuff.txt
3. Hardware
5. Storage
7. Productivity (Too many technologies to explore)
9. Software Development

Sed Address Format 3: START,END

M,N with “p” command prints Mth line to Nth line.

# sed -n ‘M,N’p filename

For example, 4,8p prints from 4th line to 8th line from input file thegeekstuff.txt

# sed -n ‘4,8’p thegeekstuff.txt

4. Security (Firewall, Network, Online Security etc)

5. Storage

6. Cool gadgets and websites

7. Productivity (Too many technologies to explore)

8. Website Design

Sed Address Format 4: ‘$’ Last Line

$ with “p” command matches only the last line from the input.

# sed -n ‘$’p filename

For example, $p prints only the last line as shown below.

# sed -n '$'p thegeekstuff.txt
10.Windows- Sysadmin, reboot etc.
 

Sed Address Format 5: NUMBER,$

N,$ with “p” command prints from Nth line to end of file.

# sed -n ‘N,$p’ filename

For example 4,$p prints from 4th line to end of file.

# sed -n '4,$p' thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 1: PATTERN

PATTERN could be unix regular expression. The below command prints only the line which matches the given pattern.

# sed -n /PATTERN/p filename

For example, following prints the line only which matches the pattern “Sysadmin”.

# sed -n /Sysadmin/p thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 2: /PATTERN/,ADDRESS

# sed -n ‘/PATTERN/,Np’ filename

For example, following prints lines which matches the pattern to Nth line, from input. 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.

# sed -n '/Hardware/,6p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites

Sed Pattern Format 3: ADDRESS,/PATTERN/

It prints from the Nth line of the input, to the line which matches the pattern. If the pattern doesnt match, it prints upto end of the input.

# sed -n ‘N,/PATTERN/p’ filename

For example, 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.

# sed -n '3,/Security/p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)

Sed Pattern Format 4: /PATTERN/,$

It prints from the line matches the given pattern to end of file.

# sed -n ‘/PATTERN/,$p’ filename

# sed -n '/Website/,$p' thegeekstuff.txt
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 5: /PATTERN/,+N

It prints the lines which matches the pattern and next N lines following the matched line.

# sed -n ‘/PATTERN/,+Np’ filename

For example, following prints the 5th line which matches the pattern /Storage/ and next two lines following /Storage/.

# sed -n '/Storage/,+2p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore)

Sed Pattern Format 6: /PATTERN/,/PATTERN/

Prints the section of file between two regular expression (including the matched line ).

# sed -n ‘/P1/,/P2/p’ filename

For example, 5th line matches “Storage” and 8th line matches “Design”, so it prints 5th to 8th.

# sed -n '/Storage/,/Design/p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore)
8. Website Design

Delete File Lines Using Address and pattern in Sed

“p” command prints the buffer (Remember to use “-n” option with “p”)

“d” command is just opposite, it’s for deletion. “d” will delete the pattern space buffer and immediately starts the next cycle.

Syntax:

# sed ‘ADDRESS’d filename

# sed /PATTERN/d filename

Delete the Nth line.

“Nd” delete the Nth line and prints the other lines.

For example, 3d deletes 3rd line and prints other lines.

$sed 3d sample.txt

Delete Starting from 3rd line and every 2nd line from there.

$sed ‘3~2d’ sample.txt

Delete from 4ht to 8th line from file

$sed ‘4,8d’ sample.txt

Delete the last line from input

$sed ‘$d’ sample.txt

Delete the line which matches the given pattern from input

$sed /Sysadmin/d sample.txt

Delete the line from which matches the given pattern to end of the file

$sed ‘/Website/ ,$d’ sample.txt

Delete the line from which matches the given pattern and 2 lines next to that

$sed ‘Storage/ ,+2d’ sample.txt

Delete blank line from a file using sed.

$sed ‘/^$/d’ sample.txt

Tagged : / / / / / / / / / / /

Unix Command: Grep – Quick Reference – Pattern – Examples – Options

unix-command-grep

Grep scans its input for a pattern, and can display the selected pattern, the line numbers of the filenames where the pattern occurs, The command uses the following syntax

grep options pattern filesname(s)

grep searches for pattern in one or more filenames.

Example for Grep command:

  1. grep “sales” emp.lst
  2. grep “director” emp1.lst emp2.lst
  3. grep ‘jai sharma’ emp.lst
  4. grep “jai Sharma $var” emp.lst

—————-Grep options—————————

Ignoring Case (i) When you look for a name, but you are not sure of the case, grep offers the –i (ignore) option which ignores case for patteen matching.

> Grep –i ‘agarwal’ emp.lst

Deleting Lines  or Inverse(-v): -v (inverse) option selects all except lines containing the pattern. Thus, you can create a file other list containing all but director.

> Grep –v “director” emp.lst > other list

Displaying line Numbers (-n): The –n(number) options displays the line numbers containing the pattern, along with the lines:

> grep –n ‘marketing’ emp.lst

Counting Line Containing patterns (-c): The –c (count) option counts the number of lines containing the pattern ( which is not the same as number of occurrences).

  1. grep –c director emp.lst
  2. grep –c director emp*.lst

Displaying Filenames (-l): The –l (list) option displays only the files names of files containing the pattern.

> grep –l ‘manager’ *.lst

Matching Multiple Patterns(-e): With the –e option, you can match the three agarwals by using grep like this:

> grep –e “Agarwal” –e “aggarwal” –e “agarwal” emp.lst

Taking patterns from a file (-f): we can place all patterns in a separate file, one pattern per lin. Grep takes inputs from there with the –f option:

> grep –f pattern.lst emp.lst

 

Tagged : / / / / / / / / / / / / /

SET UNIX HOME DIR PROPERTY using ANT

set-unix-home-dir-property-using-ant

Set properties HOMEDIR in build.xml which will be set through user logged in the current system..

Example:

<project name=”test” default=”myhome”>

    <property environment=”env”/>

    <target name=”myhome”>

        <echo message=”My home is ${env.HOME}”/>

    </target>

</project>

Tagged : / / / / / / / / / / / /

Basic of Unix – Presentation | Unix Learning Reference – Guide

unix-presentation

Basic of Unix Presentation

 

Basic Unix

View more presentations from rajeshatbuzz. (tags: basic of)
Tagged : / / / / / / / / / / /