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 file from sorted by
Terminal - Commands using file - 141 results
for file in $(seq -f '%03.f' 1 $TOTAL ); do echo "($file/$TOTAL)"; curl -f -O http://domain.com/Name_$file.ext; done
2010-01-12 15:23:44
User: nordri
Functions: echo file seq
-4

With counter format [001, 002, ..., 999] , nice with pictures or wallpapers collections.

for item in *;do echo -n "$item - ";find "$item" -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F= '{sum+=$2} END {print(sum/60)}'; done | grep -v ' - 0$'
2009-11-19 06:28:15
User: jnash
Functions: awk cut echo file grep xargs
0

I know this has been beaten to death but finding video files using mime types and printing the "hours of video" for each directory is (IMHO) easier to parse than just a single total. Output is in minutes.

Among the other niceties is that it omits printing of non-video files/folders

PS: Barely managed to fit it within the 255 character limit :D

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1
2009-11-19 06:05:36
User: jnash
Functions: cut file find grep xargs
0

Uses mime-type of files rather than relying on file extensions to find files of a certain type.

This can obviously be extended to finding files of any other type as well.. like plain text files, audio, etc..

In reference to displaying the total hours of video (which was earlier posted in command line fu, but relied on the user having to supply all possible video file formats) we can now do better:

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F "=" '{sum += $2} END {print sum/60/60; print "hours"}'
for file in *.iso; do mkdir `basename $file | awk -F. '{print $1}'`; sudo mount -t iso9660 -o loop $file `basename $file | awk -F. '{print $1}'`; done
tail -F file | egrep --color 'pattern|$'
tail -f file | egrep --color=always $\|PATTERN
2009-10-15 13:08:30
User: sitaram
Functions: egrep file tail
Tags: color
-2

but you can't see the colors in that sample output :(

for file in *.foo; do gzip "$file"; done
2009-10-15 01:27:05
User: dfrios
Functions: file gzip
1

It gzip each file in a directory separately

sed '/^$/d' file >newfile
cat -n file or cat -b file
2009-09-24 12:18:40
User: eastwind
Functions: cat file
0

cat -n file : number all line

cat -b file : number only non empty line

see man cat

find . | xargs file | grep ".*: .* text" | sed "s;\(.*\): .* text.*;\1;"
file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i x86_64 |nl | tail -1 | cut -f1 -f3; file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i "mach-o object i386" |nl | tail -1 | cut -f1 -f3
2009-09-11 16:43:27
Functions: cut file grep nl tail
-2

Not as far off as you thought, now is it?

-mac fanboy

file -i <textfile>
2009-09-08 01:33:19
User: juvenn
Functions: file
2

This command gives you the charset of a text file, which would be handy if you have no idea of the encoding.

file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i x86_64 |nl |tail -1 |cut -f1 -f3 && file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -v x86_64 |nl |tail -1 |cut -f1 -f3
2009-09-03 14:28:02
Functions: cut file grep nl tail
-1

Next time you see a mac fanboy bragging about 64-bitness of 10.6 give him this so he might sh?

for file in <filename>; do cp $file{,.bak} && sed 's/old/new/g' $file.bak > $file; done
2009-08-25 16:19:45
User: Cenobite
Functions: cp file sed
Tags: bash sed
0

"&&" runs sed if and only if the backup completed and /bin/cp exited cleanly. Works for multiple files; just specify multiple filenames (or glob). Use -v switch for cp to play it safe.

file -i * | grep -c 'text/plain'
file -i * | grep 'text/plain' | wc -l
2009-08-16 21:22:46
User: voyeg3r
Functions: file grep wc
0

get files without extensions, get ASCII and utf-8 as "text/plain"

for file in *.mp3;do mkdir -p "$(mp3info -p "%a/%l" "$file")" && ln -s "$file" "$(mp3info -p "%a/%l/%t.mp3" "$file")";done
2009-08-05 17:04:34
User: matthewbauer
Functions: file ln mkdir
5

This will mv all your mp3 files in the current directory to $ARTIST/$ALBUM/$NAME.mp3

Make sure not to use sudo - as some weird things can happen if the mp3 file doesn't have id3 tags.

no_of_files=10; counter=1; while [[ $counter -le $no_of_files ]]; do echo Creating file no $counter; dd bs=1024 count=$RANDOM skip=$RANDOM if=/dev/sda of=random-file.$counter; let "counter += 1"; done
2009-07-31 16:34:47
User: rajaseelan
Functions: dd echo file
Tags: bash dd
0

Create a bunch of random files with random binary content. Basically dd dumps randomly from your hard disk to files random-file*.

cat file | tee >> file
2009-07-30 07:34:03
User: GeckoDH
Functions: cat file tee
0

The command `cat file >> file` failes with the following error message:

cat: file: input file is output file

`tee` is a nice workaround without using any temporary files.

for file in *.001; do NAME=`echo $file | cut -d. -f1,2`; cat "$NAME."[0-9][0-9][0-9] > "$NAME"; done
2009-07-29 10:04:26
User: jaymzcd
Functions: cat cut file
2

If you use newsgroups then you'll have come across split files before. Joining together a whole batch of them can be a pain so this will do the whole folder in one.

file =top
2009-07-15 21:29:23
User: libdave
Functions: file
4

Say you want to execute 'file' on the command 'top' (to determine what type of file it is); but you don't know where 'top' resides: preface the argument with = and zsh will implicitly prepend the path.

for file in `find /var/log/ -type f -size +5000k`; do > $file; done
2009-07-03 17:38:21
User: svg
Functions: file
0

you don't need to echo, just a plain redirect is enough to empty the file

for file in `find /var/log/ -type f -size +5000k`; do echo " " > $file; done
2009-07-03 16:26:36
User: jemmille
Functions: echo file
0

Empties all files in /var/log over 5000k. Useful if /var goes crazy or if you just haven't cleaned up in a while.

for file in *.pem; do ln -s $file `openssl x509 -hash -noout -in $file`.0; done
2009-06-30 17:42:07
User: darkpand
Functions: file ln
3

When you don't have c_rehash handy. Really simple - if you have a .pem file that doesn't really contain a x509 cert (let's say, newreq.pem), it will create a link, simply called '.0', pointing to that file.

find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f
2009-06-03 09:47:08
User: mohan43u
Functions: cut file find grep sed tail xargs
Tags: tail
5

Works in Ubuntu, I hope it will work on all Linux machines. For Unixes, tail should be capable of handling more than one file with '-f' option.

This command line simply take log files which are text files, and not ending with a number, and it will continuously monitor those files.

Putting one alias in .profile will be more useful.