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 stat from sorted by
Terminal - Commands using stat - 47 results
mv -iv $FILENAME{,.$(stat -c %Z $FILENAME)}
2014-12-02 13:47:52
User: bunam
Functions: mv stat
Tags: mv
3

FILENAME=nohup.out

mv -iv $FILENAME{,.$(stat -c %Y $FILENAME)}

does it help ?

mv -iv $FILENAME{,.$(stat -c %y $FILENAME | awk '{print $1}')}
2014-12-01 22:41:38
User: pdxdoughnut
Functions: awk mv stat
Tags: mv
3

Note that the -i will not help in a script. Proper error checking is required.

FILE=file_name; CHUNK=$((64*1024*1024)); SIZE=$(stat -c "%s" $FILE); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done
2014-10-03 13:18:19
User: flatcap
Functions: losetup stat
5

It's common to want to split up large files and the usual method is to use split(1).

If you have a 10GiB file, you'll need 10GiB of free space.

Then the OS has to read 10GiB and write 10GiB (usually on the same filesystem).

This takes AGES.

.

The command uses a set of loop block devices to create fake chunks, but without making any changes to the file.

This means the file splitting is nearly instantaneous.

The example creates a 1GiB file, then splits it into 16 x 64MiB chunks (/dev/loop0 .. loop15).

.

Note: This isn't a drop-in replacement for using split. The results are block devices.

tar and zip won't do what you expect when given block devices.

.

These commands will work:

hexdump /dev/loop4

.

gzip -9 < /dev/loop6 > part6.gz

.

cat /dev/loop10 > /media/usb/part10.bin
find . -name "*.pdf" -print0 | xargs -r0 stat -c %y\ %n | sort|awk '{print $4}'|gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.pdf\n", $0, a++ }' | bash
2014-09-23 06:40:45
Functions: awk find gawk printf stat xargs
Tags: sort awk find xargs
0

Caution: distructive overwrite of filenames

Useful for concatenating pdfs in date order using pdftk

function findOlderThan () { find . -mmin -$((($(date "+%s") - $(stat -c %Y $1))/60)) -type f ; }
2014-08-29 17:52:34
User: RobertDeRose
Functions: date find stat
Tags: find date stat
0

This function will find the modification time in unix_time of the given file, then calculate the number of minutes from now to then and then find all files modified in that range.

stat -f%Su /dev/console
stat -c '%n %U:%G-%a' *
2014-05-03 04:56:23
User: snipertyler
Functions: stat
Tags: permissions
3

#Alias

alias perm="stat -c '%n %U:%G-%a'"

#Function

perm() { for ll in $@; do stat -c "%n %U:%G-%a" "$ll"; done; }

arecord -q -f cd -d 1 recvol.wav;sox recvol.wav -n stat 2>&1|grep RMS|grep amplitude|cut -d"." -f2|cut -c 1-2>recvol;echo $((`cat recvol`+1))>recvol;rec -t wav - silence 1 0.1 `cat recvol` -1 1.0 `cat recvol`%|lame -s 44.1 -a -v - >record.mp3
2014-02-27 23:23:55
User: geaplanet
Functions: arecord cd cut echo grep stat
0

It find out the mic recording level at the moment of run the command and if a noise level is higher it starts to record an mp3 file. The resulting file will have only the sounds not the silences.

find . -name "pattern" -exec stat -c%s {} \; | awk '{total += $1} END {print total}'
2014-01-15 11:07:09
User: Koobiac
Functions: awk find stat
1

Find files and calculate size with stat of result in shell

find . -type f -print0 | xargs -0 stat -c'%Y :%y %12s %n' | sort -nr | cut -d: -f2- | head
2013-08-03 09:53:46
User: HerbCSO
Functions: cut find sort stat xargs
5

Goes through all files in the directory specified, uses `stat` to print out last modification time, then sorts numerically in reverse, then uses cut to remove the modified epoch timestamp and finally head to only output the last 10 modified files.

Note that on a Mac `stat` won't work like this, you'll need to use either:

find . -type f -print0 | xargs -0 stat -f '%m%t%Sm %12z %N' | sort -nr | cut -f2- | head

or alternatively do a `brew install coreutils` and then replace `stat` with `gstat` in the original command.

perl -e 'printf "%o\n", (stat shift)[2]&07777' file
stat -f -L -c %T YOUR_FILE_OR_DIRECTORY
echo "eval \"\$(dd if=\$0 bs=1 skip=XX 2>/dev/null|gpg -d 2>/dev/null)\"; exit" > script.secure; sed -i s:XX:$(stat -c%s script.secure): script.secure; gpg -c < script.bash >> script.secure; chmod +x script.secure
2013-03-09 11:16:48
User: rodolfoap
Functions: chmod echo gpg sed stat
6

(Please see sample output for usage)

script.bash is your script, which will be crypted to script.secure

script.bash --> script.secure

You can execute script.secure only if you know the password. If you die, your script dies with you.

If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).

Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.

dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm T)>>F
2013-01-31 01:38:30
User: rodolfoap
5

This is just a proof of concept: A FILE WHICH CAN AUTOMOUNT ITSELF through a SIMPLY ENCODED script. It takes advantage of the OFFSET option of mount, and uses it as a password (see that 9191? just change it to something similar, around 9k). It works fine, mounts, gets modified, updated, and can be moved by just copying it.

USAGE: SEE SAMPLE OUTPUT

The file is composed of three parts:

a) The legible script (about 242 bytes)

b) A random text fill to reach the OFFSET size (equals PASSWORD minus 242)

c) The actual filesystem

Logically, (a)+(b) = PASSWORD, that means OFFSET, and mount uses that option.

PLEASE NOTE: THIS IS NOT AN ENCRYPTED FILESYSTEM. To improve it, it can be mounted with a better encryption script and used with encfs or cryptfs. The idea was just to test the concept... with one line :)

It applies the original idea of http://www.commandlinefu.com/commands/view/7382/command-for-john-cons for encrypting the file.

The embedded bash script can be grown, of course, and the offset recalculation goes fine. I have my own version with bash --init-file to startup a bashrc with a well-defined environment, aliases, variables.

alias locate='if [ $((`date +%s`-`eval $(stat -s /var/db/locate.database); echo $st_mtime`)) -gt 3600 ]; then echo "locate: db is too old!">/dev/stderr; sudo /usr/libexec/locate.updatedb; fi; locate -i'
2013-01-21 17:45:50
User: jhyland87
Functions: alias echo locate stat sudo
Tags: locate osx mac
0

MAC OSX doesn't come with a locate command, This will do the same thing as the locate command on a typical Linux OS.

Simply add it to your ~/.bash_profile

stat /etc/my.cnf
find . -type f -exec stat -f '%m %N' {} \; | sort -n
svn stat | grep ^\! | awk '{print $2}' | xargs svn del
2012-04-13 12:13:37
Functions: awk grep stat xargs
0

Sometimes cache-files or garbage gets added to your SVN repository. This is the way I normally clean up those when the actual files are already gone.

svn stat | grep M | cut -d " " -f8 | xargs svn revert
perl -e 'printf "%04o\n", (stat shift)[2] & 0777;' file
2012-03-22 15:05:04
User: zlemini
Functions: perl stat
1

This prints file access rights in octal - useful when "stat" is unavailable.

find . -size +10240k -exec stat -c%s" "%n {} \; | sort -rn
pv -petrs $(stat -c %s file.iso) file.iso | dd bs=1M oflag=sync of=/dev/sdX
stat -c "%s" <file>
stat --format "%s" <file>
:|dd of=./ssss.txt seek=1 bs=$(($(stat -c%s ./ssss.txt)-$(tail -n 2 ./ssss.txt|wc -c)))
2011-10-17 07:53:00
User: ri0day
Functions: dd stat tail wc
0

new way to replace text file with dd,faster than head,sed,awk if you do this with big file