Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
cd "$(mkdir -v "$(date -R)"|sed s+.`(.*)'.*)"

This is meant to create a dir named $(date -R). I could simply cd $(date -R), but the culprit is the date could change since the previous command was run (mkdir).

So I want to parse its output to determine the name of the dir created. How do I correct the code? Currently it displays > prompt, indicating that a quote is missing.

Changing ' to \' doesn't alter the result. If you have a better way to do this, please say so.

share|improve this question
I've merged your unregistered accounts for you and forced conversion of your answers to comments. You should now be able to accept and comment on this answer - until you lose your cookies again. As such, you might want to consider registering, otherwise please be aware that you can recover an unregistered account as log as you've provided a valid email address. – DMA57361 Aug 4 '11 at 9:36

IMHO you are making something simple needlessly complex. Why not just do something simple like.

NEWDIR=$(date -R);mkdir "$NEWDIR";cd "$NEWDIR"
share|improve this answer
Your solution works great, and I accept your answer. However, I'm still left wondering how to fix my code, out of curiosity. – nnbvcn Aug 4 '11 at 8:41
My 2c: (NEWDIR=$(date -R); mkdir "$NEWDIR" && cd "$NEWDIR") That doesn't clutter the environment.. – estani May 22 '14 at 16:13

With bash, you can use history expansion too:

mkdir "$(date -R)" && cd !#:^
share|improve this answer

The sed part in your command is rather broken. Try this:

sed -e "s/.*\`\(.*\)'.*/\1/"

If you frequently want to mkdir and cd in one step, try this (bash-only) function:

mdc () { mkdir "$@" && cd "${!#}"; }

It just wraps around mkdir. Use it like mdc [mkdir options] newdir and make sure newdir goes last.

share|improve this answer
I forgot about the second part; it should be: cd $(mkdir -v "$(date -R)"|sed -r s+.*`(.*)'.*+\1+)" I tried your variant, but it produces the same result when I put it in: cd "$(mkdir -v $(date -R)| sed -e s/.*`(.*)'.*/\1/)" Your mdc () works great. I would comment, but since I didn't sign up, I can only answer after losing cookies. (If you can, please change this into a comment to the answer). – nnbvcn Aug 4 '11 at 9:03
You left out the all important quotes around the regex when you put it in. Put the double quotes back in like they are in mine and see if it works that way. – jw013 Aug 4 '11 at 9:06
After I put the quotes back in, it worked. I took them out in the first place because gedit was showing wrong highlighting. Thanks. – nnbvcn Aug 4 '11 at 9:11

A sidepoint: you don't need to use sed for the output of date, because date itself has some good formatting tools of its own. If you pass date a string starting with '+' then you can use the formatting codes from strftime.

$ date '+%Y-%m-%d_%H-%M-%S'

I'd personally use something sortable for the directory name, which means I'd never use "Fri" as an early part of the name.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .