LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-16-2004, 05:58 AM   #1
ewto
Member
 
Registered: Jan 2003
Location: Ohio, USA
Distribution: RH 9.0 / Fedora Core 3
Posts: 58

Rep: Reputation: 15
can I use date command with mkdir?


I'm trying to use the date command as part of a bash script to make a directory based on the date. for example, currently there is a line in the script that is:
Code:
mkdir /home/ewto/2004_docs/$NAM
The $NAM is inputted by the user with the read command a few lines up.
What I would like to do is change the script so that the
Code:
date '+%Y'
command would cause the directory I currently call 2004_docs, "2004" or whatever the year is based on the current year.
This will prevent me having to edit the script every year, and once I have it working right, I might even change the "year" directory to a month.year combo to further make it easier to organize.
 
Old 06-16-2004, 06:06 AM   #2
bahramH
Member
 
Registered: Apr 2004
Location: France
Distribution: Mandrake
Posts: 67

Rep: Reputation: 15
yes, just pust the result of date in your variable NAM :

> NAM=`date +%y`

and then
> mkdir /home/ewto/2004_docs/$NAM

note that you have to use ` and not ' to put the resutl of a command into a variable.
 
Old 06-16-2004, 06:15 AM   #3
ewto
Member
 
Registered: Jan 2003
Location: Ohio, USA
Distribution: RH 9.0 / Fedora Core 3
Posts: 58

Original Poster
Rep: Reputation: 15
Lightning fast response ...

Thanks for your time.

ewto
 
Old 06-16-2004, 07:23 AM   #4
ewto
Member
 
Registered: Jan 2003
Location: Ohio, USA
Distribution: RH 9.0 / Fedora Core 3
Posts: 58

Original Poster
Rep: Reputation: 15
little more help

ok ... messing with this a little, when I

Code:
#!/bin/bash
YR=`date +%Y`
echo type your name
read NAM
mkdir /home/ewto/cdburner/$YR/$NAM
I get an error telling me that there is no such directory...
I've tried several variations, but the only way it works is if the "year" directory is already created ... which doesn't seem to help much.

Any ideas?
 
Old 06-16-2004, 07:36 AM   #5
QuakerJ
LQ Newbie
 
Registered: Mar 2004
Location: Nottingham, UK
Distribution: Fedora14 - Gnome2
Posts: 15

Rep: Reputation: 0
Use -p flag on mkdir - that creates any directories not already in the path

mkdir -p /home/ewto/cdburner/$YR/$NAM

In friendship

QuakerJ
 
Old 06-16-2004, 07:40 AM   #6
bahramH
Member
 
Registered: Apr 2004
Location: France
Distribution: Mandrake
Posts: 67

Rep: Reputation: 15
Well, you cannot directly create a subdirectory of a directory when none of them exists.
This is not bash programming. So you can do the following :

#!/bin/bash
YR=`date +%Y`
echo type your name
read NAM
mkdir /home/ewto/cdburner/$YR
mkdir /home/ewto/cdburner/$YR/$NAM
 
Old 06-16-2004, 08:47 AM   #7
ewto
Member
 
Registered: Jan 2003
Location: Ohio, USA
Distribution: RH 9.0 / Fedora Core 3
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks for your time guys ... the -p switch was the solution I needed and it's working exactly as I envisioned now.

ewto
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'date' command format Neorio Linux - General 3 01-18-2011 05:18 AM
Trying to make a directory with mkdir using the date command. Altheastronut Linux - General 5 06-09-2009 12:14 PM
date command aetucker1 Linux - General 3 01-21-2005 12:57 PM
date command and the use of %Z tag hq4ever Linux - Newbie 2 09-04-2004 12:15 PM
mkdir command digitalgravy Linux - General 5 08-12-2004 01:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration