2
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.

1
  • 2
    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
    Commented Aug 4, 2011 at 9:36

4 Answers 4

11

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

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

With bash, you can use history expansion too:

mkdir "$(date -R)" && cd !#:^
2

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.

3
  • 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
    Commented Aug 4, 2011 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
    Commented Aug 4, 2011 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
    Commented Aug 4, 2011 at 9:11
1

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.

You must log in to answer this question.

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