Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1051  / 3 Years ago, thu, june 17, 2021, 12:05:15

I want to create an alias to append a dated note to a file. (The use case I want to reproduce : Sebastian Daschner - How to do effective note taking as developer.


So far I was able to append the date, but I cannot figure out a way to append both the date and the note from user input.


Usage


append-to-file 'my text'

Expected result in file.txt


2021-08-30 : some previous text
2021-09-01 : my text

The alias I have written so far


alias append-to-file='tee -a file.txt <<< $(echo $(date -I) :)'

More From » bash

 Answers
0

You can do this with a little script. One way could be:


#!/bin/bash
echo "$(date -I) : $@" >> file.txt
tail -n 1 file.txt

The $@ variable stands for anything you entered at the command line. The tail command will echo the last line of the file to the screen.


Save this script in your ~/bin or in your .local/bin directory as append-to-file. Create the directory if it does not exist. Next time you log in, either of these directories will be included in your PATH. You then can enter the command anytime. What you enter will be saved in a file file.txt in the current working directory.


[#1229] Friday, June 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulild

Total Points: 239
Total Questions: 103
Total Answers: 112

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
fulild questions
Sat, Dec 11, 21, 20:10, 2 Years ago
Sat, Nov 19, 22, 16:28, 1 Year ago
Sat, Feb 25, 23, 22:59, 1 Year ago
Wed, Oct 12, 22, 04:04, 2 Years ago
Sun, Jul 31, 22, 08:07, 2 Years ago
;