How to cd to a newly created dir?

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.

nnbvcn

Posted 2011-08-04T08:02:40.067

Reputation: 21

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 – 2011-08-04T09:36:53.463

Answers

11

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

NEWDIR=$(date -R);mkdir "$NEWDIR";cd "$NEWDIR"

Zoredache

Posted 2011-08-04T08:02:40.067

Reputation: 18 453

Your solution works great, and I accept your answer. However, I'm still left wondering how to fix my code, out of curiosity. – nnbvcn – 2011-08-04T08:41:54.057

My 2c: (NEWDIR=$(date -R); mkdir "$NEWDIR" && cd "$NEWDIR") That doesn't clutter the environment.. – estani – 2014-05-22T16:13:46.717

4

With bash, you can use history expansion too:

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

glenn jackman

Posted 2011-08-04T08:02:40.067

Reputation: 18 546

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.

jw013

Posted 2011-08-04T08:02:40.067

Reputation: 1 163

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 – 2011-08-04T09:03:05.190

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 – 2011-08-04T09:06:31.850

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 – 2011-08-04T09:11:35.417

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.

Rich Homolka

Posted 2011-08-04T08:02:40.067

Reputation: 27 121