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/
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.
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
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:
This is like `cd -` but doesn't echo the new directory name, which is preferable (to me) for an alias, e.g.
alias cdo="cd $OLDPWD"
Somehow, i prefer forcing to rm interactively to accidently rm'ing everything...
Output of this command is the difference of recursive file lists in two directories (very quick!).
To view differences in content of files too, use the command submitted by mariusbutuc (very slow!):
diff -rq path_to_dir1 path_to_dir2
_ff(){
cd /mnt;
echo /mnt/*/* |sed '
s/ \/mnt\//\&/g;
'|sed '/'"$1"'/!d';
cd -;
}
ff(){
case $# in
0)
echo "usage: ff glob [sed-cmds] [--|var-name]"
;;
1)
_ff $1 |sed =
;;
[2-9])
case $2 in
--) _ff $1 |less -SN
;;
*) _ff $1 |sed -n ''"$2"''|tr '\n' '\040' |sed 's/.*/export '"$3"'=\"&/;s/=\" /=\"/;s/ $/\"/' > $HOME/.ff;
case $# in
3)
. $HOME/.ff
;;
esac;
sed '
s/export .*=\"/\$'"$3"' = \"/;' $HOME/.ff;\
;;
esac
;;
esac;
}
v(){
local a=$HOME;
sed '
s/export /less -n \$/;
s/=.*//;
' $a/.ff > $a/.v ;
. $a/.v ;
}
Another approach using ls(1)
lsl(){
_lsl ()
{
ls -l $3 /mnt/*/$1* 2>/dev/null;
};
case $# in
0)
echo "usage: lsl pat [ls-options|result-no]";
echo "usage: lsle pat [sed-cmds]"
;;
1)
_lsl $1 |sed =
;;
2)
case $2 in
-*) _lsl $1 $@;;
*)
_lsl $1 |sed 's/.* //;
'"$2"'!d;
'"$2"'q' > $HOME/.lsl ;
export v=$(sed 1q $HOME/.lsl);
echo \$v = $v
;;
esac
;;
esac;
}
exp(){
echo "%s/\$/ /";
echo "%j";
echo "s/^/export v=\"";
echo "s/\$/\"";
echo "s/ \"\$/\"";
echo ".";
echo "wq";
}
lsle(){
lsl $1 -1 |sed $2 > .lsl&&
exp |ed -s .lsl >&-&&
. .lsl&&
echo \$v = $v;
}
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.
Let's say you have a set of files in tree A that you want duplicated to tree B while preserving their directory structure / hierarchy. (For example, you might want to copy your 'profile' model/views/controller from one Rails application to another.) The "pax" command will copy all matching files to the destination while creating any necessary directories.
I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define:
export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history.
Usage: jd dir
Requires globstar. To set globstar use:
shopt -s globstar
Simple tar pipe to be used to copy directories while including hidden files and maintaining file permissions
PmWiki stores wiki pages as Group.Name. Simply split the directory listing and count frequency of group occurances.
This is a equivalent to the GNU ' readlink' tool, but it supports following all the links, even in different directories.
An interesting alternative is this one, that gets the path of the destination file
myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
The biggest advantage of this over the functions is that it is portable.
Replace the head -1 with head -n that is the n-th item you want to go to.
Replace the head with tail, go to the last dir you listed.
You also can change the parameters of ls.