Solved Bash: file name including date -time and spaces

Solved issue
Dear all,

This will be my last post: thanks for all the kind support, tips, your time and patience. This eventually ended up in this (test/learning)-script.
  1. The script is now located in ~/.local/bin # is this better place? both ~.bin and ~/.local/bin are automatically added to $PATH by ~/.profile (DEBIAN 12) if they exist
  2. I realized later - and forgot to mention it to you - that I want to have my file names both Linux and Windows-compatible. So the file name now does no longer include non-compatible characters like (<space>, plus sign, colon (from time stamp).
  3. 2 very valuable tips - not used here but tried - in one line:
    Code:
    date --iso-8601=seconds | cut -d'+' -f1
  4. I'm using https://www.shellcheck.net/ now
So I ended up with the following script:

Bash:
#!/bin/bash
#
# ask for meaningful start of the output filename
if [[ $# -eq 0 ]]; then
    read -p 'Begin file name with: ' start_name
else
    start_name="$1"
fi
if [[ -z "$start_name" ]]; then
    start_name=$'learning '
fi
# Directory to store capture files
capture_dir='/home/hans/Files/test'
for cnt in $(seq 1 10); do
    touch $capture_dir/$start_name\_$(date +"%Y-%m-%dT%H%M%S").txt
    sleep 3
done

Resulting in e.g. "troost_2025-04-08T160008.txt" and learning a lot for my 1st bash script.

Thanks again!
 




Follow Linux.org

Members online


Top