Skip to content

Latest commit

 

History

History
69 lines (40 loc) · 1.04 KB

add-current-date-and-time-to-filename.md

File metadata and controls

69 lines (40 loc) · 1.04 KB

Add current date and time to filename

Use date

Reference page on Arch Wiki:

--

example:

copy current aliases into text file with current date and time in filename

alias >> $(date +%Y%m%d-%H%M)-alias.txt

gives: 20210328-1739-alias.txt

or with seconds:

alias >> $(date +%Y%m%d-%H%M%S)-alias.txt

--

Script to append date and time to a filename, from user tatojo on StackOverflow https://stackoverflow.com/a/64018224 (slightly edited for my needs):

dateadd.sh

nano dateadd.sh

#!/bin/bash

now=$(date +"%Y%m%d-%H%M")
FILE="$1"
name="${FILE%.*}"
ext="${FILE##*.}"

# cp -v $FILE $name-$now.$ext
mv -v $FILE $name-$now.$ext

chmod +x dateadd.sh

run with

./dateadd.sh testfilename.txt

results

testfilename-20210328-1738.txt

can move into .local/bin/ so it is in the PATH to execute from anywhere?

cp dateadd.sh ~/.local/bin/dateadd

cd
touch testing.txt
dateadd testing.txt