Monday, April 29, 2024
28
rated 0 times [  28] [ 0]  / answers: 1 / hits: 117568  / 2 Years ago, fri, february 25, 2022, 11:46:32

I'm seeking a way to create directory and change my present working directory to newly created directory just by using a single command. How can I do this?



i.e Instead of doing



user@Computer:~$ mkdir NewDirectory
user@Computer:~$ cd NewDirectory
user@Computer:~/NewDirectory$


I want to do



user@computer:~$ **command** NewDirectory
user@Computer:~/NewDirectory$


What can the command be?


More From » command-line

 Answers
4

If you really want it to be just one command, I suggest adding something like this to your .bashrc:



md () { mkdir -p "$@" && cd "$1"; }


Entering md foo on the command line will then create a directory called foo and cd into it immediately afterwards. Please keep in mind, that you will have to reload your .bashrc for the changes to take effect (i.e. open a new console, or run source ~/.bashrc).



Cf. http://www.commandlinefu.com/commands/view/3613/create-a-directory-and-change-into-it-at-the-same-time for possible alternatives, too.


[#32934] Saturday, February 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antebrow

Total Points: 291
Total Questions: 135
Total Answers: 117

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;