Creating a directory based on date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a directory based on date
# 1  
Old 08-20-2015
Creating a directory based on date

I want to create a directory based on date. I do not have date -d command. What I do have is:

either use "date" command to get current date

or
Code:
 DATE1=$(perl -e 'print scalar(localtime(time - 1 )), "\n";' |awk '{print $2,$3,$5}') |awk '{print $3}'

can be modified to produce the desired year.

Then as soon as the year changes to 2016 I want the mkdir command in a script to then do a mkdir 2016, and 2017 and so on as the year increments in the current directory. So there will be another year folder each time the new year comes around.

Is this possible?

So far I now have this:
Code:
 if [[ $d == 2014 ]]; then x=$(($d+1)); mkdir $x; fi

it is a good start, but I want it to continue incrementing for each year

Last edited by newbie2010; 08-20-2015 at 04:53 PM..
# 2  
Old 08-20-2015
You say you don't have
Code:
date -d

Do you have the date command without the -d option? If you do, something like
Code:
$ date "+%Y"
2015
$

does the trick on my system. "man date" and search for "format" for other options. Adding something like
Code:
0 0 1 1 * mkdir <some-path>/`date "+%Y"`

to your root crontab ought to do the trick. You can stack the `date` format options to get something like foobar/2015/08/20/ .. etc

Last edited by featheredfrog; 08-20-2015 at 05:10 PM..
These 2 Users Gave Thanks to featheredfrog For This Post:
# 3  
Old 08-20-2015
Not sure if this is what you want...
OSX 10.7.5, default bash terminal.
Just loop it as required and modify to suit...
Code:
#!/bin/bash
# newdir
YEARPATH="/Users/barrywalker/"
DATE=( $(date) )
if [ -e "$YEARPATH${DATE[3]}" ]
then
	echo "Drawer exists..."
	sleep 1
else
	mkdir "$YEARPATH${DATE[3]}"
fi
exit 0

Results...
Code:
Last login: Thu Aug 20 21:12:56 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 newdir
AMIGA:barrywalker~/Desktop/Code/Shell> ls /Users/barrywalker/
03080.sh		AAWG			Movies
03083.sh		AudioScope.sh		Music
03090.sh		AudioScope.sh.txt	Pictures
03100.sh		AudioScope.sh~		Public
03120.sh		AudioScope.txt		Scope
03140.sh		BW_HELP			URL
03180.sh		Desktop			listing.txt
03197.sh		Documents		sox-14.4.0
03220.sh		Downloads
03220.txt		Library
AMIGA:barrywalker~/Desktop/Code/Shell> ./newdir
AMIGA:barrywalker~/Desktop/Code/Shell> ls /Users/barrywalker/
03080.sh		2015			Library
03083.sh		AAWG			Movies
03090.sh		AudioScope.sh		Music
03100.sh		AudioScope.sh.txt	Pictures
03120.sh		AudioScope.sh~		Public
03140.sh		AudioScope.txt		Scope
03180.sh		BW_HELP			URL
03197.sh		Desktop			listing.txt
03220.sh		Documents		sox-14.4.0
03220.txt		Downloads
AMIGA:barrywalker~/Desktop/Code/Shell> ./newdir
Drawer exists...
AMIGA:barrywalker~/Desktop/Code/Shell> ./newdir
Drawer exists...
AMIGA:barrywalker~/Desktop/Code/Shell> ./newdir
Drawer exists...
AMIGA:barrywalker~/Desktop/Code/Shell> _

Notice the 2015 drawer now exists and is ignored until the next year is reached.
# 4  
Old 08-20-2015
With a recent bash, you could use the %(...)T format string. And, why don't you just bluntly run mkdir and ignore the error msg?
Code:
 mkdir $(printf "%(%Y)T\n")
mkdir: cannot create directory ‘2015’: File exists

It will create the 2016 directory when time comes.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Searching for a files based on current date directory

Hi All, I've been trying to do some recursive searching but not been very successful. Can someone please help. Scenario: I have directory structure /dir1/dir2/dir3/ 2019/ 11/ 17 18 19 20 so what I want to do is run a script and as its 2019/11/18/ today it would go and only search... (3 Replies)
Discussion started by: israr75
3 Replies

2. UNIX for Dummies Questions & Answers

Unable to find files, those can be present anywhere in the directory tree,based on its creation date

Hi I am unable to find files, those are present anywhere in the same directory tree, based on the creation date. I need to find the files with their path, as I need to create them in another location and move them. I need some help with a script that may do the job. Please help (2 Replies)
Discussion started by: sam192837465
2 Replies

3. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

4. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

5. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

6. Shell Programming and Scripting

creating a file name based on date

I need to automate a weekly process of piping a directory list to a csv file. Normally I do ls -l > files_04182010.csv (04182010 being the date..) Can someome show me how I would script this, so that when the script is ran it grabs the current date and formats it and allows me to use that... (8 Replies)
Discussion started by: jeffs42885
8 Replies

7. Shell Programming and Scripting

Need script to select multiple files from archive directory based on the date range

hi all, here is the description to my problem. input parameters: $date1 & $date2 based on the range i need to select the archived files from the archived directory and moved them in to working directory. can u please help me in writing the code to select the multiple files based on the... (3 Replies)
Discussion started by: bbc17484
3 Replies

8. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

9. Shell Programming and Scripting

shell script to selectively tar directory based on date

hye everybody :) , i'm new to the scripting world.. hope you guys can help me out with this one.. i'm trying to identify any directory under /tmp/saya that is created more than one day from the current date.. e.g, today is March 14, so any directory that has time stamp March 13 backwards, i... (2 Replies)
Discussion started by: fara_aris
2 Replies

10. Shell Programming and Scripting

Shell script for Creating Directory with name as system date

Dear Sir/Madam, I need a bit of your help. The problem is as follows : I have to create a directory in unix whose name is that of system date in the dd_mon_yyyy format . I am able to extract a date in required format ina variable , but when i'm using this variable in mkdir it is not... (7 Replies)
Discussion started by: aarora_98
7 Replies
Login or Register to Ask a Question