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 files from sorted by
Terminal - Commands tagged files - 36 results
for i in `find -L /var/ -wholename \*log\* -type d`; do COUNT=`ls -1U $i | wc -l`; if [ $COUNT -gt 10 ]; then echo $i $COUNT; fi; done
find -maxdepth 1 -type f -newermt "00:00" -printf "%f\n" | sort
2013-03-23 12:50:01
User: TetsuyO
Functions: find
Tags: sort find files
-2

Finds files modified today since 00:00, removes ugly dotslash characters in front of every filename, and sorts them.

*EDITED* with the advices coming from flatcap (thanks!)

ls -al | sort +4n
2012-06-26 19:20:05
User: ankush108
Functions: ls sort
Tags: size sort files
0

ls -al gives all files, sort +4n sorts by 5th field numerically

pdftk A=1.pdf B=2.pdf C=3.pdf cat C A output CA.pdf
pdftk 1.pdf 2.pdf cat output 12.pdf
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 ;

rename s/^/./ *
file <filename>
2011-11-19 23:39:29
User: lordtoran
Functions: file
-1

Some shell newbies don't know this very handy file management related command so I decided to include it here.

You need to have the "file" package installed.

find . -iname "*.mp3" -type f -print0 | xargs -0 -I '{}' mv {} /new/path/to/mp3/{}
2011-07-18 05:28:32
User: paulochf
Functions: find mv xargs
2

Use case: folder with flac files with tree structure ../artist/album/number-title.flac

1) convert flac->mp3 in the same folder: http://www.commandlinefu.com/commands/view/6341/convert-all-.flac-from-a-folder-subtree-in-192kb-mp3

2) search for mp3 files and recreate tree structure to another path: http://www.commandlinefu.com/commands/view/8853/copy-selected-folder-found-recursively-under-src-retaining-the-structure

3) move all mp3 files to that new folder: this command

vim `find . -iname '*.php'`
2011-05-11 01:19:28
User: wsams
Functions: vim
0

In this case, we'll be editing every PHP file from the current location down the tree.

You can show all the files in the vim buffer with :buffers which outputs something like,

:buffers

1 %a "./config/config.php" line 1

2 "./lib/ws-php-library.php" line 0

3 "./lib/css.php" line 0

4 "./lib/mysqldb.class.php" line 0

5 "./lib/config.class.php" line 0

6 "./lib/actions.php" line 0

Press ENTER or type command to continue

If you'd like to edit ./lib/mysqldb.class.php for example, enter :b4 anytime you're editing a file. You can switch back and forth.

for file in `cat urls.txt`; do echo -n "$file " >> log.txt; curl --head $file >> log.txt ; done
2010-10-19 02:54:13
User: Glutnix
Functions: echo file
-1

urls.txt should have a fully qualified url on each line

prefix with

rm log.txt;

to clear the log

change curl command to

curl --head $file | head -1 >> log.txt

to just get the http status

tempfile=$(/bin/mktemp)
2010-06-05 21:36:49
User: takeshin
3

To create directory, use:

tempdir=$(/bin/mktemp -d)
vim -r 2>&1 | grep '\.sw.' -A 5 | grep 'still running' -B 5
2010-04-17 19:43:35
User: rkulla
Functions: grep vim
3

Catches .swp, .swo, .swn, etc.

If you have access to lsof, it'll give you more compressed output and show you the associated terminals (e.g., pts/5, which you could then use 'w' to figure out where it's originating from): lsof | grep '\.sw.$'

If you have swp files turned off, you can do something like: ps x | grep '[g,v]im', but it won't tell you about files open in buffers, via :e [file].

ls | egrep -v "[REGULAR EXPRESSION]" | xargs rm -v
2010-04-01 02:40:40
User: Saxphile
Functions: egrep ls rm xargs
Tags: files rm
-1

This is a slight variation of an existing submission, but uses regular expression to look for files instead. This makes it vastly more versatile, and one can easily verify the files to be kept by running ls | egrep "[REGULAR EXPRESSION]"

find . -type f |sed "s#.*/##g" |sort |uniq -c -d
2010-02-17 11:59:54
User: shadycraig
Functions: find sed sort uniq
0

Useful for C projects where header file names must be unique (e.g. when using autoconf/automake), or when diagnosing if the wrong header file is being used (due to dupe file names)

find /path/to/dir -type f -printf "%T@|%p\n" 2>/dev/null | sort -n | tail -n 1| awk -F\| '{print $2}'
newest () { find ${1:-\.} -type f |xargs ls -lrt ; }
newest () { DIR=${1:-'.'}; CANDIDATE=`find $DIR -type f|head -n1`; while [[ ! -z $CANDIDATE ]]; do BEST=$CANDIDATE; CANDIDATE=`find $DIR -newer "$BEST" -type f|head -n1`; done; echo "$BEST"; }
2010-02-04 12:40:44
User: shadycraig
Functions: echo head
1

Works recusivley in the specified dir or '.' if none given.

Repeatedly calls 'find' to find a newer file, when no newer files exist you have the newest.

In this case 'newest' means most recently modified. To find the most recently created change -newer to -cnewer.

find -type d -name ".svn" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".svn" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
2010-01-28 09:45:29
User: 2chg
Functions: find md5sum sort uniq xargs
2

Improvement of the command "Find Duplicate Files (based on size first, then MD5 hash)" when searching for duplicate files in a directory containing a subversion working copy. This way the (multiple dupicates) in the meta-information directories are ignored.

Can easily be adopted for other VCS as well. For CVS i.e. change ".svn" into ".csv":

find -type d -name ".csv" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".csv" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
find . ! -name <FILENAME> -delete
2010-01-13 23:29:51
User: haplo
Functions: find
Tags: files rm
8

it will remove everything except the file names matching you can use also use wildcards

( shopt -s extglob; rm !(<PATTERN>) )
2010-01-13 16:43:15
User: sputnick
Functions: rm
Tags: files rm glob shopt
7

Remove everything except that file with shell tricks inside a subshell to avoid changes in the environment.

help shopt
newest () { candidate=''; for i in "$@"; do [[ -f $i ]] || continue; [[ -z $candidate || $i -nt $candidate ]] && candidate="$i"; done; echo "$candidate"; }
2009-10-29 17:35:01
User: johnraff
Functions: echo
Tags: bash files
1

Usage example:

newest Desktop/*

Replace "-nt" with "-ot" for oldest.

Run

shopt -s dotglob

first to include dotfiles.

git show --relative --pretty=format:'' --name-only HASH
2009-08-14 03:36:03
User: lingo
Tags: git files list
2

Lists ONLY the files changed by the given HASH/HEAD/list of hashes, etc. The message, commit ID, author, etc. is not included

cat files.txt | xargs tar -cv | tar -x -c $DIR/
2009-08-06 22:55:21
User: lingo
Functions: cat tar xargs
0

If you want certain files out of a directory hierarchy, this will copy just the listed files, but will create the directory hierarchy in the new location ($DIR/)

tar -C <source> -cf - . | tar -C <destination> -xf -