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 - 452 results
ls -atr /home/reports/*.csv -o --time-sty=+%s | tail -1 | awk '{print systime()-$5}'
ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
2011-05-01 13:32:58
User: hute37
Functions: awk ls printf xargs
Tags: awk mv xargs
3

rename file name with fixed length nomeric format pattern

ls | sed 'p;s/foo/bar/' | xargs -n2 mv
ls * | sed -e 'p;s/foo/bar/' | xargs -n2 mv
2011-04-30 00:21:10
User: windfold
Functions: ls sed xargs
3

Renames all files in a directory named foo to bar.

foobar1 gets renamed to barbar1

barfoo2 gets renamed to barbar2

fooobarfoo gets renamed to barobarfoo

NOTE: Will break for files with spaces AND new lines AND for an empty expansion of the glob '*'

find -L / -samefile /path/to/file -exec ls -ld {} +
2011-04-27 06:14:15
User: eightmillion
Functions: find ls
17

This command finds and prints all the symbolic and hard links to a file. Note that the file argument itself be a link and it will find the original file as well.

You can also do this with the inode number for a file or directory by first using stat or ls or some other tool to get the number like so:

stat -Lc %i file

or

ls -Hid file

And then using:

find -L / -inum INODE_NUMBER -exec ls -ld {} +
eval ls -l /proc/{$(pgrep -d, COMMAND)}/cwd
2011-04-14 13:41:58
User: splante
Functions: eval ls
3

This is an alternative to another command using two xargs. If it's a command you know there's only one of, you can just use:

ls -l /proc/$(pgrep COMMAND)/cwd
ls -lth podcasts/
yt-pl2mp3() {umph -m 50 $1 | cclive -f mp4_720p; IFS=$(echo -en "\n\b"); for track in $(ls | grep mp4 | awk '{print $0}' | sed -e 's/\.mp4//'); do (ffmpeg -i $track.mp4 -vn -ar 44100 -ac 2 -ab 320 -f mp3 $track.mp3); done; rm -f *.mp4}
2011-03-20 14:43:20
User: sattellite
Functions: awk echo grep ls rm sed
0

umph is parsing video links from Youtube playlists ( http://code.google.com/p/umph/ )

cclive is downloading videos from Youtube ( http://cclive.sourceforge.net/ )

Example:

yt-pl2mp3 7AB74822FE7D03E8
find / -type f -size +20000k -exec ls -lh {} \; | awk '{printf $9} {for (i=10;i<=NF;i++) {printf " "$i}} {print ": "$5}'
ls -1 | shuf -n 1
echo Selected $(ls -1 | sort -R | head -n 1)
ls -1 | sort -R | sed -n 's/^/Selected /;1p'
ls -1 | awk 'BEGIN{srand()} {x[NR] = $0} END{print "Selected", x[1 + int(rand() * NR)]}'
2011-03-13 20:05:06
User: saibbot
Functions: awk ls
Tags: awk random
-3

I use this command to select a random movie from my movie collection..

ls -alt /directory/ | awk '{ print $6 " " $7 " -- " $9 }'
$ right(){ l="$(cat -)"; s=$(echo -e "$l"| wc -L); echo "$l" | while read l;do j=$(((s-${#l})));echo "$(while ((j-->0)); do printf " ";done;)$l";done;}; ls --color=none / | right
center(){ l="$(cat -)"; s=$(echo -e "$l"| wc -L); echo "$l" | while read l;do j=$(((s-${#l})/2));echo "$(while ((--j>0)); do printf " ";done;)$l";done;}; ls --color=none / | center
2011-02-14 16:50:35
User: glaudiston
Functions: echo ls read wc
0

Center the output text in max line length of buffered output pipe;

ls | grep -Eo "\..+" | sort -u
folder=0;mkdir $folder; while find -maxdepth 1 -type f -exec mv "{}" $folder \; -quit ; do if [ $( ls $folder | wc -l ) -ge 100 ]; then folder=$(( $folder + 1 )); mkdir $folder; fi ; done
2011-02-11 21:28:01
User: Juluan
Functions: find ls mkdir mv wc
-1

If you have a folder with thousand of files and want to have many folder with only 100 file per folder, run this.

It will create 0/,1/ etc and put 100 file inside each one.

But find will return true even if it don't find anything ...

ls -Xp | grep -Eo "\.[^/]+$" | sort | uniq
2011-02-10 20:47:59
User: Amarok
Functions: grep ls sort
Tags: uniq ls grep
4

Works on current directory, with built-in sorting.

ls -l --time-style=+"%Y-%m-%d %H:%M:%S"
2011-02-10 17:15:37
User: bartonski
Functions: ls
4

the --time-style argument to 'ls' takes several possible modifiers: full-iso, long-iso, iso, locale, +FORMAT.

The +FORMAT modifier uses the same syntax as date +FORMAT.

--time-style=+"%Y-%m-%d %H:%M:%S" strikes a happy medium between accuracy and verbosity:

ls -lart --time-style=long-iso

doesn't show time down to the nearest second,

ls -lart --time-style=full-iso

displays time to 10E-9 second resolution, but with no significant digits past the full seconds, also showing the timezone:

-rw-r--r-- 1 bchittenden bchittenden 0 2011-02-10 12:07:55.000000000 -0500 bar
dpkg -S $( which ls )
exec 3<&0; ls -1N | while read a; do echo "Rename file: $a"; read -e -i "$a" -p "To: " b <&3 ; [ "$a" == "$b" ] || mv -vi "$a" "$b"; done
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 ...