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 -F | grep '*$'
find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
ls $(grep cifs /etc/fstab | grep -v ^# |awk ' { print $2 } ') 1>/dev/null
2010-12-10 09:27:19
User: niall
Functions: awk grep ls
Tags: fstab cifs
1

It disturbs me when my logwatch report tells me a share or machine has disappeared, esp as mount isn't telling me what's gone. This command outputs to stderr the erroring cifs entries from fstab.

ls -1t --group-directories-first /path/to/dir/ | tail -n 1
2010-12-02 12:25:16
User: fpunktk
Functions: ls tail
1

reverse the sorting of ls to get the newest file:

ls -1tr --group-directories-first /path/to/dir/ | tail -n 1

Problems:

If there are no files in the directory you will get a directory or nothing.

for i in $(ls *.flv); do ffmpeg2theora -v 6 --optimize $i; done
2010-12-01 14:13:31
User: joenjeru
Functions: ls
0

Do you have a large library of flv's you have picked up over the years using FlashGot Firefox plugin?

Do you want to be able to convert them to Ogg Theora (video) at once?

Try out this script...

ls /var/log/ |wc -l
ls --[TAB][TAB]
2010-11-18 15:31:28
User: strzel_a
Functions: ls
1

Use it for command like : mkdir, chown, ls, less...

ls -l
ls | grep .gz >> list.txt && cat list.txt | while read x ; do gunzip -d $x ; done && rm -rf list.txt
zcat -f $(ls -tr access.log*)
2010-10-30 22:54:54
User: dooblem
Functions: ls zcat
Tags: logs zcat
1

with zcat force option it's even simpler.

ls |tee >(grep xxx |wc >xxx.count) >(grep yyy |wc >yyy.count) |grep zzz |wc >zzz.count
ls -d1a /var/www/*/web | xargs du -hs
2010-10-18 17:16:23
User: DRoBeR
Functions: du ls xargs
-1

Calculate foldersize for each website on an ISPConfig environment. It doesn't add the jail size. Just the "public_html".

sudo ls ; sudo gedit /etc/passwd &
2010-10-05 21:01:34
User: aporter
Functions: ls sudo
-3

Take advantage of sudo keeping you authenticated for ~15 minutes.

The command is a little longer, but it does not require X (it can run on a headless server).

fname=$1;f=$(ls -la $fname);fsz=$(echo $f|awk '{ print $5 }');nrrec=$(wc -l $fname|awk '{ print $1 }');recsz=$(expr $fsz / $nrrec);echo "$recsz"
2010-09-14 08:40:22
User: vuurst
Functions: awk echo expr ls wc
0

command was too long...

this is the complete command:

fname=$1; f=$( ls -la $fname ); if [ -n "$f" ]; then fsz=$( echo $f | awk '{ print $5 }' ); if [ "$fsz" -ne "0" ]; then nrrec=$( wc -l $fname | awk '{ print $1 }' ); recsz=$( expr $fsz / $nrrec ); echo "$recsz"; else echo "0"; fi else echo "file $fname does not exist" >&2; fi

First the input is stored in var $fname

The file is checked for existance using "ls -lart".

If the output of "ls -lart" is empty, the error message is given on stderr

Otherwise the filelength is taken from the output of "ls -lart" (5th field)

With "wc -l" the number of records (or lines) is taken.

The record size is filelength devided by the number of records.

please note: this method does not take into account any headers, variable length records and only works on ascii files where the records are sperated by 0x0A (or 0x0A/0x0D on MS-DOS/Windows).

ls *.JPG | cut -d . -f 1 | xargs -L1 -i convert -resize 684 {}.JPG {}.jpg
ls -l $HOME
ls -l ~
ls ~
2010-09-12 10:45:01
User: harish
Functions: ls
-18

It will list all files in your home directory even if your current directory is not home directory

ls -lAhutr
2010-09-07 05:49:46
Functions: ls
-7

This is a simple command, but useful when you don't remember what episode need to see :D

ls /dev/disk/by-uuid/ -alh
ls -w 1 > list.m3u
2010-08-27 07:03:17
User: Tungmar
Functions: ls
2

A short variant if you have only one directory whit only audio files in it.

find . -type f -mtime -1 \! -type d -exec ls -l {} \;
for crt in $(locate -r '.+\.crt' | grep -v "/usr/share/ca-certificates/"); do ls -la $crt; done
2010-08-23 12:22:48
User: udog
Functions: grep locate ls
Tags: openssl locate
0

Finds all cert files on a server and lists them, finding out, which one is a symbolic link and which is true.

You want to do this when a certificate expires and you want to know which files to substitute with the new cert.

man $(ls -1 /usr/share/man/man?/ | shuf -n1 | cut -d. -f1)
2010-08-20 23:36:10
User: dooblem
Functions: cut ls man
Tags: man sort shuf
-3

Another one.

Maybe not the quicker because of the sort command, but it will also look in other man sections.

updated with goodevilgenius 'shuf' idea

man $(ls /bin | shuf | head -1)
2010-08-20 23:12:51
Functions: head ls man
Tags: man
1

I'm not sure why you would want to do this, but this seems a lot simpler (easier to understand) than the version someone submitted using awk.