Bash Alias Create File with Current Timestamp in Filename

Bash alias create file with current timestamp in filename

Use single quotes to prevent immediate expansion.

alias unix='echo $(date +%s)'

Update: While I'm happy to have been able to explain the different expansion behavior between single and double quotes, please also see the other answer, by Robby Cornelissen, for a more efficient way to get the same result. The echo is unnecessary here since it only outputs what date already would output by itself. Therefore, date doesn't need to be run in a subshell.

How to name a file with current time?

Use command substitution to capture date's output as a string.

echo 'hello' > "$(date +%T)".txt

Adding timestamp to a filename with mv in BASH

The few lines you posted from your script look okay to me. It's probably something a bit deeper.

You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.

BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.

EDIT

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

Two ways:

  1. Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
  2. To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.

SVN: Creating alias that allows filename variable

Solved by @jszakmeister:

Plugged: svnhist() { svn log -q "$@" | grep '^r' | cut -f1 -d' ' ; }

Into .bash_profile

zsh alias not creating file properly

Remove fileName. That's a placeholder for the file name to create, which in your case is $1.

create() {
touch $1
}

Having issues getting an alias or bash function to force chef-client to alway log

Just put this in the config file (/etc/chef/client.rb):

require "time"
log_location "/var/log/chef/chef-client-run/#{Time.now.iso8601}.log"

Or something like that, adjust the formatting of the timestamp as desired.

Also, if you want to track changes, I wouldn't use the log output. Make an event handler and track the actual data you want.

Linux Script to redirect output to log file with date filename

You can use date to choose the format of the log file. Assuming YYYY-MM-DD, you can use the following. Note using '>>' to append/create the log file.

java abc.java >> "logfile.$(date +'%Y-%m-%d').log"
# Test
echo abc.java >> "logfile.$(date +'%Y-%m-%d').log"

Also note that 'java abc.java' need to be reviewed. The java command is usually invoked with class name (java abc), and not the name of a file.

Creating alias in Linux to a file or directory whose path changes with time

You could try somethink like that :

alias my_alias=$(echo $OUT_HOME/logs/misc/$(date +"%Y-%m-%d-%H-%M-%S-%4N")-machine_name/commands/logfile.txt)


Related Topics



Leave a reply



Submit