How to get a filename modified by attaching modified timestamp

Hi,

I want to modify a filename in AIX by attaching the last modified timestamp. I want the timestamp completely in numerical format (eg:200905081210. yr-2009, mnth - 05, date -08, hr - 12, mins - 10).

For example if the filename is a.log and it was modified on April 6th 2008 at 21.00. I should get the filename changed to a.log200804062100. Please advise ..

Thanks in advance!!!:slight_smile:

can anyone post a reply to this at the earliest.. I m struck here and not able to proceed with my coding without this..:frowning: thanks in advance..

what have you tried so far?

I didn get any way to get the timestamp in numerical format...

date +%G%d%H%M%S
200908011027

Thanks
NT

its the file's timestamp if i am not wrong, not the system time stamp.

Hi Ghostdog,

Yes you are correct, Sorry for not reading the question properly.
My post will simply give the current time stamp in numeric format.

Thanks
NT

let me clarify again.. my intention is whenever a logfile gets modified, attach the modified timestamp and store the file into the same dir so that it will not overwrite the old ones. I need it in numerical form for some other manipulations later.. hope this clarifies..

you can try something like

ls -l filename
-rwxr-xr-x   1 root         other         29 May  8 10:07 filename

cp filename filename.`ls -l filename|awk '{print $8$6$7}'`

ls -l filename*
-rwxr-xr-x   1 root         other         29 May  8 10:07 filename
-rwxr-xr-x   1 root         other         29 May  8 10:07 filename.10:07May8

just noticed your remit, its not all the way there but a start :frowning:

Thanks tag..
Bt I may want to hav the timestamp completely in numberformat.. :frowning:
I have heard of some option called -mtime.. will it help me here?

try this

ls -l --time-style=+%Y%m%d%H%M%S | awk '{print $7$6}'

hi .. could u pls paste me the test o/p too.. i m nt abl to run this.. :frowning:

[root@Reddyraja test]# ls -l
total 0
-rw-r--r-- 1 root root 0 2009-05-08 18:39 test1.log
-rw-r--r-- 1 root root 0 2009-05-08 18:39 test.log
[root@Reddyraja test]# ls -l --time-style=+%Y%m%d%H%M%S
total 0
-rw-r--r-- 1 root root 0 20090508183914 test1.log
-rw-r--r-- 1 root root 0 20090508183903 test.log

[root@Reddyraja test]# ls -l --time-style=+%Y%m%d%H%M%S | awk 'NR>1{print $7,$7$6}' | while read line; do mv `echo $line` ; done

[root@Reddyraja test]# ls -l --time-style=+%Y%m%d%H%M%S
total 0
-rw-r--r-- 1 root root 0 20090508183914 test1.log20090508183914
-rw-r--r-- 1 root root 0 20090508183903 test.log20090508183903

[root@Reddyraja test]# ls
test1.log20090508183914 test.log20090508183903

Hi.. I am not sure whether u r using some other flavours of Unix. In AIX i dont get the out put of ls in number format as u... i mean the month will be in letters . Also when i try to run this cmd

ls -l --time -style=+%Y%m%d%H%M%S | awk '{print $7$6}'

its giving me error..

$ ls -l --time -style=+%Y%m%d%H%M%S | awk '{print $7$6}'
ls: illegal option -- - staff 512 Jan 19 2008 x.dt.old.old
ls: illegal option -- y staff 47 May 4 12:14
ls: illegal option -- =
ls: illegal option -- +
ls: illegal option -- %
ls: illegal option -- Y
ls: illegal option -- %
ls: illegal option -- %
ls: illegal option -- %
ls: illegal option -- H
ls: illegal option -- %
ls: illegal option -- M
ls: illegal option -- %
ls: illegal option -- S
usage: ls [-1ACFLNRabcdefgilmnopqrstux] [File...]

am i doing anythin wrong?? :rolleyes:

no space between --time and -style use --time-style
ls -l --time -style=+%Y%m%d%H%M%S | awk '{print $7$6}'

[root@Reddyraja test]# ls --version
ls (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard Stallman and David MacKenzie.

[root@Reddyraja test]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information)
with -l: show ctime and sort by name
otherwise: sort by ctime
-C list entries by columns
--color[=WHEN] control whether color is used to distinguish file
types. WHEN may be `never', `always', or `auto'
-d, --directory list directory entries instead of contents,
and do not dereference symbolic links
-D, --dired generate output designed for Emacs' dired mode
-f do not sort, enable -aU, disable -ls --color
-F, --classify append indicator (one of /=>@|) to entries
--file-type likewise, except do not append `
'
--format=WORD across -x, commas -m, horizontal -x, long -l,
single-column -1, verbose -l, vertical -C
--full-time like -l --time-style=full-iso
-g like -l, but do not list owner
--group-directories-first
group directories before files
-G, --no-group in a long listing, don't print group names
-h, --human-readable with -l, print sizes in human readable format
(e.g., 1K 234M 2G)
--si likewise, but use powers of 1000 not 1024
-H, --dereference-command-line
follow symbolic links listed on the command line
--dereference-command-line-symlink-to-dir
follow each command line symbolic link
that points to a directory
--hide=PATTERN do not list implied entries matching shell PATTERN
(overridden by -a or -A)
--indicator-style=WORD append indicator with style WORD to entry names:
none (default), slash (-p),
file-type (--file-type), classify (-F)
-i, --inode print the index number of each file
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN
-k like --block-size=1K
-l use a long listing format
-L, --dereference when showing file information for a symbolic
link, show information for the file the link
references rather than for the link itself
-m fill width with a comma separated list of entries
-n, --numeric-uid-gid like -l, but list numeric user and group IDs
-N, --literal print raw entry names (don't treat e.g. control
characters specially)
-o like -l, but do not list group information
-p, --indicator-style=slash
append / indicator to directories
-q, --hide-control-chars print ? instead of non graphic characters
--show-control-chars show non graphic characters as-is (default
unless program is `ls' and output is a terminal)
-Q, --quote-name enclose entry names in double quotes
--quoting-style=WORD use quoting style WORD for entry names:
literal, locale, shell, shell-always, c, escape
-r, --reverse reverse order while sorting
-R, --recursive list subdirectories recursively
-s, --size print the size of each file, in blocks
-S sort by file size
--sort=WORD sort by WORD instead of name: none -U,
extension -X, size -S, time -t, version -v
--time=WORD with -l, show time as WORD instead of modification
time: atime -u, access -u, use -u, ctime -c,
or status -c; use specified time as sort key
if --sort=time
--time-style=STYLE with -l, show times using style STYLE:
full-iso, long-iso, iso, locale, +FORMAT.
FORMAT is interpreted like `date'; if FORMAT is
FORMAT1<newline>FORMAT2, FORMAT1 applies to
non-recent files and FORMAT2 to recent files;
if STYLE is prefixed with `posix-', STYLE
takes effect only outside the POSIX locale
-t sort by modification time
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time
with -l: show access time and sort by name
otherwise: sort by access time
-U do not sort; list entries in directory order
-v sort by version
-w, --width=COLS assume screen width instead of current value
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-1 list one file per line

SELINUX options:

--lcontext Display security context. Enable -l. Lines
will probably be too wide for most displays.
-Z, --context Display security context so it fits on most
displays. Displays only mode, user, group,
security context and file name.
--scontext Display only security context and file name.
--help display this help and exit
--version output version information and exit

~$ ls -la file*
-rw-r--r-- 1 leen leen 0 2009-06-02 22:02 file

~$ mv file file_`ls -l --time-style=+%G%d%H%M%S |awk '{print $6}'|tail -1`

~$ ls -la file*
-rw-r--r-- 1 leen leen 0 2009-06-02 22:02 file_200903201848

Something like that?

mv siddhesh siddh_new.`date +%Y%m%d`

I have tested the same, and it works properly fine.

-----Post Update-----

I have tested this command and it works fine.

mv siddhesh siddh_new.`date +%Y%m%d`

Regards,
SIddhesh