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
cd "~/Music/iTunes/iTunes Media/Mobile Applications";ls > filepath
ls -tl --time-style=full-iso
ls -l | sort +4n for ascending order or ls -l | sort +4nr for descending order
ls **/*.c(|pp)~(unit-test|android)/*
ls ${PATH//:/ }
2012-04-26 19:45:52
User: Zulu
Functions: ls
9

List all commands present on system by folder.

PATH contains all command folder separated by ':'. With ${PATH//:/ }, we change ':' in space and create a list of folder for ls command.
ls -d1 pattern*/ | wc -l
ls -s|grep -E "^ *0"|sed "s/^ *0 //g"|xargs -i rm "{}"
2012-04-18 14:50:46
User: glaudiston
Functions: grep ls rm sed xargs
-8

Remove all zero size files from current directory. Its a not recursive option like:

find . -size 0c -exec rm {} \;

find ./src -type d -name "Entity" | xargs ls -A | cut -d . -f1 | sed 's_^_app/console doctrine:generate:entities YourOwnBundleName:_'
2012-04-11 21:28:02
User: renoirb
Functions: cut find ls sed xargs
0

When I do a major change in my entities, I want to find a way to find all my Entities names and create the commande for me.

So instead of doing ls src/Your/OwnBundle... and then do it manually, this helps a lot.

man -t ls > ls.ps && pdf2ps ls.ps && rm ls.ps
du -s $(ls -l | grep '^d' | awk '{print $9}') | sort -nr
ls -r | ?{-not $_.psiscontainer} | group extension | select name, count, @{n='average'; e={($_.group | measure -a length).average}} | ft -a @{n='Extension'; e={$_.name}}, count, @{n='Average Size (KB)'; e={$_.average/1kb}; f='{0:N2}'}
2012-03-13 17:58:10
User: brianpeiris
Functions: ls
Tags: PowerShell
0

Here's an annotated version of the command, using full-names instead of aliases. It is exactly equivalent to the short-hand version.

# Recursively list all the files in the current directory.

Get-ChildItem -Recurse |

# Filter out the sub-directories themselves.

Where-Object { return -not $_.PsIsContainer; } |

# Group the resulting files by their extensions.

Group-Object Extension |

# Pluck the Name and Count properties of each group and define

# a custom expression that calculates the average of the sizes

# of the files in that group.

# The back-tick is a line-continuation character.

Select-Object `

Name,

Count,

@{

Name = 'Average';

Expression = {

# Average the Length (sizes) of the files in the current group.

return ($_.Group | Measure-Object -Average Length).Average;

}

} |

# Format the results in a tabular view, automatically adjusted to

# widths of the values in the columns.

Format-Table -AutoSize `

@{

# Rename the Name property to something more sensible.

Name = 'Extension';

Expression = { return $_.Name; }

},

Count,

@{

# Format the Average property to display KB instead of bytes

# and use a formatting string to show it rounded to two decimals.

Name = 'Average Size (KB)';

# The "1KB" is a built-in constant which is equal to 1024.

Expression = { return $_.Average / 1KB };

FormatString = '{0:N2}'

}

ls | grep -i mp3 | sort -R | sed -e 's/.*/"&"/' | xargs mpg123
2012-03-10 20:51:36
User: retrodanny
Functions: grep ls sed sort xargs
1

* grep -i leaves only mp3 files (case insentitive)

* sort -R randomizes list (may use GNU 'shuf' instead).

* the sed command will add double quotes around each filename (needed if odd characters are present)

ls -Rl dir1/ > /tmp/dir1.ls; ls -Rl dir2/ > /tmp/dir2.ls; meld /tmp/dir1.ls /tmp/dir2.ls
2012-03-04 13:06:55
User: joeseggiola
Functions: ls
0

Compare the ls -Rl output of two directories in meld (you can also use diff -y instead of meld).

find <directory> -type f -printf "%T@\t%p\n"|sort -n|cut -f2|xargs ls -lrt
ls -ltr --directory $(find . -regex "./.*[^/]*\'" -type f | xargs -n 1 dirname | sort | uniq)
2012-03-02 03:48:47
User: pdkl95
Functions: dirname find ls sort xargs
0

This let me find some a set of modifications that were made to a rather large tree of files, where the file-names themselves were not unique (actually: insanely redundant and useless. "1.dat 2.dat ..."). Pruning down to last-branch brough things back to the "project-name" scope, and it's then easy to see which branches of the tree have recently changed, or any other similar search.

Ideally, it should sort the directories by the mtime of the most recent *file* *inside* the directory, but that's probably outside the scope of a (sane...) command line.

ls|grep .mp3 >list.txt; while read line; do newname=`echo $line|sed 's/\ /-/g'|sort`; newname=`echo $newname|tr -s '-' `; echo $newname; echo $newname>> tracklist.txt;mv "$line" "$newname"; done <list.txt; rm list.txt
ls -t1 $* | head -1 ;
2012-02-10 22:13:24
Functions: head ls
0

Returns the most recently modified file in the current (or specified) directory. You can also get the oldest file, via:

ls -t1 $* | tail-1 ;

ls -l | gawk -v FIELDWIDTHS='1 3 3 3' '{print $2}'
find ./ -type f -size +100000k -exec ls -lh {} \; 2>/dev/null| awk '{ print $8 " : " $5}'
2012-01-21 04:19:35
User: Goez
Functions: awk find ls
0

This command does a basic find with size. It also improves the printout given (more clearer then default)

Adjusting the ./ will alter the path.

Adjusting the "-size +100000k" will specify the size to search for.

ls -t | head
2012-01-17 16:28:32
User: scottlinux
Functions: ls
Tags: tail ls head,
2

This will quickly display files last changed in a directory, with the newest on top.

ls -l | sed -e 's/--x/1/g' -e 's/-w-/2/g' -e 's/-wx/3/g' -e 's/r--/4/g' -e 's/r-x/5/g' -e 's/rw-/6/g' -e 's/rwx/7/g' -e 's/---/0/g'
lso(){ jot -w '%04d' 7778 0000 7777 |sed '/[89]/d;s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \ |sh \ |{ echo "lso(){";echo "ls \$@ \\";echo " |sed '";sed 's, ,@,2;s,@.*,,;s,\(.* \)\(.*\),s/\2/\1/,;s, ,,';echo \';echo };};}
2012-01-08 05:48:24
User: argv
Functions: chmod echo ls sed sh
0

this requires the use of a throwaway file.

it outputs a shell function.

assuming the throwaway file is f.tmp

usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...

notes:

credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.

many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.

one can achieve the same sort of octal output with stat(1), if that utility is available.

here's another version to account for systems with seq(1) instead of jot(1):

lso(){

case $# in

1)

{ case $(uname) in

FreeBSD)

jot -w '%04d' 7778 0000 7777 ;;

*)

seq -w 0000 7777 ;;

esac; } \

|sed '

/[89]/d

s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \

|sh \

|{

echo "lso(){";

echo "ls \$@ \\";

echo " |sed '";

sed '

s, ,@,2;

s,@.*,,;

s,\(.* \)\(.*\),s/\2/\1/,;

s, ,,';

echo \';

echo };

};

;;

*)

echo "usage: lso tmp-file";

;;

esac;

}

this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).

1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.

ls | view -
2012-01-04 07:18:44
User: lefada
Functions: ls
Tags: vim
0

view does not enable the buffer because it opens in read-only, so it does the same

ls -d1 $PWD/*
ls | vim +'set bt=nowrite' -