1

I am writing a script that requires to get a dynamic folder path like this.

somefolder/prefix.datetime_suffix/foldera/folderb

datetime is in format YYYYMMDDHHMMSS. datetime part is changing.

In a script I need to get a full path of folderb to move files there.

How can I do that?

2
  • what's your problem? to build the datetime string by a given datetime? Commented Oct 30, 2018 at 17:57
  • it is not known in advance :( Commented Oct 30, 2018 at 18:45

2 Answers 2

2

You could have an inline date call in your path like:

    MYVARIABLE="somefolder/`date '+%Y%m%d%H%M%S'`/foldera/folderb/"

or (using the cleaner syntax)

    MYVARIABLE="somefolder/$(date '+%Y%m%d%H%M%S')/foldera/folderb/"

After setting the above variable, you could use it with mv command like:

    mv ./file.xyz $(MYVARIABLE)

Also, the man page for date lists all the formatting options.

Sign up to request clarification or add additional context in comments.

3 Comments

Mark Howell's answer works perfectly well. As a personal preference, I prefer this Bash syntax (assuming you are using Bash ...) somefolder/$(date '+%Y%m%d%H%M%S')/foldera/folderb Because my brain parses balanced parens more easily than equal back-tic pairs
thanks but I need it in a variable. Sorry for confusion. I had to mention it)
I also like the $() format, though often use backticks because of fewer keystrokes.
0
find /complete_path_to/some_folder -name folderb

1 Comment

thanks but I need it in a variable. Sorry for confusion. I had to mention it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.