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 tagged zsh from sorted by
Terminal - Commands tagged zsh - 68 results
bc <<<"..."
python3
for i in {0..255}; do echo -e "\e[38;05;${i}m\\\e[38;05;${i}m"; done | column -c 80 -s ' '; echo -e "\e[m"
zmv '(*.*)(.*)' '${1//./_}$2'
2013-04-06 01:57:34
User: khayyam
Tags: zsh
0

Example of zsh globbing and parameter expansion.

(*.*)(.*) ... the pattern we want to act on, a period followed by a string and then period, we split the pattern into two sections which will become $1, the first part of the match, and $2, second

{1//./_}$2 ... the parameter expansion for $1 with a string substitution, followed by the match $2, the second part of the pattern.
zmv -Q '(**/)(*)(.)' '$1${(L)2}'
2013-04-03 04:27:51
User: khayyam
Tags: zsh
1

Example of zsh globing, glob qualifier, and substitution:

-Q state that the parameter will contain a glob qualifier.

(**/)(*) is recursive

(.) is our glob qualifier, with states the match is a file "."

The first parameter $1, is then substituted with $2 but with lowercasing '(L)' ... a (U) would of course be from lower to upper.

zmv -Q '(**/)* *(.)' '$f:gs/ /_'
print -rl /**/*(.f:o+w:)
2013-04-03 02:53:00
User: khayyam
0

Example of using zsh glob qualifier ...

"." = files

"f:" = files with access rights matching:

o+w = other plus write

ls -tl **/*(om[1,20])
2013-03-24 00:14:03
User: khayyam
Functions: ls
Tags: ls zsh
0

zsh globbing and glob qualifier:

'**/*' = recursive

om = ouput by modification (last access)

[1,20] = twenty files.

The '-t' switch is provided to ls so that the files are ordered with the most recent at the top. For a more 'find' like output the following can be used.

print -rl **/*(om[1,20])

print /dev/disk/by-id/*(@[1]:t)
2013-03-23 23:22:14
User: khayyam
Tags: zsh
0

example of the use of zsh glob qualifiers:

"@" = the symlink qualifier

"[1]" = first element

:t = remove leading path components, leaving the tail

print ${$(ifconfig wlan0)[6]}
2013-03-23 20:29:29
User: khayyam
Functions: ifconfig
Tags: zsh
0

A method for aquiring the ip address using zsh. If you prefer the use of iproute2 (which, frankly, you should) then the following should provide the same (ip outputs CIDR addresses):

print ${$(ip -o -4 a s eth0)[4]}

we could also pass a qualifier to take only the IP and not the (CIDR) mask

print ${$(ip -o -4 a s eth0)[4]:h}

or, similarly, for the MAC address:

print ${$(ip l l eth0)[15]}

print -rl **/*(.Lm+100)
2013-03-23 16:18:25
User: khayyam
Tags: zsh
0

zsh:

"**/*" = recursive

"(.Lm+100)" "." = files, "L" = filesize glob qualifier, "m" = mb, "+100" = 100

print -rl **/*(.m0)
2013-03-23 16:05:49
User: khayyam
Tags: zsh
0

example of zsh globbing and glob qualifiers:

"**/*" recursive

(.m0) '.' = regular file, 'm0' = modified zero days (so, today).

zmv '* *' '$f:gs/ /_'
for f ([0-9].txt) zmv $f '${(l:1::0:)}'$f
2013-03-22 01:53:42
User: khayyam
Tags: zsh
0

zsh: add leading zero ... altogether pointless, as there can only be a maximum of 10 'single digit' files, and so a maximum of 10 files the command can act on. Padding further zeros will produce '0010', '001' and so break sequance. The only proper method is to itterate the numbers like so:

i=1; for f (*) zmv $f '${(l:3::0:)$((++i))}'.txt

but this has the unfortunate side effect of incrementing the values by 1 ... which may not be desirable.

chmod u+x **/*.sh
ls -Sh **/*(.Lm+100) | tail -5
2013-03-21 20:22:11
User: khayyam
Functions: ls tail
Tags: tail ls zsh
1

zsh: list of files sorted by size, greater than 100mb, head the top 5. '**/*' is recursive, and the glob qualifiers provide '.' = regular file, 'L' size, which is followed by 'm' = 'megabyte', and finally '+100' = a value of 100

zmv '(*).txt' '$1.csv'
2013-03-21 02:28:30
User: khayyam
Tags: zsh
0

Requires zsh. You might also do the following:

for (*.txt) mv $i ${i/.txt/.csv}

or I imagine the following will work in bash:

for i in "*.txt" ; do mv $i ${i/.txt/.csv} ; done

i=1; for f (*.jpg) zmv $f '${(l:3::0:)$((++i))}'$f
2013-03-21 01:51:28
User: khayyam
Tags: zsh
0

zsh example using a 'for' loop and arithmetic expression:

files matching the pattern '*.jpg' are renamed with a 3 digit prefix, keeping the previous filename and suffix.

perl -MModule::Name\ 9999 -e 1
2013-01-15 22:51:39
User: three18ti
Functions: perl
Tags: bash perl zsh
-1

This attempts to load a Perl Module (-M flag) and use version 9999, since no module has a version this high, Perl exits either a) telling you the version of the module installed or b) tells you it can't find the module.

if [ -n "$SSH_CONNECTION" ]; then export TMOUT=300; fi
2012-11-03 18:39:25
User: Testuser_01
Functions: export
0

This will check if a user is logged in using ssh and will log out the user automatically after the specified time in seconds without data retrieval on the server side.

Will work with bash and zsh so put it into your sourced shell file on the server side.

Be aware that users can change this themselves as it's just a envoronment variable.

trap "source ~/.zshrc" USR1
2012-11-03 07:24:03
User: ppaschka
Functions: trap
Tags: bash zsh
5

Put this in your zshrc, source it, then run 'pkill -usr1 zsh' to source it in all open terminals. Also works with bash. More info: http://www.reddit.com/r/commandline/comments/12g76v/how_to_automatically_source_zshrc_in_all_open/

rm -v *.(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(.e/'[[ -f ${REPLY:r}.tex ]]'/)
2012-09-18 20:49:28
User: xro
Functions: rm
Tags: rm zsh latex test
0

Uses zsh globbing syntax to safely remove all the files known to be generated by LaTeX, but only if there is actually a .tex source file with the same basename present. So we don't accidentally delete a .nav .log or .out file that has nothing to do with LaTeX, e/'[[ -f ${REPLY:r}.tex ]]'/ actually checks for the existance of a .tex file of the same name, beforehand.

A different way to do this, would be to glob all *.tex files and generate a globbing pattern from them:

TEXTEMPFILES=(*.tex(.N:s/%tex/'(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(.N)'/)) ; rm -v ${~TEXTEMPFILES}

or, you could use purge() from grml-etc-core ( http://github.com/grml/grml-etc-core/blob/master/usr_share_grml/zsh/functions/purge )

rmdir --ignore-fail-on-non-empty -p **/*(/^F)
2012-09-18 20:28:46
User: xro
Functions: rmdir
0

recursively delete empty directories and directories which only contain empty directories using zsh globbing syntax. ** is for recursive globbing. *(/^F) matches all entries that are directories which are not full.

If you only want to delete empty directories and not those directories which contained only empty directories and will be empty afterwards, just leave out the options to rmdir:

rmdir **/*(/^F)
rm **/*(-@)
2012-09-18 20:18:57
User: xro
Functions: rm
Tags: rm symlinks zsh
1

recursively deletes all broken symlinks using zsh globbing syntax.

end_w_nl() { [[ $(tail -c1 $1 | xxd -ps) == 0a ]] }
2012-09-18 20:11:29
User: xro
Functions: tail
Tags: tail xxd zsh
0
end_w_nl filename

will check if the last byte of filename is a unix newline character. tail -c1 yields the file's last byte and xxd converts it to hex format.