Commands using cd (215)

  • The biggest advantage of this over the functions is that it is portable.


    60
    mkdir /home/foo/doc/bar && cd $_
    kzh · 2011-08-12 11:29:19 13
  • This uses Bash's "process substitution" feature to compare (using diff) the output of two different process pipelines.


    35
    diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
    mbirk · 2009-05-21 04:44:29 44
  • How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do. This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist. Show Sample Output


    32
    md () { mkdir -p "$@" && cd "$@"; }
    drewk · 2009-09-24 16:09:19 20
  • Uses the last argument of the last executed command, and gets the directory name from it. Use $!:t for the filename alone, without the dirname. Show Sample Output


    25
    cd !$:h
    lingo · 2009-08-07 00:37:08 10
  • When you fill a formular with Firefox, you see things you entered in previous formulars with same field names. This command list everything Firefox has registered. Using a "delete from", you can remove anoying Google queries, for example ;-)


    19
    cd ~/.mozilla/firefox/ && sqlite3 `cat profiles.ini | grep Path | awk -F= '{print $2}'`/formhistory.sqlite "select * from moz_formhistory" && cd - > /dev/null
    klipz · 2009-04-13 20:23:37 15
  • If you use symlinks a lot, especially nested symlinks, this puts you back on the absolute path to command-line-fu-nirvana. (Note the backticks around pwd). Show Sample Output


    16
    cd `pwd -P`
    Davvolun · 2009-02-17 17:21:24 13
  • Forces the -i flag on the rm command when using a wildcard delete. Show Sample Output


    15
    cd <directory>; touch ./-i
    ljmhk · 2011-05-12 11:01:58 20
  • This command looks for a single file named emails.txt which is located somewhere in my home directory and cd to that directory. This command is especially helpful when the file is burried deep in the directory structure. I tested it against the bash shells in Xubuntu 8.10 and Mac OS X Leopard 10.5.6


    11
    cd $(dirname $(find ~ -name emails.txt))
    haivu · 2009-05-01 21:26:58 14
  • record audio notes or meetings requires arecord and lame run mp3gain on the resulting file to increase the volume / quality ctrl-c to stop recording Show Sample Output


    11
    arecord -q -f cd -r 44100 -c2 -t raw | lame -S -x -h -b 128 - `date +%Y%m%d%H%M`.mp3
    oracular · 2009-09-25 05:32:52 8
  • This little function will smarten 'cd'. If you try to cd into a file (which I guess we all have done), it cd's into the directory of that file instead. I had to use nesten if's, to get cd to still work with 'cd' (to get to $HOME), 'cd -' (to get to last directory), and 'cd foo\ bar'. Show Sample Output


    9
    cd() { if [ -z "$1" ]; then command cd; else if [ -f "$1" ]; then command cd $(dirname "$1"); else command cd "$1"; fi; fi; }
    xeor · 2010-04-23 19:17:43 6
  • I wrote this a long time ago, wondering why this wasn't floating around somewhere out there (at least not where I could find).. this seems much more simple than multiple aliases and can cd out of directories easier. Show Sample Output


    9
    up() { local x='';for i in $(seq ${1:-1});do x="$x../"; done;cd $x; }
    evil · 2012-05-16 04:21:41 5
  • After typing cd directory [enter] ls [enter] so many times, I figured I'd try to make it into a function. I was surprised how smoothly I was able to integrate it into my work on the command line. Just use cdls as you would cd. It will automatically list the directory contents after you cd into the directory. To make the command always available, add it to your .bashrc file. Not quite monumental, but still pretty convenient. Show Sample Output


    8
    function cdls { cd $1; ls; }
    joem86 · 2009-03-10 19:13:47 16
  • This command create a new temp directory using mktemp (to avoid collisions) and change the current working directory to the created directory. Show Sample Output


    8
    cd "$(mktemp -d)"
    Weboide · 2009-05-20 11:48:12 11
  • This command securely erases all the unused blocks on a partition. The unused blocks are the "free space" on the partition. Some of these blocks will contain data from previously deleted files. You might want to use this if you are given access to an old computer and you do not know its provenance. The command could be used while booted from a LiveCD to clear freespace space on old HD. On modern Linux LiveCDs, the "ntfs-3g" system provides ReadWrite access to NTFS partitions thus enabling this method to also be used on Wind'ohs drives. NB depending on the size of the partition, this command could take a while to complete. Show Sample Output


    8
    # cd $partition; dd if=/dev/zero of=ShredUnusedBlocks bs=512M; shred -vzu ShredUnusedBlocks
    mpb · 2009-06-21 14:17:22 12
  • Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file. Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.


    8
    export QQ=$(mktemp -d);(cd $QQ; curl -s -O http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' > clf.sh;rm -r $QQ
    bartonski · 2010-01-30 19:47:42 39

  • 7
    ( cd SOURCEDIR && tar cf - . ) | (cd DESTDIR && tar xvpf - )
    res0nat0r · 2009-06-26 19:13:51 14
  • Another way of doing it that's a bit clearer. I'm a fan of readable code.


    7
    script_path=$(cd $(dirname $0);pwd)
    jgc · 2009-10-14 16:04:03 8
  • This is useful for quickly jumping around branches in a file system, or operating on a parellel file. This is tested in bash. cd to (substitute in PWD, a for b) where PWD is the bash environmental variable for the "working directory" Show Sample Output


    7
    cd ${PWD/a/b}
    greggster · 2011-03-03 06:27:12 10
  • This is usefull to diff 2 paths in branches of software, or in different versions of a same zip file. So you can get the real file diff. Show Sample Output


    7
    diff <(cd A; find -type f|xargs md5sum ) <(cd B; find -type f | xargs md5sum )
    glaudiston · 2013-07-02 18:02:05 9
  • Often, the very next command after the cd command is 'ls', so why not combine them?. Tested on a Red Hat derivative and Mac OS X Leopard Update: changed ${1:-$HOME} to "${@:-$HOME}" to accomodate directories with spaces in the names


    6
    cd() { builtin cd "${@:-$HOME}" && ls; }
    haivu · 2009-03-05 22:37:35 12
  • Based on linkinpark342 suggestion. Sometimes you have to browse your way through a lot of sub-directories. This command cd to the previous sub-directory in alphabetical order. For example, if you have the directories "lectures/01-intro", "lectures/02-basic", "lectures/03-advanced" and so on, and your PWD is "02-basic", it jumps to "01-intro".


    6
    cd ../"$(ls -F ..|grep '/'|grep -B1 `basename $PWD`|head -n 1)"
    gwiener · 2009-05-18 06:44:02 8
  • This command will set bash as the default shell for all users in a FreeBSD system.


    6
    cd /usr/home && for i in *;do chsh -s bash $i;done
    casidiablo · 2009-12-31 18:48:53 6
  • `up 3` will climb the directory tree by three steps. `up asdf` will do nothing, and returns exit code 1 as an error should.


    6
    up() { [ $(( $1 + 0 )) -gt 0 ] && cd $(eval "printf '../'%.0s {1..$1}"); }
    Mozai · 2012-06-15 17:10:45 5
  • Usage: up N I did not like two things in the submitted commands and fixed it here: 1) If I do cd - afterwards, I want to go back to the directory I've been before 2) If I call up without argument, I expect to go up one level It is sad, that I need eval (at least in bash), but I think it's safe here. eval is required, because in bash brace expansion happens before variable substitution, see http://rosettacode.org/wiki/Repeat_a_string#Using_printf


    6
    function up { cd $(eval printf '../'%.0s {1..$1}) && pwd; }
    michelsberg · 2013-01-21 12:57:45 7
  • Greps IRC logs for phrases and lists users who said them. Show Sample Output


    5
    cd ~/.purple/logs/; egrep -ri "i can haz|pwn|l33t|w00|zomg" * | cut -d'/' -f 3 | sort | uniq | xargs -I {} echo "Note to self: ban user '{}'"
    rhythmx · 2009-02-05 21:23:53 17
  •  1 2 3 >  Last ›

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

Remount root in read-write mode.
Saved my day, when my harddrive got stuck in read-only mode.

most used unix commands

Convert CSV to JSON
Replace 'csv_file.csv' with your filename.

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"

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"

Run a command as root, with a delay
$ sleep 1h ; sudo command or $ sudo sleep 1h ; sudo command won't work, because by the time the delay is up, sudo will want your password again.

get function's source
no need to reinvent the wheel. Thanks to the OP for the "obsolete" hint. 'declare' may come in pretty handy on systems paranoid about "up-to-dateness"

Create a tar archive using xz compression
Compress files or a directory to xz format. XZ has superior and faster compression than bzip2 in most cases. XZ is superior to 7zip format because it can save file permissions and other metadata data.

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"

Create a QR code image in MECARD format
Add the QR code image on your webpage, business card ., etc, so people can scan it and quick add to their Contact Address Book. Tested on iPhone with QRreader.


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: