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 file from sorted by
Terminal - Commands tagged file - 73 results
sed -n "/^.\{73,\}/p" < /path/to/file
2014-03-20 12:31:57
User: flatcap
Functions: sed
0

Filter out lines of input that contain 72, or fewer, characters.

"sed -n" : don't print lines by default

"/^.\{73,\}/" : find lines that start with 73 (or more) characters

"p" : print them

while read i; do [ ${#i} -gt 72 ] && echo "$i"; done < /path/to/file
2014-03-20 12:27:06
User: flatcap
Functions: echo read
1

Filter out lines of input that contain 72, or fewer, characters.

This uses bash only. ${#i} is the number of characters in variable i.

perl -nle 'print length,"\t",$_ if length > 37' < /path/to/input/file
pdftk *.pdf cat output merged.pdf
2014-03-02 01:53:37
User: o0110o
Functions: cat
2

Merge Multiple PDFs In Alphabetical Order

find . -mtime +30 -exec mv {} old/ \;
2014-02-09 23:05:41
User: minnmass
Functions: find mv
Tags: bash file
-2

Use find's built-in "exec" option to avoid having to do any weirdness with quoting.

for i in $(find . -mtime +30); do mv $i old/; done
2014-02-05 01:24:45
User: valferon
Functions: find mv
Tags: bash file
-3

Will move in that case every file in the current folder older than 30 days to the "old" folder

Replace "mv $i old/" by any command such as rm / echo to do something different.

iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.ext
find . -type f -regex ".*/core.[0-9][0-9][0-9][0-9]$"
2014-01-17 16:44:47
User: H3liUS
Functions: find
0

Will find and list all core files from the current directory on. You can pass | xargs rm -i to be prompted for the removal if you'd like to double check before removal.

find . -maxdepth 1 -type d -exec sh -c "printf '{} ' ; find '{}' -type f -ls | wc -l" \;
2013-07-29 19:46:35
User: HerbCSO
Functions: find sh
2

For each directory from the current one, list the counts of files in each of these directories. Change the -maxdepth to drill down further through directories.

rsync -a --append source-file destination
$text = do {local(@ARGV, $/) = $file ; <>; }; [or] sub read_file { local(@ARGV, $/) = @_ ; <>; }
2013-06-12 11:41:49
User: matya
0

Found it on:

http://stackoverflow.com/questions/318789/whats-the-best-way-to-open-and-read-a-file-in-perl

The yet most simple way to read all the contents of a file to a variable. I used it in a perl script to replace $text="`cat /sys/...`", and stipping down 9 secs of runtime due less forks

fsarchvier probe simple
find . -name \*.swp -type f -delete
2013-01-19 07:38:03
User: ashwinkumark
Functions: find
Tags: file remove
0

Removes all *.swp files underneath the current directory. Replace "*.swp" with your file pattern(s).

export HISTFILESIZE=99999
2013-01-02 09:25:06
User: totti
Functions: export
1

set how many commands to keep in history

Default is 500

Saved in /home/$USER/.bash_history

Add this to /home/$USER/.bashrc

HISTFILESIZE=1000000000

HISTSIZE=1000000

sed '/foo/ s/foo/foobar/g' <filename>
2013-01-02 08:52:44
User: totti
Functions: sed
Tags: sed file optimize
-6

Use optimized sed to big file/stream to reduce execution time

Use

sed '/foo/ s/foo/foobar/g' <filename>

insted of sed

's/foo/foobar/g' <filename>
find -type f | xargs file | grep ".*: .* text" | sed "s;\(.*\): .* text.*;\1;"
ls -l /proc/*/fd/* | grep 'deleted'| grep "\/proc.*\file-name-part"
2012-09-13 09:54:16
User: totti
Functions: grep ls
0

Accidentally deleted some file while used by a program ? (Eg: a song)

Use this command to find the file handle and recover using

cp /proc/pid/fd/filehandle /new/recoverd-file.ext
testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; }
2012-02-21 16:54:53
User: AskApache
Functions: echo sed test
2

Applies each file operator using the built-in test.

testt /home/askapache/.sq

/home/askapache/.sq

-a True - file exists.

-d True - file is a directory.

-e True - file exists.

-r True - file is readable by you.

-s True - file exists and is not empty.

-w True - the file is writable by you.

-x True - the file is executable by you.

-O True - the file is effectively owned by you.

-G True - the file is effectively owned by your group.

-N True - the file has been modified since it was last read.

Full Function:

testt ()

{

local dp;

until [ -z "${1:-}" ]; do

dp="$1";

[[ ! -a "$1" ]] && dp="$PWD/$dp";

command ls -w $((${COLUMNS:-80}-20)) -lA --color=tty -d "$dp";

[[ -d "$dp" ]] && find "$dp" -mount -depth -wholename "$dp" -printf '%.5m %10M %#15s %#9u %-9g %#5U %-5G %Am/%Ad/%AY %Cm/%Cd/%CY %Tm/%Td/%TY [%Y] %p\n' -a -quit 2> /dev/null;

for f in a b c d e f g h L k p r s S t u w x O G N;

do

test -$f "$dp" && help test | sed "/-$f F/!d" | sed -e 's#^[\t ]*-\([a-zA-Z]\{1\}\) F[A-Z]*[\t ]* True if#-\1 "'$dp'" #g';

done;

shift;

done

}

c="cp -a";e="~";echo -e "\npaste\n";i=0;k="1"; while [[ "$k" != "" ]]; do read -a k;r[i]=$k;((i++));done;i=0;while :;do t=${r[i]};[ "$t" == "" ] && break; g=$(echo $c ${r[i]} $e);echo -e $g "\ny/n?";read y;[ "$y" != "n" ] && eval $g;((i++));done
2011-12-04 12:45:44
User: knoppix5
Functions: echo eval read
-1

Schematics:

command [options] [paste your variable here] parameter

command [options] [paste entire column of variables here] parameter

...

(hard-code command "c" and parameter "e" according to your wishes: in example shown command = "cp -a" and parameter = "~")

Features:

- Quick exchange only variable part of a long command line

- Make variable part to be an entire column of data (i.e. file list)

- Full control while processing every single item

Hints:

Paste column of data from anywhere. I.e. utilize the Block Select Mode to drag, select and copy columns (In KDE Konsole with Ctrl+Alt pressed, or only Ctrl pressed in GNOME Terminal respectively).

Disadvantages:

You can paste only one single variable in a row. If there are more space separated variables in a row only first one will be processed, but you can arrange your variables in a column instead. To transpose rows to columns or vice versa look at Linux manual pages for 'cut' and 'paste'.

TODO:

- add edit mode to vary command "c" and parameter "e" on the fly

- add one edit mode more to handle every list item different

- add y/n/a (=All) instead of only y(=default)/n to allowed answers

Disclaimer:

The code is not optimized, only the basic idea is presented here. It's up to you to shorten code or extend the functionality.

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.

stat -c "%s" <file>
cat <file> > /dev/null
2011-11-12 15:45:27
User: totti
Functions: cat
Tags: ram file cache
8

Best result when file size less than half of RAM size

stat --format "%s" <file>
read -d ""
2011-10-25 18:44:11
User: totti
Functions: read
1

Handle any bad named file which contains ",',\n,\b,\t,` etc

Store the file name as null character separated list

find . -print0 >name.lst

and retrieve it using

read -r -d ""

Eg:

find . -print0 >name.lst; cat name.lst| while IFS="" read -r -d "" file; do ls -l "$file"; done