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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands by khayyam from sorted by
Terminal - Commands by khayyam - 18 results
zmv '(*.*)(.*)' '${1//./_}$2'
2013-04-06 01:57:34
User: khayyam
Tags: zsh

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

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

Example of using zsh glob qualifier ...

"." = files

"f:" = files with access rights matching:

o+w = other plus write

vim sftp://[[email protected]]host.domain.tld:[port]/[path/][file]
2013-03-24 01:31:20
User: khayyam
Functions: vim
Tags: vim

vim can open ssh/sftp and ftp connections for file editing using 'netrw'. If no path or file is provided vim opens the directory as a filelist.

See: :help netrw.

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

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

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

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


"**/*" = 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

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

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.

(($1 > 0)) && echo "var is a number"
2013-03-22 01:15:00
User: khayyam
Functions: echo

calculate if "$1" is a number ... decimals included :)

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

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

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

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.

ack --type=php <string>
2013-03-20 20:27:31
User: khayyam
Tags: ack

The 'sample output' shows the file(s) found and the line at which '' was found (for the example I used the 'string'.)