One Command to Create and Change Directory

how to change directory using Windows command line

The "cd" command changes the directory, but not what drive you are working with. So when you go "cd d:\temp", you are changing the D drive's directory to temp, but staying in the C drive.

Execute these two commands:

D:
cd temp

That will get you the results you want.

One command to create a directory and file inside it linux command

mkdir B && touch B/myfile.txt

Alternatively, create a function:

mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }

Execute it with 2 arguments: path to create and filename. Saying:

mkfile B/C/D myfile.txt

would create the file myfile.txt in the directory B/C/D.

Create directory and files with one command

I recommend -p.

qc() { local p="$1";
if [[ -n "$p" ]];
then mkdir -p "$p" # can be any full or relative path;
else echo "Use: qc <dirpath> [f1[..fN]]"; return 1;
fi;
shift;
for f; do touch "$p/$f"; done;
}

$: qc
Use: qc <dirpath> [f1[..fN]]

$: cd /tmp
$: qc a/b/c 5 4 3 2 1 # relative path
$: qc a/b # no files; dir already exists; no problem
$: qc /tmp/a/b/c/d 3 2 1 # full path that partially exists
$: find a # all ok
a
a/b
a/b/c
a/b/c/1
a/b/c/2
a/b/c/3
a/b/c/4
a/b/c/5
a/b/c/d
a/b/c/d/1
a/b/c/d/2
a/b/c/d/3

use cmd.exe to change directory and run command in that directory

You may want to invoke CD with the /d option, thus not only changing the current directory on drive c: but also going there (in case you are not already on that drive).

 cmd /c "cd /d c:\temp && dir"

How to change folder with git bash?

The command is:

cd  /c/project/

Tip:
Use the pwd command to see which path you are currently in, handy when you did a right-click "Git Bash here..."

How to mkdir and switch to new directory in one line

The portable way to do this is with a shell function--not a bash function (using bashims like function). Put this in the relevant .profile for interactive use:

 mkdir () {
case $1 in
(-c) command mkdir -p "$2" && cd "$2";;
(*) command mkdir "$@";;
esac
}

This adds the -c option to mkdir for interactive use. Without -c the utility acts as it always does.- And note the quoting of "$2" so this works with directories with white space in their name.

execute a subcommand in a different directory as the current one in fish?

Right, many of us coming from Posix shells like bash or zsh are used to being able to run a command substitution like this using $() or just backticks and leave the parent shell environment untouched. It's a nice trick, IMHO.

On the other hand, fish doesn't automatically create a subshell when using its command substitution operator (). There's a feature request here for that, but the workaround (as suggested there) is fairly straightforward -- Just explicitly create a subshell inside the command substitution. E.g.:

mv (fish -c 'cd ~/Downloads; ls -t | head -1 | xargs -I {} readlink -f {}') ./

The downside is that syntax highlighting/checking doesn't work in the quoted text, and quoting/escaping rules get more complicated.



Related Topics



Leave a reply



Submit