Need help in Bashscript for monthwise - bash

I've a directory name date which contains files from jan to dec, for example file name jan.sh, feb.sh until dec.sh. Now I want to make a bash script that will copy each file to xyz directory on the first day of each month. On jan it will copy jan.sh and on feb.sh and so on until the month of dec.

CURRENT_MONTH=$(date "+%b") # e.g. The display depends on locale (LANG environment variable)
cp "${CURRENT_MONTH}.sh" destination_dir # Adjust the formatting of source name according to the display of date

Related

.bash_history file has no timestamp

The /home/foo/.bash_history files contains no timestamp.
The root's .bash_history file has a timestamp.
How can I enable the timestamping in the users files?
BR
Christian
You can define variable HISTTIMEFORMAT to time format (see strftime):
Add next linea ~/.profile:
export HISTTIMEFORMAT=%F
From this point all history have time

Having Issue with file name appended with date -shell scripting

I tried to append the current day and time to the existing file name in shell scripting and I found my command is not working as expected.
For example, if my file name is f1.log and I nees to append it along with current time. This appended version must be used for further processing of the file.
I tried with the following script but getting an error
now=$(date +"%m-%d-%Y/%T")
echo hi >>time.log
mv "time.log" "time.$now.log" (error here : file or directory not found)
echo hello >> time.log$now (have to continue processing with new file)
You cannot have a / character in a filename. The mv command is looking for a directory named with the minute, day, and year of the output of date and trying to create a file named by the time. Just change your format to not include / in the filename.
The problem is with shell's interpertation of / in your date +"%m-%d-%Y/%T".
Change it to a - instead (or something else, as long as it's not / or another meta character that will make the files difficult to work with in the future)

How to update a program with a daily changing file name?

I have a program that reads in a file and does some parsing to it. The file is generated by another program every night. Due to the ICD the date is part of the file name.
The file name changes every night due to the date change so I am not sure how to have my program change the fileIn name to accommodate this.
If the current fileIn is:
in20120103out.dat
Tomorrow's fileIn is:
in20120104out.dat
filename = Time.now.strftime("in%Y%m%dout.dat")
or
require 'date'
filename = Date.today.strftime("in%Y%m%dout.dat")
strftime means string format for time. The %-parameters are placeholder for Year (Y), month (m), day (d). (There are more placeholders, e.g. year without century...)
You can use the strftime() method to generate the date portion of the filename:
t = Time.­now
filename = "in#{t.strftime('%Y%m%d')}out.dat"
And then use the filename variable to open the file as normal.

Compress a file with todays date

I want to compress a file with name ASCOnnect200_V5.0.0.0_09_Dec_11.zip. I it possible with winrar/rar?
The file name ASCOnnect200_V5.0.0.0_ will be always constant and the date part will be in the format dd-mmm-yy.
In cmd:
rar.exe a -ag+YYYYMMDD arc {FolderName}
The answer is in this forum :)

Batch command to move files to a new directory

I want to write a batch job that when executed will grab all the files in the C:\Test\Log folder and move them to a new directory in the C:\Test. This new directory will have a name called "Backup-" and CURRENT DATE.
So once completed, the log folder should be empty with all the files now located in the new folder.
I know I would have to use the MOVE command, but have no idea how to dynamically create a new folder, and use the date to name it.
Something like this might help:
SET Today=%Date:~10,4%%Date:~4,2%%Date:~7,2%
mkdir C:\Test\Backup-%Today%
move C:\Test\Log\*.* C:\Test\Backup-%Today%\
SET Today=
The important part is the first line. It takes the output of the internal DATE value and parses it into an environmental variable named Today, in the format CCYYMMDD, as in '20110407`.
The %Date:~10,4% says to extract a *substring of the Date environmental variable 'Thu 04/07/2011' (built in - type echo %Date% at a command prompt) starting at position 10 for 4 characters (2011). It then concatenates another substring of Date: starting at position 4 for 2 chars (04), and then concats two additional characters starting at position 7 (07).
*The substring value starting points are 0-based.
You may need to adjust these values depending on the date format in your locale, but this should give you a starting point.
this will also work, if you like
xcopy C:\Test\Log "c:\Test\Backup-%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%" /s /i
del C:\Test\Log

Resources