I want to display the date modified of a file that is in another directory.
e.g. I am in /some/directory and I grep -rHl "foo" which returns a list of files. I am curious about the date modified of /a/completely/different/directory/result.txt without having to go to that directory and list the files.
Can this be done?
Could use stat from GNU Coreutils:
stat -c %y /path/to/file
output:
2020-12-08 15:43:01.306251716 +0100
Or ls from GNU Coreutils:
ls --full-time /path/to/file
output:
rw------- 1 user user 759 2020-12-08 15:43:01.306251716 +0100 /path/to/file
Related
I am using zsh 4.3.6. I would like to have the timestamp in the command line history. The history -i always shows the current time:
> history -i
498 2020-04-27 14:54 history -i
499 2020-04-27 14:54 cat ~/.zsh_history
500 2020-04-27 14:54 exit
It seems that the timestamp is not stored in the $HISTFILE:
> cat $HISTFILE
ls
zsh --version
history -i
How can I have the persistent command line history timestamp?
Thanks.
setopt EXTENDED_HISTORY # puts timestamps in the history
or
setopt extendedhistory
to ~/.zshrc
Output of a command looks something like this
# ls -l abc.zip
lrwxrwxrwx 1 sri dba 122 Mar 27 23:37 /a/b/c/abc.zip -> /x/y/z/abc.zip
I need to extract/cut the whole path which comes after ->. I have used cut -f -d but its not working some times, I guess column number is changing. So I need sed equivalent of this.
What you are reading is the information about a symlink. Parsing ls is definitely a bad idea.
What about using readlink instead? It does read value of a symbolic link:
readlink abc.zip
Or with -f for the full path:
readlink -f abc.zip
See an example:
$ touch a
$ ln -s a b # we create a symbolic link "b" to the file "a"
$ ls -l b
lrwxrwxrwx 1 me me 1 Apr 6 09:54 b -> a
$ readlink -f "b" # we check the full "destination" of the symlink "b"
/home/me/a
$ readlink "b" # we check the "destination" of the symlink "b"
a
This might work for you (GNU sed):
sed -n 's/^l.*->\s*//p' file
Using 'cut' you can only specify single character delimiters.
For string delimiters an easy and clear option is awk. In your case try:
ls -ls abc.zip | awk -F '->' '{print $2}'
My program goes as shown below where $i is a file.
TIMESTAMP=`ls -ltr $i | awk '{print $6,$7,$8;}'`
Output of this is Jan 6 12:13.
For sorting based on the date, I need to convert it to the format DD/MM/YY HH:MM.
Also please suggest if there is a way of sorting the files with the timestamp in the format Jan 6 12:13.
I am currently using Korn shell. Solaris 5.10.
Under absolutely no circumstances, parse ls(1) output!
Instead, use the right tool for the job. If you need to sort ls output, use ls’ various sort options. If you need to do other processing, make use of a tool… in the Solaris 8 installation I have access to, GNU stat is installed, which makes this easy:
tg#stinky:~ $ stat -c '%y %n' /bin/[ck]sh
2008-07-08 14:16:07.000000000 +0200 /bin/csh
2008-06-10 16:28:32.000000000 +0200 /bin/ksh
tg#stinky:~ $ uname -a
SunOS stinky 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Fire-V240 Solaris
Otherwise, you could use scripting languages with stat(2) access, such as Perl, to display times for pathnames, like this (be aware of newlines in filenames, though):
tg#stinky:~ $ find /bin/[ck]sh | perl -MPOSIX -ne 'chomp; print POSIX::strftime("%d/%m/%Y %H:%M\n",localtime((stat)[9]));'
08/07/2008 14:16
10/06/2008 16:28
But, as others already pointed out in comments, %Y-%m-%d is indeed easier to sort.
The Korn Shell does not have any built-in functions for time manipulation.
The “magic chars in filenames”-safe version of this (also tested under Solaris 8) is:
find /bin/[ck]sh -print0 | perl -0 -MPOSIX -ne 'chomp; print POSIX::strftime("%d/%m/%Y %H:%M\n",localtime((stat)[9]));'
Of course, the find /bin/[ck]sh part is just an example, you can feed any pathname list you have to the command.
How can I pipe a text stream into a file and, while the file is still in use, wipe it for job rotation?
Long version:
I've been struggling for a while onto an apparently minor issue, that's making my experiments impossible to continue.
I have a software collecting data continuously from external hardware (radiotelescope project) and storing in a csv format. Being the installation at a remote location I would, once a day, copy the saved data in a secure place and wipe the file content while, for the same reason, I can NOT to stop the hardware/software, thus software such as log rotation wouldn't be an option.
For as much effort spent see my previous post, it seems the wiped file keeps growing although empty.
Bizarre behavior, showing file size, truncate file, show file size again:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv;truncate radio.csv -s 1; ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 994277 Jan 18 21:32 radio.csv
252 radio.csv
-rw-r--r-- 1 pi pi 1 Jan 18 21:32 radio.csv
0 radio.csv
Then, as soon as more data comes in:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 1011130 Jan 18 21:32 radio.csv
24 radio.csv
I thought to pipe the output into a sed command and save right away, with no luck altogether. Also, filesystem/hardware doesn't seems buggy (tried different hardware/distro/filesystem).
Would anyone be so nice to give me a hint how to proceed?
Thank you in advance.
Piped into tee with -a option. The file was kept open by originating source.
Option APPEND of tee helped to stick at the EOF new data; in the given issue, appending to the beginning when file zeroed.
For search engine and future reference:
sudo rtl_power -f 88M:108M:10k -g 1 - | tee -a radio.csv -
Now emptying the file with
echo -n > radio.csv
gets the file zeroed as expected.
I am new to accurev, used to use SVN earlier. I want to a get diff file consisting of all the changes in kept files in a given directory. I know ac diff -b <file>
gives diff in a file, but if I have many files and I want the diff of all the kept files in a given directory, is there a straight forward command to do this like svn diff?
You are going to need to create a script if you only want to diff kept files in a given directory. Basically you will run an 'accurev stat -k' -> parse output for given directory -> 'accurev diff -b'
On a *NIX machine the commands below work nicely.
The -k option to AccuRev's stat command says find the file with "(kept)" status. Using the -fal options to stat provides just the Depot relative pathway to the file. No addition filtering needed. So the command line would be:
accurev stat -k -fal | xargs accurev diff -b
Produces output like:
accurev stat -k -fal | xargs accurev diff -b
diffing element /./COPYING
341a342
> Tue Mar 18 08:38:39 EDT 2014
> Change for demo purposes.
diffing element /./INSTALL
3a4,7
> New Change
>
> Another Change
>
Dave