heiko-barth.de

// BASH: "mkdir" und "cd" kombinieren

Vielleicht kennt ihr das: Man erstellt ein Verzeichnis mit mkdir foo und wechselt anschließend direkt mit cd foo hinein.

Um das etwas zu verkürzen, kann man sich wie folgt behelfen:

Im Suchpfad ($PATH), z.B. unter /usr/local/bin, erstellt man ein Skript mkcd.

mkcd
#!/bin/bash
 
mkdir -p "$@" &&
cd "$@"

Ruft man nun auf der BASH

. mkcd foo

auf, wird ein Verzeichnis erstellt und automatisch hinein gewechselt.

Der Punkt am Anfang ist nötig, da sonst nur das aufrufende Skript in das Verzeichnis wechselt und nicht die aktuelle BASH Sitzung.

Noch eleganter geht es mit einem BASH-Alias. Diesen erstellt man z.B. in ~/.bashrc.

alias mkcd='source mkcd'

Nun reicht es mkcd foo einzugeben.

Leave a comment…



   ____  ____    __ __   ____  _   __   _  __  _____
  / __/ / __ \  / // /  /  _/ | | / /  | |/_/ / ___/
 / _/  / /_/ / / _  /  _/ /   | |/ /  _>  <  / /__  
/___/  \____/ /_//_/  /___/   |___/  /_/|_|  \___/
  • E-Mail address will not be published.
  • Formatting:
    //italic//  __underlined__
    **bold**  ''preformatted''
  • Links:
    [[http://example.com]]
    [[http://example.com|Link Text]]
  • Quotation:
    > This is a quote. Don't forget the space in front of the text: "> "
  • Code:
    <code>This is unspecific source code</code>
    <code [lang]>This is specifc [lang] code</code>
    <code php><?php echo 'example'; ?></code>
    Available: html, css, javascript, bash, cpp, …
  • Lists:
    Indent your text by two spaces and use a * for
    each unordered list item or a - for ordered ones.
Web 2.0



RSS   RSS abonieren

Github   Github
QR Code