Commands tagged cd (58)

  • Use dots to cd down directories instead of having to remember all of the pesky back slashes! Better yet, works on even and odd number of dots! Now, just estimate how far down you want to traverse. Show Sample Output


    1
    for i in {1..6};do c=;d=;for u in `eval echo {1..$i}`;do c="$c../";d="$d..";eval "$d(){ cd $c;}"; eval "$d.(){ cd $c;}";done;done
    bbbco · 2013-09-04 20:12:45 9

  • 1
    cd -P .
    snipertyler · 2014-12-16 15:39:29 7
  • I've seen a lot of overly complicated attempts at figuring out "where am I?" I think this is a part of the problem: type -a pwd force the use of the binary version of `pwd` instead of the built-in with "/bin/pwd -P" -P option provides an absolute path to the present working directory for the overly cautious type: (which pwd) -P Show Sample Output


    1
    /bin/pwd -P
    forestb · 2015-11-21 22:26:57 10
  • Make it a reusable function and add the -p flag to mkdir to create directories recursively usage: mydir some/dir/to/create


    0
    mydir(){mkdir -p $1 && cd $1}
    m0jumb0 · 2011-04-06 15:03:33 3
  • ksh's version of cd has an optional syntax where you can type "cd old new" and it will replace "old" with "new" in your current directory and take you there. This is very handy when you have a parallel directory structure, like source and object directories. As suggested, you can just type cd ${PWD/old/new} to get this in bash, but this function in your .bashrc will let you type the ksh cd syntax and avoid typing the special characters while preserving other cd functionality. Show Sample Output


    0
    cd () { cdop=""; while [ "$1" != "${1#-}" ]; do cdop="${cdop} ${1}"; shift; done; if [ $# -eq 2 ]; then newdir="${PWD/$1/$2}"; [ -d "${newdir}" ] || { echo "no ${newdir}"; return 1; }; builtin cd $cdop "${newdir}"; else builtin cd $cdop "$@"; fi }
    splante · 2011-04-07 14:36:26 3
  • I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.


    0
    mydir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
    xeor · 2011-04-27 16:33:38 4
  • Move efficiently between directories. . This command adds a couple of extra features to cd, without affecting normal use. CDPATH use is also unaffected. It introduces and environment variable CDDIR which is used as an alternate home directory. . Note: I don't want to alter $HOME because then all my dot files will move. . Examples: . cd dir Change directory to "dir" (using CDPATH if necessary) . cd dir/file.txt Change directory to "dir" (containing folder of "file.txt") This allows you to cut'n'paste, or use . CDDIR is unset cd Change directory to $HOME . CDDIR=/home/flatcap/work cd Change directory to /home/flatcap/work . For convenience, put the command, and the following, in your .bashrc or .bash_profile export CDDIR="/home/flatcap/work" alias cdd="CDDIR=$(pwd)" Show Sample Output


    0
    cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
    flatcap · 2011-06-24 08:48:13 10
  • Sometimes you need the full path to your script, regardless of how it was executed (which starting directory) in order to maintain other relative paths in the script. If you attempt to just use something simple like: STARTING_DIR="${0%/*}" you will only get the relative path depending on where you first executed the script from. You can get the relative path to the script (from your starting point) by using dirname, but you actually have to change directories and print the working directory to get the absolute full path. Show Sample Output


    0
    STARTING_DIR=$(cd $(dirname $0) && pwd)
    bbbco · 2011-11-30 17:35:15 5
  • Creates a directory and then cds into it directly Show Sample Output


    0
    function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
    ankush108 · 2012-06-26 17:19:16 4
  • manage directory stack Show Sample Output


    0
    pushd path/to/dir/
    ankush108 · 2012-06-26 17:23:55 6
  • Use shopt -s cdspell to correct the typos in the cd command Show Sample Output


    0
    shopt -s cdspell
    ankush108 · 2012-06-26 17:28:57 3
  • as alternative to cd $OLDPWD


    0
    cd -
    Blaskovic · 2012-07-04 22:34:47 3
  • I realise that this is just a reiteration of another entry (regardless of whether I came up with all this all by myself), but I would like present my additional alias' in context as a method of managing your directories. Rather convenient.


    0
    alias md='mkdir -p'; alias rd='rmdir'; mcd () { mkdir "$@" && cd "$_"; }
    expelledboy · 2012-08-12 12:54:51 7
  • Since none of the systems I work on have readlink, this works cross-platform (everywhere has perl, right?). Note: This will resolve links. Show Sample Output


    0
    FULLPATH=$(perl -e "use Cwd 'abs_path';print abs_path('$0');")
    follier · 2013-02-01 20:09:34 5
  • The comp.unix.shell posting by St?phane Chazelas also lists the following offsets: type 32768 (1 byte) id 32769 (5 bytes) version 32774 (1 byte) system_id 32776 (32 bytes) volume_id 32808 (32 bytes) volume_space_size 32848 (8 bytes) escape_sequences 32856 (32 bytes) volume_set_size 32888 (4 bytes) volume_sequence_number 32892 (4 bytes) logical_block_size 32896 (4 bytes) path_table_size 32900 (8 bytes) type_l_path_table 32908 (4 bytes) opt_type_l_path_table 32912 (4 bytes) type_m_path_table 32916 (4 bytes) opt_type_m_path_table 32920 (4 bytes) root_directory_record 32924 (34 bytes) volume_set_id 32958 (128 bytes) publisher_id 33086 (128 bytes) preparer_id 33214 (128 bytes) application_id 33342 (128 bytes) copyright_file_id 33470 (37 bytes) abstract_file_id 33507 (37 bytes) bibliographic_file_id 33544 (37 bytes) creation_date 33581 (17 bytes) modification_date 33598 (17 bytes) expiration_date 33615 (17 bytes) effective_date 33632 (17 bytes) file_structure_version 33649 (1 byte) application_data 33651 (512 bytes)


    0
    dd if=/dev/cdrom bs=1 skip=32808 count=32 conv=unblock cbs=32 2>/dev/null
    mlk · 2013-04-14 20:50:58 6
  • Only requirement is bash shell. No functions needed. Show Sample Output


    0
    mkdir -p /path/to/folder.d; \cd $_
    asphaltleopard · 2013-04-20 00:30:56 5
  • Additionally you cal also print the current directory in the end. cdb(){ range=$(eval "echo '{1..$1}'"); toPrint="'../%.0s' $range"; printfToEval=$(echo "printf $toPrint"); toCd=$(eval $printfToEval); eval "cd $toCd"; pwd; } Show Sample Output


    0
    cdb(){ range=$(eval "echo '{1..$1}'"); toPrint="'../%.0s' $range"; printfToEval=$(echo "printf $toPrint"); toCd=$(eval $printfToEval); eval "cd $toCd"; }
    knoxxs · 2015-07-08 11:51:18 9
  • Creates a function that can be used instead of cd when navigating the directory tree. Automagically displays slightly more than nothing. Show Sample Output


    0
    ct() {cd $1; tree -L 2}
    rasinj · 2015-08-13 21:58:03 9

  • 0
    alias ..='cd ..'; alias ...='cd ../../'; alias ....='cd ../../../'; alias .....='cd ../../../../'; alias ......='cd ../../../../../';
    kenorb · 2015-10-09 16:14:00 10

  • 0
    cdb() { for i in $(seq $1); do cd ..; done }
    cadrian · 2016-05-02 07:01:21 9

  • 0
    function cdb() { select dir in $(find -type d -name "$1" -not -path '*/\.*' -prune); do cd "${dir}" && break; done }
    oldo · 2017-04-08 14:40:11 59

  • 0
    take dirname
    prabhakaran9397 · 2017-09-26 16:12:46 21
  • The Windows Subsystem for Linux (WSL) is a compatibility layer for running binary Linux executables natively in Windows. A folder such as "C:\Program Files (x86)\Common Files" is represented as "/mnt/c/Program Files (x86)/Common Files". This function allows you to change the current directory to a Windows folder. Show Sample Output


    0
    function _cd() { local dir; dir="$(sed -e 's~\([a-z]\):~/mnt/\L\1~gi' <<< "${*//'\'/"/"}" )"; if [ -d "$dir" ]; then cd "$dir" || exit; fi; }
    mikhail · 2019-06-06 17:53:28 40
  • work for execute file


    -1
    dirname $(readlink -f ${BASH_SOURCE[0]})
    unixyangg · 2011-12-02 16:10:38 3
  • This example is taken from Cygwin running on Win7Ent-64. Device names will vary by platform. Both commands resulted in identical files per the output of md5sum, and ran in the same time down to the second (2m45s), less than 100ms apart. I timed the commands with 'time', which added before 'dd' or 'readom' gives execution times after the command completes. See 'man time' for more info...it can be found on any Unix or Linux newer than 1973. Yeah, that means everywhere. readom is supposed to guarantee good reads, and does support flags for bypassing bad blocks where dd will either fail or hang. readom's verbosity gave more interesting output than dd. On Cygwin, my attempt with 'readom' from the first answer actually ended up reading my hard drive. Both attempts got to 5GB before I killed them, seeing as that is past any CD or standard DVD. dd: 'bs=1M' says "read 1MB into RAM from source, then write that 1MB to output. I also tested 10MB, which shaved the time down to 2m42s. 'if=/dev/scd0' selects Cygwin's representation of the first CD-ROM drive. 'of=./filename.iso' simply means "create filename.iso in the current directory." readom: '-v' says "be a little noisy (verbose)." The man page implies more verbosity with more 'v's, e.g. -vvv. dev='D:' in Cygwin explicitly specifies the D-drive. I tried other entries, like '/dev/scd0' and '2,0', but both read from my hard drive instead of the CD-ROM. I imagine my LUN-foo (2,0) was off for my system, but on Cygwin 'D:' sort of "cut to the chase" and did the job. f='./filename.iso' specifies the output file. speed=2 simply sets the speed at which the CD is read. I also tried 4, which ran the exact same 2m45s. retries=8 simply means try reading a block up to 8 times before giving up. This is useful for damaged media (scratches, glue lines, etc.), allowing you to automatically "get everything that can be copied" so you at least have most of the data. Show Sample Output


    -1
    dd bs=1M if=/dev/scd0 of=./filename.iso OR readom -v dev='D:' f='./filename.iso' speed=2 retries=8
    scotharkins · 2013-10-23 15:53:27 6
  •  < 1 2 3 > 

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Rename files in batch

Download an Entire website with wget

Set laptop display brightness
Run as root. Path may vary depending on laptop model and video card (this was tested on an Acer laptop with ATI HD3200 video). $ cat /proc/acpi/video/VGA/LCD/brightness to discover the possible values for your display.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

a short counter
Maybe you know shorter ?

Log colorizer for OSX (ccze alternative)
Download colorizer by @raszi @ http://github.com/raszi/colorize

add all files not under version control to repository
This should handle whitespaces well and will not get confused if your filenames have "?" in them

Sort netflow packet capture
Sort netflow packet capture by unique connections excluding source port.

back ssh from firewalled hosts
host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh 127.0.0.1 -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: