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 tagged ls from sorted by
Terminal - Commands tagged ls - 102 results
rm -R `ls | egrep -v 'dir1|dir2|file1'`
find * -type d -maxdepth 0
2011-08-07 06:04:50
User: edogawaconan
Functions: find
Tags: find ls grep sh
1

Alternatively,

ls -F | grep /\$

but will break on directories containing newlines. Or the safe, POSIX sh way (but will miss dotfiles):

for i in *; do test -d "./$i" && printf "%s\n" "$i"; done
ls -1d */
ls -l | grep ^d | sed 's:.*\ ::g'
2011-08-06 23:52:46
User: LinuxMan
Functions: grep ls sed
Tags: bash sed ls grep
-10

Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know

ls -Fhitlar
2011-07-11 10:29:34
User: ringzero
Functions: ls
Tags: ls
1

Was playing with the shell. It struck to me, just by rearranging the parameters, i was able to remember what they did and in a cool way.

Enter the 'hitlar' mode.

bash-3.2$ ls -hitlar

Shows all items with inodes, in list view, human readable size, sorted by modification time in reverse,

bash-3.2$ ls -Fhitlar

Shows the same with classification info. Add the hitlar mode alias to your .bashrc.

bash-3.2$ echo "alias hitlar='ls -Fhitlar'" >> ~/.bashrc

bash-3.2$ hitlar

bash-3.2$ hitlar filename

ls -alt /directory/ | awk '{ print $6 " " $7 " -- " $9 }'
find . -type f | awk -F'.' '{print $NF}' | sort| uniq -c | sort -g
ls | grep -Eo "\..+" | sort -u
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 | 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.

xdg-open .
2010-10-05 04:20:31
User: schlaegel
-1

Opens the current working directory in the user's preferred application using freedesktop.org's xdg-open.

gnome-open .
2010-10-01 13:16:00
User: pahnin
-1

when working under a cli sometime you need to list the files with ls

but u can open gnome file browser with the command 'gnome-open .' under current directory

ls -l $HOME
ls -l ~
watch 'ls -tough --full-time *.vmdk'
2010-08-20 17:28:28
User: vRobM
Functions: watch
1

To monitor .vmdk files during snapshot deletion (commit) on ESX only (ESXi doesn't have the watch command):

1. Navigate to the VM directory containing .vmdk files.

# watch "ls -tough --full-time *.vmdk"

where:

-t sorts by modification time

-o do not list group information (to narrow the output)

-u sorts by access time

-g only here for the purpose to easily remember the created mnemonic word 'tough'

-h prints sizes in human readable format (e.g., 1K 234M 2G)

--full-time sets the time style to full-iso and does not list user information (to narrow the output)

optionally useful parameters to the watch command:

-d highlight changes between updates

-n seconds to wait between updates (default is 2)

-t turn off printing the header

man $(ls /bin | sed -n $((RANDOM % $(ls /bin | wc -l) + 1))p)
2010-08-20 17:15:33
User: putnamhill
Functions: ls man sed wc
Tags: man sed ls wc random
-2

Great idea camocrazed. Another twist would be to display a different man page based on the day of the year. The following will continuously cycle through all man pages:

man $(ls /bin | sed -n $(($(date +%j) % $(ls /bin | wc -l)))p)
ls --quoting-style={escape,shell,c}
ls | sed 's/.*/"&"/'
2010-08-17 15:38:51
User: putnamhill
Functions: ls sed
Tags: sed ls
-5

Looks like you're stuck with sed if your ls doesn't have a -Q option.

ls -Xp /path/to/dir | grep -Eo "\.[^/]+$" | uniq
2010-08-12 16:32:54
User: karpoke
Functions: grep ls
Tags: uniq ls grep
0

If we want files with more than one extension, like .tar.gz, only appear the latest, .gz:

ls -Xp /path/to/dir | grep -Eo "\.[^./]+$" | uniq
ls -l `which foo`
2010-07-09 01:34:02
User: adeverteuil
Functions: ls
1

You may also use the $(which foo) variant instead of backticks. I personnaly have an alias ll='ls -l'.

find / -type f -name *.tar.gz -size +10M -exec ls -l {} \;
2010-06-29 12:39:02
User: 0disse0
Functions: find ls
Tags: find ls exec rm type
0

Please be careful while executing the following command as you don?t want

to delete the files by mistake. The best practice is to execute the same

command with ls ?l to make sure you know which files will get deleted when

you execute the command with rm.

statt(){ C=c;stat --h|sed '/Th/,/NO/!d;/%/!d'|while read l;do p=${l/% */};[ $p == %Z ]&&C=fc&&echo ^FS:^;echo "`stat -$C $p \"$1\"` ^$p^${l#%* }";done|column -ts^; }
2010-06-11 23:31:03
User: AskApache
Functions: column read sed
3

This shows every bit of information that stat can get for any file, dir, fifo, etc. It's great because it also shows the format and explains it for each format option.

If you just want stat help, create this handy alias 'stath' to display all format options with explanations.

alias stath="stat --h|sed '/Th/,/NO/!d;/%/!d'"

To display on 2 lines:

( F=/etc/screenrc N=c IFS=$'\n'; for L in $(sed 's/%Z./%Z\n/'<<<`stat --h|sed -n '/^ *%/s/^ *%\(.\).*$/\1:%\1/p'`); do G=$(echo "stat -$N '$L' \"$F\""); eval $G; N=fc;done; )

For a similarly powerful stat-like function optimized for pretty output (and can sort by any field), check out the "lll" function

http://www.commandlinefu.com/commands/view/5815/advanced-ls-output-using-find-for-formattedsortable-file-stat-info

From my .bash_profile ->

http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'
7

I love this function because it tells me everything I want to know about files, more than stat, more than ls. It's very useful and infinitely expandable.

find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n' | sort -rgbS 50%

00761 drwxrw---x askapache:askapache 777:666 [06/10/10 | 06/10/10 | 06/10/10] [d] /web/cg/tmp

The key is:

# -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'

which believe it or not took me hundreds of tweaking before I was happy with the output.

You can easily use this within a function to do whatever you want.. This simple function works recursively if you call it with -r as an argument, and sorts by file permissions.

lsl(){ O="-maxdepth 1";sed -n '/-r/!Q1'<<<$@ &&O=;find $PWD $O -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'|sort -rgbS 50%; }

Personally I'm using this function because:

lll () { local a KS="1 -r -g"; sed -n '/-sort=/!Q1' <<< $@ && KS=`sed 's/.*-sort=\(.*\)/\1/g'<<<$@`; find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'|sort -k$KS -bS 50%; }

# i can sort by user

lll -sort=3

# or sort by group reversed

lll -sort=4 -r

# and sort by modification time

lll -sort=6

If anyone wants to help me make this function handle multiple dirs/files like ls, go for it and I would appreciate it.. Something very minimal would be awesome.. maybe like:

for a; do lll $a; done

Note this uses the latest version of GNU find built from source, easy to build from gnu ftp tarball. Taken from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

ls -rl --time-style=+%s * | sed '/^$/,/^total [0-9]*$/d' | sort -nk6
perl -e 'foreach (@ARGV) {@T=stat($_); print localtime($T[8])." - ".$_."\n"}'