Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using ln from sorted by
Terminal - Commands using ln - 31 results
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
ln -s /BASE/* /TARGET/
2013-09-12 18:47:35
User: thehitman
Functions: ln
Tags: bash
5

Simple and easy to remember, if it already exists then it just ignores it.

for i in '/tmp/file 1.txt' '/tmp/file 2.jpg'; do ln -s "$i" "$i LINK"; done
2013-08-02 08:30:50
User: qwertyroot
Functions: ln
0

Replace

'/tmp/file 1.txt' '/tmp/file 2.jpg'

with

"$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"

for Nautilus script

Or with

%F

for Thunar action

If you linking the symlinks itself, but want to link to source files instead of symlinks, use

"`readlink -m "$i"`"

instead of

"$i"

like this:

for i in '/tmp/file 1.txt' '/tmp/file 2.jpg'; do ln -s "`readlink -m "$i"`" "$i LINK"; done

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ln -s /base/* /target && ls -l /target
tag() { local t="$HOME/tags/$1"; [ -d $t ] || mkdir -p $t; shift; local i; for i in $*; do ln -s $(readlink -f $i) $t;done}
2012-02-10 15:43:02
Functions: ln mkdir readlink
0

shell function which allows you to tag files by creating symbolic links directories in a 'tags' folder.

The tag function takes a tag name as its first argument, then a list of files which take that tag. The directory $HOME/tags/tagname will then hold symbolic links to each of the tagged files. This function was modified from bartonski's (http://www.commandlinefu.com/commands/view/10216) inspired by tmsu (found at https://bitbucket.org/oniony/tmsu/wiki/Home) with readlink function by flxndn (http://www.commandlinefu.com/commands/view/10222).

Example:

tag dog airedale.txt .shizturc weimeraner.pl

This will create $HOME/tags/dog which contains symbolic links to airedale.txt .shizturc and weimeraner.pl

tag() { local t="$HOME/tags/$1"; [ -d $t ] || mkdir -p $t; shift; ln $* $t;}
2012-02-08 12:40:45
User: bartonski
Functions: ln mkdir
2

The tag function takes a tag name as its first argument, then a list of files which take that tag. The directory $HOME/tags/tagname will then hold symbolic links to each of the tagged files. This function was inspired by tmsu (found at https://bitbucket.org/oniony/tmsu/wiki/Home).

Example:

tag dog airedale.txt .shizturc weimeraner.pl

This will create $HOME/tags/dog which contains symbolic links to airedale.txt .shizturc and weimeraner.pl

fdupes -R -1 path | while read -r line; do (echo $line | xargs -n 1 | (first="true"; firstfile=""; while read file; do if [ "$first" == "true" ]; then first="false"; firstfile=$file; else ln --force "$firstfile" "$file"; fi; done)); done
2012-02-01 15:08:18
User: eclewis
Functions: echo ln read xargs
1

This variation can handle file paths containing spaces.

fdupes -r -1 Neu | while read line; do j="0"; buf=""; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; buf=$file; else ln -f $buf $file; fi; done; done
echo -e '#!/bin/bash\nssh remote-user@remote-host $0 "$@"' >> /usr/local/bin/ssh-rpc; chmod +x /usr/local/bin/ssh-rpc; ln -s hostname /usr/local/bin/ssh-rpc; hostname
2011-12-28 17:43:34
User: mechmind
Functions: chmod echo hostname ln
Tags: ssh rpc
-3

It's useful mostly for your custom scripts, which running on specific host and tired on ssh'ing every time when you need one simple command (i use it for update remote apt repository, when new package have to be downloaded from another host).

Don't forget to set up authorization by keys, for maximum comfort.

ln -s "`find /path -type f -iname $(ls -t /path | head -1 )`" /path/latest
recoll -t -q "keyword" | grep message/rfc822 | sed -s 's,^.*\('$MAILDIR'[^]]*\)\].*$,\"\1\",' | xargs ln -sft $MAILDIR/bingo/cur/
fail () { ln -s /nonexistent 0_FAIL_${1}; }
2011-11-06 20:14:33
User: pipeliner
Functions: ln
Tags: ls color link fail
0

If you use colored ls(1), the broken symbolic links significantly differ from regular files and directories in the ls listing. In my case it is bright red. 0 is for getting the first place in the list.

(IFS=$'\n'; ln -sf $(awk '((NR % 2) != 0 && NR > 1) {print "prefix" $0}' list.m3u) target_folder)
2011-09-25 16:45:28
User: lxe
Functions: awk ln
1

This command will place symbolic links to files listed in an m3u playlist into a specified folder. Useful for uploading playlists to Google Music.

prefix = The full path prefix to file entries in your .m3u file, if the file paths are relative. For example, if you have "Music/folder/song.mp3" in your list.m3u, you might want to specify "/home/username" as your prefix.

list.m3u = Path to the playlist

target_folder = Path to the target folder in which you would like to create symlinks

ln -nsf <TARGET> <LINK>
2011-07-14 14:07:06
User: rdc
Functions: ln
Tags: Linux symlinks
21

Instead of deleting an existing symlink and then re-creating it pointing at the new location, it is possible to perform the same action with this one command.

Interesting discussion on whether this is possible to do atomically here: http://answers.google.com/answers/threadview?id=526119

fdupes -r -1 path | while read line; do j="0"; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; else ln -f ${line// .*/} $file; fi; done; done
adobenospy() { for I in ~/.adobe ~/.macromedia ; do ( [ -d $I ] && rm -rf $I ; ln -s -f /dev/null $I ) ; done }
2011-06-08 14:34:52
User: Timothy
Functions: ln rm
0

A safer way to block adobe and macromedia flash tracking and spyware

fdupes -r -1 path | while read line; do j="0"; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; else sudo ln -f ${line// .*/} $file; fi; done; done
for h in `find /proc/*/fd -ilname "/tmp/Flash*" 2>/dev/null`; do ln -s "$h" `readlink "$h" | cut -d' ' -f1`; done
2011-03-02 09:43:42
User: hons
Functions: cut ln
17

Newer versions of the flashplayer browser plugin delete the tmp flash video immediately after opening a filehandle to prevent the user from "exporting" the video by simply copying the /tmp/FlashXYZ file. This command searches such deleted flash videos and creates symbolic links to the opened filehandle with the same name as the deleted file.

This allows you to play your flash-videos (from e.g. youtube) with e.g. mplayer or copy the buffered video if you want to keep it.

$ find . -iname *.mp3 | while read line ; do ln -s "$line" $(echo -e "$line" | openssl md5).mp3 ; done ; mpg123 *.mp3
for i in ~/.adobe ~/.macromedia ; do ( rm $i/ -rf ; ln -s /dev/null $i ) ; done
2010-12-29 13:23:48
User: mhs
Functions: ln rm
18

Brute force way to block all LSO cookies on a Linux system with the non-free Flash browser plugin. Works just fine for my needs. Enjoy.

find /deep/tree/ -type f -print0|xargs -0 -n1 -I{} ln -s '{}' .
2010-12-21 13:00:33
User: dinomite
Functions: find ln xargs
Tags: find xargs links
1

If you want to pull all of the files from a tree that has mixed files and directories containing files, this will link them all into a single directory. Beware of filesystem files-per-directory limits.

cd /usr/src ; wget http://www.rarlab.com/rar/unrarsrc-4.0.2.tar.gz ; tar xvfz unrarsrc-4.0.2.tar.gz ; cd unrar ; ln -s makefile.unix Makefile ; make clean ; make ; make install
ln -nvfs /source /destination
2010-11-28 17:22:37
User: daniele
Functions: ln
2

-n: dereference the existing link

-v: (optional) to be sure of what is being done

-f: force the deletion of the existing one

-s: creates a symlink

Be careful: the destination can also be a file or a directory and it will be overwritten.

ln
rm ~/.bash_history; ln -s /dev/null ~/.bash_history
2009-10-08 17:40:48
Functions: ln rm
-5

Remove your BASH history and then link it to /dev/null