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 ls from sorted by
Terminal - Commands using ls - 448 results
ls -a | egrep "^\.\w"
2009-11-11 18:19:56
User: kulor
Functions: egrep ls
Tags: egrep ls dotfiles
-2

trying to copy all your dotfiles from one location to another, this may help

ls -1rSA | tail
2009-10-27 15:34:59
User: rbossy
Functions: ls
-5

This will only work on files since ls won't tell the size of a directory contents.

Note that the first switch is the digit one, not the letter ell.

pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null
2009-10-22 12:32:11
User: syladmin
Functions: cd echo ls sed
Tags: permissions
0

Can easily be scripted in order to show permission "tree" from any folder. Can also be formated with

column -t

{ pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null ; } | column -t

from http://www.commandlinefu.com/commands/view/3731/using-column-to-format-a-directory-listing

ls | xargs -n1 gzip
a=($(ls *html)) && a=${a[$(expr ${#a[@]} - 1)]} && rm $a
2009-10-12 16:40:06
Functions: expr ls rm
-3

plays with bash arrays. instead of storing the list of files in a temp file, this stores the list in ram, retrieves the last element in the array (the last html file), then removes it.

ls -d */
2009-10-08 22:07:22
User: brianmuckian
Functions: ls
37

-d: list directory entries instead of contents, and do not dereference symbolic links

ls -F|grep /
2009-10-08 16:35:15
User: romulusnr
Functions: grep ls
-1

No need for -l and the output can be sent directly into another function expecting directory names.

(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ ; ls -l | sed 1d) | column -t
ls -alh #mycomment
2009-10-06 13:55:06
Functions: ls
5

Comments can be used directly on the command line so I can save in the history a brief description of what command does.

ls [FILENAME] | xargs openssl sha1
2009-10-03 02:05:43
User: m00dimus
Functions: ls xargs
1

List files and pass to openssl to calculate the hash for each file.

ls -A
ls .??*
2009-09-29 14:40:37
User: Paula
Functions: ls
-5

Works 99.9% of the time; so far never required a more complex expression in manual input.

ls .[!.]*
2009-09-29 13:50:13
User: danam
Functions: ls
-3

Although rm is protected against it, there are many commands that would wreak havoc on entering the obvious ".*" to address "dot-files". This sweet little expression excludes the dirs "." and ".." that cause the problems.

enscript jrandom.txt -o - | ps2pdf - ~/tmp/jrandom.pdf (from file) or: ls | enscript -o - | ps2pdf - ~/tmp/ls.pdf (from stdout)
tar -cvf /dev/null . | while read i; do ls -l $i; done
2009-09-16 16:59:15
User: lbonanomi
Functions: ls read tar
-8

I find the ouput of ls -lR to be un-satisfying (why is the path data up there?) and find syntax to be awkward. Running 'du -a' means you will have likely to trim-off filesize data before feeding filenames to the next step in the pipe.

ls -lt|grep ^-|awk 'NR>5 { print $8 }'|xargs -r rm
ls -t | tail +6 | xargs rm
ls -t | awk 'NR>5 {system("rm \"" $0 "\"")}'
2009-09-16 04:58:08
User: haivu
Functions: awk ls
Tags: awk ls
-2

I have a directory containing log files. This command delete all but the 5 latest logs. Here is how it works:

* The ls -t command list all files with the latest ones at the top

* The awk's expression means: for those lines greater than 5, delete.

cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin
2009-09-09 18:09:14
User: kamathln
Functions: cat grep ls readlink rm xargs
Tags: Debian find dpkg
11

This helped me find a botnet that had made into my system. Of course, this is not a foolproof or guarantied way to find all of them or even most of them. But it helped me find it.

ls -ldct /lost+found |awk '{print $6, $7}'
ls -lct /etc/ | tail -1 | awk '{print $6, $7, $8}'
2009-09-04 16:52:50
User: peshay
Functions: awk ls tail
5

shows also time if its the same year or shows year if installed before actual year and also works if /etc is a link (mac os)

ls -lct /etc | tail -1 | awk '{print $6, $7}'
2009-09-03 10:26:37
User: MrMerry
Functions: awk ls tail
10

Show time and date when you installed your OS.

ls -shF --color
2009-09-03 05:45:33
User: Viperlin
Functions: ls
-3

use manpages, they give you "ultimate commands"

"ls -SshF --color" list by filesize (biggest at the top)

"ls -SshFr --color" list by filesize in reverse order (biggest at the bottom)

ls *.c | while read F; do gcc -Wall -o `echo $F | cut -d . -f 1 - ` $F; done
2009-08-28 13:01:56
User: pichinep
Functions: cut gcc ls read
-7

Compile *.c files with "gcc -Wall" in actual directory, using as output file the file name without extension.

ls | while read f; do mv "$f" "${f// /_}";done