0

mkdir $(date '+%d-%b-%Y')

then cd to the dynamically created directory

How to "cd" to a directory which is created using "mkdir $(date '+%d-%b-%Y')" and do the operations by moving into the created directory in bash script

3 Answers3

3

Simple way would be, you store the directory name in a variable

    dirname=$(date '+%d-%b-%Y')
    if [ -n "$dirname" ]; then
        mkdir "$dirname"
        if [ -d "$dirname" ]; then
            cd "$dirname"
        fi
    fi

Added some error handling and also if your file is written in Windows and being run in an unix environment or vice-versa, I would recommend using dos2unix which will handle the new line character conversions (this is for the ? characters OP is seeing in ls).

ViKiG
  • 764
  • 9
  • 21
  • 1
    It's not important in this particular case (unless someone did something silly with `$IFS`), but it's generally a good idea to double-quote variable references (e.g. `mkdir "$dirname"`). BTW, the reason it's good to use a variable (rather than running `date` twice) is that if this runs at 11:59:59 at night, you might create one directory then try to `cd` into the next day's. – Gordon Davisson Feb 18 '19 at 07:32
  • dirname=$(date '+%d-%b-%Y') while doing this the directory is created with ?? appended at the end , why this is happening and what must be done –  Feb 18 '19 at 09:32
  • @msk Don't know why it is happening for you. I tested the above script, it is working fine. – ViKiG Feb 18 '19 at 10:17
  • 1
    @ViKiG Your script may have DOS/Windows-style line endings, which makes the shell think every line has a spurious carriage return character at then end (which some versions of `ls` will display as "?"). See: [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Feb 18 '19 at 22:38
  • 1
    Oh, and one more suggestion: if this is for a script, it'd be a really good idea to have some error checking on these commands, so if either the directory can't be created or it can't be moved into, the rest of the script doesn't blindly continue runnint... in the wrong place. – Gordon Davisson Feb 18 '19 at 22:41
1

In Bash, $_ expands to the last argument to the previous command. So you could do:

mkdir $(date '+%d-%b-%Y')
cd $_

In a real Bash program you would want to quote the expansions (use Shellcheck on your code to check for missing quotes), and check for errors on both mkdir and cd.

pjh
  • 6,388
  • 2
  • 16
  • 17
0

Can you show me your case?

In most cases, you should not cd in to the directory. Use absolute path instead:

Good practice:

mkdir /tmp/mydir/
cp -R /usr/local/example/ /tmp/mydir/
sed 's/foo/bar/g' /tmp/mydir/afile

Bad practice:

mkdir /tmp/mydir/
cd /tmp/mydir/
cp -R /usr/local/example/ .
sed 's/foor/bar/g' afile

P.S. Subj:

$ mkdir $(date '+%d-%b-%Y')
$ cd $(date '+%d-%b-%Y')
$ pwd
/Users/user/18-Feb-2019
Viktor Khilin
  • 1,760
  • 9
  • 21