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 - 455 results
ls > ls; paste ls ls > ren; nano ren; sed 's/^/mv /' ren|bash; rm ren ls
2011-02-08 17:07:13
User: romulusnr
Functions: ls paste rm sed
-2

Instead of tedious manual mv commands and tabbing, this routine creates a file listing all the filenames in the PWD twice, edit the second instance on each line to the new name, then save the file, the routine does the rest. Feel free to replace nano with your holy war editor of choice.

You will get a lot of "mv: 'x' and 'x' are the same file" warnings, these could be cleaned up but the routine works.

ls -ai | grep filename | find . -inum `awk '{print $1}'` -exec rm {} \;
2011-02-04 10:29:54
User: YogiBare
Functions: find grep ls rm
-1

This command, when run from the directory containing "filename", will remove the file and any hard or symbolic links to the file.

IFS=$'\n'; i=1; ls -lt *mp3 | cut -d ":" -f2 | cut -d " " -f2- | while read f; do mv "$f" $(echo "$i"."$f"); ((i++)); done
2011-01-22 00:21:12
User: m1cawber
Functions: cut echo ls mv read
0

i use this after ripping internet radio streams to number the files as they originally played (even though streamripper can do this with -q).

to number other types of files, or all files, just change the *mp3. to rename directories only you could use

... ls -lt | grep ^d | cut -d ":" -f2 | cut -d " " -f2- | while read ...

ls -S|head -1find
ls /usr/bin | shuf -n 1
2011-01-07 22:09:12
User: pebkac
Functions: ls
6

This command will show an random command. this is useful if you want to explore various random commands.

ls -la | tee ~/log.txt
ls | grep '^[A-Z0-9]*$'
2010-12-19 21:45:53
User: b_t
Functions: grep ls
0

Some source package have many 'README' kind of files, among many other regular files/directories. This command could be useful when one wants to list only 'README' kind of files among jungle of other files. (e.g. I came across this situation after downloading source for module-init-tools)

Warning: This command would miss a file like => README.1 (or one with spaces in-between)

Corrections welcome.

ls *(.x)
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
2

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 ~