comparing two files last modification date - shell
This is a discussion on comparing two files last modification date - shell ; I have two files, say file1 and file2, I would like to find out which of these two files was modified last. Thanks...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| of these two files was modified last. Thanks |
|
#2
| |||
| |||
|
puzzlecracker wrote: > I have two files, say file1 and file2, I would like to find out which > of these two files was modified last. You can inspect that roughly with ls -l If you want it with higher precision have a look at the command stat. For visual inspection with user friendly dates use stat -x%y file1 file2 and if you want to do arithmetic use the output values (seconds) from stat -x%Y file1 file2 Janis > > Thanks |
|
#3
| |||
| |||
|
On Nov 13, 9:49*am, Janis Papanagnou wrote: > puzzlecracker wrote: > > I have two files, say file1 and file2, I would like to find out which > > of these two files was modified last. > > You can inspect that roughly with ls -l > If you want it with higher precision have a look at the command stat. > For visual inspection with user friendly dates use > > * *stat -x%y file1 file2 > > and if you want to do arithmetic use the output values (seconds) from > > * *stat -x%Y file1 file2 > > Janis > > > > > Thanks argh stat: invalid option -- x |
|
#4
| |||
| |||
|
On Nov 13, 10:13*am, puzzlecracker > On Nov 13, 9:49*am, Janis Papanagnou > wrote: > > > > > puzzlecracker wrote: > > > I have two files, say file1 and file2, I would like to find out which > > > of these two files was modified last. > > > You can inspect that roughly with ls -l > > If you want it with higher precision have a look at the command stat. > > For visual inspection with user friendly dates use > > > * *stat -x%y file1 file2 > > > and if you want to do arithmetic use the output values (seconds) from > > > * *stat -x%Y file1 file2 > > > Janis > > > > Thanks > > argh stat: invalid option -- x Try "man stat" on your system (e.g. on an Ubuntu system here stat has a "-c" option instead of a "-x" option). |
|
#5
| |||
| |||
|
2008-11-13, 06:35(-08), puzzlecracker: > I have two files, say file1 and file2, I would like to find out which > of these two files was modified last. [...] Some shells (ksh, zsh, bash) have [[ file1 -nt file2 ]]. In the standard toolchest, you've got "find -newer" and "ls -t" if find file1 -prune -newer file2 -print | grep -q .; then echo "file1 is newer than file2" fi Or: NL=' ' case $(ls -td file1 file2) in ("file1${NL}file2") echo "file1 is not older than file2";; ("file2${NL}file1") echo "file2 is not older than file1";; (*) echo >&2 error;; esac -- Stéphane |
|
#6
| |||
| |||
|
puzzlecracker wrote: > On Nov 13, 9:49 am, Janis Papanagnou > wrote: > >>puzzlecracker wrote: >> >>>I have two files, say file1 and file2, I would like to find out which >>>of these two files was modified last. >> >>You can inspect that roughly with ls -l >>If you want it with higher precision have a look at the command stat. >>For visual inspection with user friendly dates use >> >> stat -x%y file1 file2 >> >>and if you want to do arithmetic use the output values (seconds) from >> >> stat -x%Y file1 file2 >> >>Janis >> >> >> >> >>>Thanks > > > argh stat: invalid option -- x Sorry, mistyped! I meant -c (x and c are adjacent on my keyboard). Janis |
|
#7
| |||
| |||
|
On Nov 13, 10:28*am, Stephane CHAZELAS wrote: > 2008-11-13, 06:35(-08), puzzlecracker:> I have two files, say file1 and file2, I would like to find out which > > of these two files was modified last. > > [...] > > Some shells (ksh, zsh, bash) have [[ file1 -nt file2 ]]. > > In the standard toolchest, you've got "find -newer" and "ls -t" > > if find file1 -prune -newer file2 -print | grep -q .; then > * echo "file1 is newer than file2" > fi > > Or: > > NL=' > ' > case $(ls -td file1 file2) in > * ("file1${NL}file2") echo "file1 is not older than file2";; > * ("file2${NL}file1") echo "file2 is not older than file1";; > * (*) echo >&2 error;; > esac > > -- > Stéphane Thanks the following doesnt quite do what I want. First I want to see if file exist and if it does, I want to compare if -e $obj || find $i -prune -newer $obj -print | grep -q . ; then fi Thanks |
|
#8
| |||
| |||
|
puzzlecracker wrote: > > Thanks the following doesnt quite do what I want. First I want to see > if file exist and if it does, I want to compare > > if -e $obj || find $i -prune -newer $obj -print | grep -q . ; if [[ -e $obj ]] || ... Or use single [...] instead of [[...]] in case you use an old shell. Janis > then > > > fi > > > Thanks |
|
#9
| |||
| |||
|
2008-11-13, 07:51(-08), puzzlecracker: [...] > Thanks the following doesnt quite do what I want. First I want to see > if file exist and if it does, I want to compare > > if -e $obj || find $i -prune -newer $obj -print | grep -q . ; > then > > > fi [...] -e is not a valid command. The command that does tests is the "[" command (aka test). "||" means OR, you want "&&" for AND. You've forgotten the quotes around the variable expansions. But you don't need to run the "[" command anyway, you can as well do: if find "$i" -prune -newer "$obj" -print 2> /dev/null | grep -q . then ... fi find will not output anything on its standard output if it can't find "$i", so the test will fail. Note that the above will fail to work properly if "$i" starts with a "-". -- Stéphane |
|
#10
| |||
| |||
|
2008-11-13, 17:12(+01), Janis Papanagnou: > puzzlecracker wrote: >> >> Thanks the following doesnt quite do what I want. First I want to see >> if file exist and if it does, I want to compare >> >> if -e $obj || find $i -prune -newer $obj -print | grep -q . ; > > if [[ -e $obj ]] || ... > > Or use single [...] instead of [[...]] in case you use an old shell. [...] Or in case you want to write a standard script that can run with any POSIX compliant shell, which in general I'd recommend. [ -e "$obj" ] or ls -Ld -- "$obj" > /dev/null 2>&1 Note that [ -e will return false for a symlink to a non-existent file (or a file in a directory you don't have access to). The equivalent of ls -d -- "$obj" > /dev/null 2>&1 would be: [ -e "$obj" ] || [ -L "$obj" ]. Another note on symlinks. find x -newer y and ls -t x y will compare the dates of x and y even if x or y is a symlink. While ls -tL x y, find -H x -newer y and [[ x -nt y ]] will compare the dates of the targets of the symlinks. -- Stéphane |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT -4. The time now is 10:39 PM.




Linear Mode