From a547b9cee25fdffd3fb394ac488c01fbfe1a999e Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 2 Apr 2025 09:26:38 -0500 Subject: [PATCH] Add Create A Filename With The Current Date as a Unix TIL --- README.md | 3 +- ...create-a-filename-with-the-current-date.md | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 unix/create-a-filename-with-the-current-date.md diff --git a/README.md b/README.md index 67d51c6..f3b514c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1633 TILs and counting..._ +_1634 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1542,6 +1542,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Count The Number Of ripgrep Pattern Matches](unix/count-the-number-of-ripgrep-pattern-matches.md) - [Count The Number Of Words On A Webpage](unix/count-the-number-of-words-on-a-webpage.md) - [Create A File Descriptor with Process Substitution](unix/create-a-file-descriptor-with-process-substitution.md) +- [Create A Filename With The Current Date](unix/create-a-filename-with-the-current-date.md) - [Create A Sequence Of Values With A Step](unix/create-a-sequence-of-values-with-a-step.md) - [Curl With Cookies](unix/curl-with-cookies.md) - [Curling For Headers](unix/curling-for-headers.md) diff --git a/unix/create-a-filename-with-the-current-date.md b/unix/create-a-filename-with-the-current-date.md new file mode 100644 index 0000000..c8ae95c --- /dev/null +++ b/unix/create-a-filename-with-the-current-date.md @@ -0,0 +1,38 @@ +# Create A Filename With The Current Date + +I was recently working on a script to pull a scrubbed database dump using the +`pg_dump` Postgres utility. Ultimately, the script does something like this to +dump a remote database to a local file: + +```bash +pg_dump \ + -h host.region.rds.amazonaws.com \ + -U db_username \ + -d db_name \ + -F c \ + -f scrubbed-database-$(date +%Y-%m-%d).dump +``` + +Notice the last part of that command where we define the name of the dump file. +It has a `$(...)` that is used to run and interpolate a command as part of the +filename. + +Here is that `date` command run on its own: + +```bash +$ date +%Y-%m-%d +2025-04-02 +``` + +In the above command, that would mean if I were to run it today, I'd get +`scrubbed-database-2025-04-02.dump`. + +This approach can be used with any command where you are producing a file that +you want to be dated or timestamped. + +Here is another example that incorporates the time as well: + +```bash +$ touch $(date +%Y%m%d_%H%M%S)-migration.sql +# => 20250402_092442-migration.sql +```