0

I recently successfully added timestamps to my terminal prompt: https://stackoverflow.com/questions/61335641/bash-zsh-terminal-prompt-time-date/61346774#61346774

But i soon realized that what I actually want is a timestamp appended to the beginning of each output line of my terminal. Is there a simple For/Do statement I can add to my .bashrc file such that for any command, do (for example PrintF) %Y-%m-%d %H:%M:%S (this would be added to each new line of output printed in the terminal)?

where this:

2020-04-21 09:04:50 [purr@purr-ms7998 ~]$ sudo pacman -Syyu
:: Synchronizing package databases...
 core                               172.0 KiB   272 KiB/s 00:01 [----------------------------------] 100%
 extra                             2016.1 KiB   470 KiB/s 00:04 [----------------------------------] 100%
 community                            5.9 MiB   265 KiB/s 00:23 [----------------------------------] 100%
 multilib                           192.9 KiB   157 KiB/s 00:01 [----------------------------------] 100%
:: Starting full system upgrade...
 there is nothing to do
2020-04-21 09:05:44 [purr@purr-ms7998 ~]$ 

Would become this:

[2020-04-21 09:04:50] [purr@purr-ms7998 ~]$ sudo pacman -Syyu
[2020-04-21 09:04:50]:: Synchronizing package databases...
[2020-04-21 09:04:50] core                               172.0 KiB   272 KiB/s 00:01 [----------------------------------] 100%
[2020-04-21 09:04:50] extra                             2016.1 KiB   470 KiB/s 00:04 [----------------------------------] 100%
[2020-04-21 09:04:50] community                            5.9 MiB   265 KiB/s 00:23 [----------------------------------] 100%
[2020-04-21 09:04:50] multilib                           192.9 KiB   157 KiB/s 00:01 [----------------------------------] 100%
[2020-04-21 09:04:50]:: Starting full system upgrade...
[2020-04-21 09:04:50] there is nothing to do
[2020-04-21 09:05:44] [purr@purr-ms7998 ~]$ 

I am a complete beginner, so if a for/do statement could work for this purpose, please provide an example so that I can learn the correct syntax for what I'm trying to do.

I am currently using xfce4-terminal if that is of any use.

inxi:

System:    Kernel: 5.6.5-3-MANJARO x86_64 bits: 64 compiler: gcc v: 9.3.0 
           parameters: BOOT_IMAGE=/boot/vmlinuz-5.6-x86_64 
           root=UUID=98f63e52-6a5d-422b-88d0-73642c6c3ee8 rw quiet apparmor=1 security=apparmor 
           udev.log_priority=3 
           Desktop: Xfce 4.14.2 tk: Gtk 3.24.13 info: xfce4-panel wm: xfwm4 dm: LightDM 1.30.0 
           Distro: Manjaro Linux 
Info:      Processes: 204 Uptime: 47m Memory: 15.53 GiB used: 5.30 GiB (34.1%) Init: systemd v: 244 
           Compilers: gcc: 9.3.0 Shell: bash v: 5.0.16 running in: xfce4-terminal inxi: 3.0.37
  • I think the output between two prompts has nothing to do with bash. The output is written out by the program pacman in your case. To output time, pacman program should print out the time bash or terminal does not have control over the output while pacman is running. And that means that every other program would need to output it, too. In my opinion, this is impossible. Maybe you could run a script before pacman that would run in background and print out the time every n seconds. Then you would stop that script. To run such script all the time would be very annoying. – nobody Apr 21 at 16:30
  • I see, so since all the commands that I typically use are actually programs (i.e: pacman, ls, locate, youtube-dl, find, grep, etc.), you think I would have to run a script during execution of each program I run in the shell. So there is no way of overriding this since all these commands ultimately are executed in the shell? – Purry Apr 21 at 16:41
  • Seeing the other topic: unix.stackexchange.com/q/26728/117549, perhaps it’s possible with xargs? If so maybe I could just create aliases for all my commands that add the use of xargs to implement time stamps for every line of output? Any thoughts on this method? – Purry Apr 21 at 16:50
  • If so, The only problem is there were so many proposed solutions and I saw no examples of output from their suggestions. Also what files would I add these statements to, etc? – Purry Apr 21 at 17:01
1

You can install the package moreutils (upstream URL https://joeyh.name/code/moreutils/), I am pretty sure it exists on Arch Linux.

moreutils provides a ts command which timestamps every line coming from a program standart output. Ex:

 apt-get update | ts 
Apr 21 21:15:50 Get:1 http://security.debian.org buster/updates InRelease [65.4 kB]
Apr 21 21:15:50 Hit:2 http://deb.debian.org/debian buster InRelease
Apr 21 21:15:50 Get:3 http://deb.debian.org/debian buster-backports InRelease [46.7 kB]
Apr 21 21:15:51 Get:4 http://security.debian.org buster/updates/main amd64 Packages [187 kB]
Apr 21 21:15:51 Get:5 http://security.debian.org buster/updates/main Translation-en [100 kB]
Apr 21 21:15:51 Fetched 400 kB in 1s (438 kB/s)
Apr 21 21:15:54 Reading package lists...

Now it will not work automatically for every command as you asked but might solve the need for the most useful commands.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.