
Terminal - Commands using printf - 139 results
_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
This is sample output - yours may be different.
proc lister
usage: p
proc killer
usage: p patt [signal]
uses only ps, grep, sed, printf and kill
no need for pgrep/pkill (not part of early UNIX)
_p(){
ps ax \
|grep $1 \
|sed '
/grep.'"$1"'/d' \
|while read a;do
printf ${a%% *}' ';
printf "${a#* }" >&2;
printf '\n';
done;
}
p(){
case $# in
0)
ps ax |grep .|less -iE;
;;
1)
_p $1;
;;
[23])
_p $1 2>/dev/null \
|sed '/'"$2"'/!d;
s,.*,kill -'"${3-15}"' &,'|sh -v
;;
esac;
}
alas, can't get this under 255 chars.
flatcap?
_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
This is sample output - yours may be different.
proc lister
usage: p
proc killer
usage: p patt [signal]
uses only ps, grep, sed, printf and kill
no need for pgrep/pkill (not part of early UNIX)
_p(){
ps ax \
|grep $1 \
|sed '
/grep.'"$1"'/d' \
|while read a;do
printf ${a%% *}' ';
printf "${a#* }" >&2;
printf '\n';
done;
}
p(){
case $# in
0)
ps ax |grep .|less -iE;
;;
1)
_p $1;
;;
[23])
_p $1 2>/dev/null \
|sed '/'"$2"'/!d;
s,.*,kill -'"${3-15}"' &,'|sh -v
;;
esac;
}
alas, can't get this under 255 chars.
flatcap?
find /path/to/dir -iname "*.ext" -print0 | xargs -0 mplayer -really-quiet -cache 64 -vo dummy -ao dummy -identify 2>/dev/null | awk '/ID_LENGTH/{gsub(/ID_LENGTH=/,"")}{SUM += $1}END{ printf "%02d:%02d:%02d\n",SUM/3600,SUM%3600/60,SUM%60}'
This is sample output - yours may be different.
Improvement on Coderjoe's Solution. Gets rid of grep and cut (and implements them in awk) and specifies some different mplayer options that speed things up a bit.
du -k | sort -n | perl -ne 'if ( /^(\d+)\s+(.*$)/){$l=log($1+.1);$m=int($l/log(1024)); printf ("%6.1f\t%s\t%25s %s\n",($1/(2**(10*$m))),(("K","M","G","T","P")[$m]),"*"x (1.5*$l),$2);}' | more
This is sample output - yours may be different.
4.0 K ** ./bin
4.0 K ** ./.config/gnome-main-menu
4.0 K ** ./.config/.mono/certs/Trust
4.0 K ** ./Desktop
4.0 K ** ./.fvwm
4.0 K ** ./.gconfd
4.0 K ** ./.gnome2/accels
4.0 K ** ./.gnome2/keyrings
4.0 K ** ./.gnome2/nautilus-scripts
4.0 K ** ./.gnome2_private
4.0 K ** ./.gnupg/private-keys-v1.d
4.0 K ** ./.kde/share/config
4.0 K ** ./.mozilla/firefox/9ikhdhm7.default/extensions
4.0 K ** ./netv/netvault/clientpackages
4.0 K ** ./netv/netvault/extrabkl
4.0 K ** ./netv/netvault/extrapackages
4.0 K ** ./.qt
4.0 K ** ./.skel
4.0 K ** ./.wapi
4.0 K ** ./.zenuser.gconf.defaults
4.0 K ** ./.zenuser.gconf.mandatory
8.0 K *** ./.config/.mono/certs
8.0 K *** ./Documents
8.0 K *** ./firefox/firefox/defaults/pref
8.0 K *** ./.gconf/apps/gnome-settings/gnome-panel
8.0 K *** ./.gconf/apps/nautilus/preferences
8.0 K *** ./.gconf/apps/panel/applets/clock_screen0/prefs
8.0 K *** ./.gconf/apps/panel/applets/main_menu_screen0
8.0 K *** ./.gconf/apps/panel/applets/mixer_screen0
8.0 K *** ./.gconf/apps/panel/applets/notification_area_screen0
8.0 K *** ./.gconf/apps/panel/applets/show_desktop_button_screen0
8.0 K *** ./.gconf/apps/panel/applets/tomboy_screen0
8.0 K *** ./.gconf/apps/panel/applets/window_list_screen0/prefs
8.0 K *** ./.gconf/apps/panel/general
8.0 K *** ./.gconf/apps/panel/global
8.0 K *** ./.gconf/apps/panel/toplevels/bottom_panel_screen0/background
8.0 K *** ./.gconf/desktop/gnome/accessibility/keyboard
8.0 K *** ./.gconf/desktop/gnome/background
8.0 K *** ./.gconf/desktop/gnome/peripherals/keyboard/host-posadm1/0
8.0 K *** ./.gconf/system/http_proxy
8.0 K *** ./.gconf/system/proxy
8.0 K *** ./.gnome2/share/cursor-fonts
8.0 K *** ./.gnome2/share/fonts
8.0 K *** ./.gnome/gnome-vfs
8.0 K *** ./.kde/share
8.0 K *** ./.nautilus/metafiles
8.0 K *** ./src/.tmp_versions
12.0 K *** ./.config/.mono
12.0 K *** ./firefox/firefox/defaults
12.0 K *** ./.gconf/apps/gnome-settings
12.0 K *** ./.gconf/apps/nautilus
12.0 K *** ./.gconf/desktop/gnome/accessibility
12.0 K *** ./.gconf/desktop/gnome/peripherals/keyboard/host-posadm1
12.0 K *** ./.kde
12.0 K *** ./.mozilla/firefox/9ikhdhm7.default/chrome
12.0 K *** ./.nautilus
12.0 K *** ./.thumbnails/fail/gnome-thumbnail-factory
16.0 K **** ./.gconf/apps/panel/applets/clock_screen0
16.0 K **** ./.gconf/apps/panel/applets/window_list_screen0
16.0 K **** ./.gconf/apps/panel/toplevels/bottom_panel_screen0
16.0 K **** ./.gconf/desktop/gnome/peripherals/keyboard
16.0 K **** ./netv/netvault/eula/Chinese/BIG5
16.0 K **** ./netv/netvault/eula/ChineseSimp/GB2312
16.0 K **** ./netv/netvault/eula/Korean/EUC-KR
16.0 K **** ./.thumbnails/fail
20.0 K **** ./firefox/firefox/chrome/icons/default
20.0 K **** ./.gconf/apps/panel/toplevels
20.0 K **** ./.gconf/desktop/gnome/peripherals
20.0 K **** ./.gconf/system
20.0 K **** ./.gnome2/share
20.0 K **** ./.kbd
20.0 K **** ./netv/netvault/eula/French/ISO-8859-1
20.0 K **** ./netv/netvault/eula/JapaneseEUC/EUC-JP
20.0 K **** ./.thumbnails
24.0 K **** ./firefox/firefox/chrome/icons
24.0 K **** ./firefox/firefox/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}
24.0 K **** ./netv/netvault/eula/English
24.0 K **** ./netv/netvault/eula/German/ISO-8859-1
28.0 K ***** ./.config
28.0 K ***** ./firefox/firefox/chrome
28.0 K ***** ./firefox/firefox/extensions
28.0 K ***** ./firefox/firefox/searchplugins
28.0 K ***** ./.gnome
28.0 K ***** ./.gnupg
32.0 K ***** ./netv/netvault/eula/Chinese
32.0 K ***** ./netv/netvault/eula/ChineseSimp
36.0 K ***** ./.gnome2
40.0 K ***** ./firefox/firefox/icons
40.0 K ***** ./netv/netvault/eula/Korean
44.0 K ***** ./.gconf/desktop/gnome
44.0 K ***** ./netv/netvault/eula/French
44.0 K ***** ./zlmagent/data/locale/en_US/LC_MESSAGES
48.0 K ***** ./.gconf/desktop
48.0 K ***** ./.metacity/sessions
48.0 K ***** ./netv/netvault/eula/German
48.0 K ***** ./netv/netvault/eula/JapaneseEUC
48.0 K ***** ./.ssh
48.0 K ***** ./zlmagent/data/locale/en_US
52.0 K ***** ./.metacity
52.0 K ***** ./zlmagent/data/locale/pt_BR/LC_MESSAGES
56.0 K ****** ./zlmagent/data/locale/pt_BR
60.0 K ****** ./zlmagent/data/locale/de_DE/LC_MESSAGES
60.0 K ****** ./zlmagent/data/locale/es_ES/LC_MESSAGES
60.0 K ****** ./zlmagent/data/locale/fr_FR/LC_MESSAGES
64.0 K ****** ./.mozilla/firefox/9ikhdhm7.default/bookmarkbackups
64.0 K ****** ./zlmagent/data/locale/de_DE
64.0 K ****** ./zlmagent/data/locale/es_ES
64.0 K ****** ./zlmagent/data/locale/fr_FR
76.0 K ****** ./.gconf/apps/panel/applets
116.0 K ******* ./.gconf/apps/panel
124.0 K ******* ./zlmagent/readmes
144.0 K ******* ./.gconf/apps
148.0 K ******* ./squid-old
168.0 K ******* ./netv/netvault/serverpackages
168.0 K ******* ./zlmagent/data/configureScripts
184.0 K ******* ./.gstreamer-0.10
216.0 K ******** ./.gconf
232.0 K ******** ./firefox/firefox/components
260.0 K ******** ./zlmagent/data/eulas
272.0 K ******** ./netv/netvault/eula
300.0 K ******** ./zlmagent/data/locale
340.0 K ******** ./.vmware/VMware vCenter Converter Standalone/Logs
348.0 K ******** ./.vmware/VMware vCenter Converter Standalone
352.0 K ******** ./.vmware
624.0 K ********* ./firefox/firefox/dictionaries
1.4 M ********** ./zlmagent/data/packages/runtime-deps/sles-10-i586
1.4 M ********** ./zlmagent/data/packages/runtime-deps
1.6 M *********** ./zlmagent/data/packages/imaging/sles-10-i586
1.6 M *********** ./zlmagent/data/packages/imaging
1.9 M *********** ./src
1.9 M *********** ./.mozilla/firefox/9ikhdhm7.default/Cache
3.4 M ************ ./netv/netvault/packages
3.6 M ************ ./zlmagent/data/packages/sles-10-i586/sles-10-i586
3.6 M ************ ./zlmagent/data/packages/sles-10-i586
5.0 M ************ ./.mozilla/firefox/9ikhdhm7.default
5.0 M ************ ./.mozilla/firefox
5.0 M ************ ./.mozilla
5.9 M ************* ./scripts
14.0 M ************** ./zlmagent/data/packages/mono/sles-10-i586
14.0 M ************** ./zlmagent/data/packages/mono
24.9 M *************** ./netv/netvault
24.9 M *************** ./netv
25.9 M *************** ./zlmagent/data/packages/client/sles-10-i586
25.9 M *************** ./zlmagent/data/packages/client
28.4 M *************** ./firefox/firefox
46.5 M **************** ./zlmagent/data/packages
47.7 M **************** ./zlmagent/data
47.8 M **************** ./zlmagent
57.9 M **************** ./firefox
92.8 M ***************** ./acro
184.5 M ****************** ./servername-backup
437.2 M ******************* .
This one line Perl script will display the smallest to the largest files sizes in all directories on a server.
genRandomText() { x=({a..z}); for(( i=0; i<$1; i++ )); do printf ${x[$((RANDOM%26))]}; done; printf "\n"; }
This is sample output - yours may be different.
Here's my version. It's a bit lengthy but I prefer it since it's all Bash.
VAR="%23%21%2fbin%2fbash" ; printf -v VAR "%b" "${VAR//\%/\x}" ; echo $VAR
This is sample output - yours may be different.
You can use ordinary printf to convert "%23%21%2fbin%2fbash" into "#!/bin/bash" with no external utilities, by using a little known printf feature -- the "%b" specifier converts shell escapes. Replace % with \x and printf will understand the urlencoded string.
BASH's printf has an extension to set a variable directly, too. So you get to convert urlencoded strings from garble to plaintext in one step with no externals and no backticks.
This is sample output - yours may be different.
$ touch apple banana cherry
$ printf "$PWD/%s\n" *
/home/flatcap/apple
/home/flatcap/banana
/home/flatcap/cherry
List the full path of some files.
You can add ".*" on the end if you want to display hidden files.
hex() { printf "%X\n" $1; }
This is sample output - yours may be different.
mplayer -endpos 0.1 -vo null -ao null -identify *.avi 2>&1 |grep ID_LENGTH |cut -d = -f 2|awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
This is sample output - yours may be different.
Better awk example, using only mplayer, grep, cut, and awk.
awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display
This is sample output - yours may be different.
Parses /etc/group to "dot" format and pases it to "display" (imagemagick) to show a usefull diagram of users and groups (don't show empty groups).
human_filesize() { awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } '}
This is sample output - yours may be different.
$ human_filesize 1234567890
1.15 Gb
Converts a number of bytes provided as input, to a human readable number.
myhex=$(printf '%02X' ${myip//./ };)
This is sample output - yours may be different.
where myip=10.10.10.5, $myhex will equal "0A0A0A05"
Converts IP octets to hex using printf command. Useful for generating pxeboot aliases in the pxelinux.cfg folder.
find . -type f -exec awk '/linux/ { printf "%s %s: %s\n",FILENAME,NR,$0; }' {} \;
This is sample output - yours may be different.
while true; do cat /proc/net/dev; sleep 1; done | awk -v dc="date \"+%T\"" '/eth0/{i = $2 - oi; o = $10 - oo; oi = $2; oo = $10; dc|getline d; close(dc); if (a++) printf "%s %8.2f KiB/s in %8.2f KiB/s out\n", d, i/1024, o/1024}'
This is sample output - yours may be different.
02:18:43 1.71 KiB/s in 0.11 KiB/s out
02:18:44 0.75 KiB/s in 0.25 KiB/s out
02:18:45 0.47 KiB/s in 0.20 KiB/s out
02:18:46 0.59 KiB/s in 0.15 KiB/s out
02:18:47 1.12 KiB/s in 1.37 KiB/s out
02:18:48 0.65 KiB/s in 0.21 KiB/s out
02:18:49 0.50 KiB/s in 0.31 KiB/s out
02:18:50 1.28 KiB/s in 0.37 KiB/s out
02:18:51 0.50 KiB/s in 0.05 KiB/s out
02:18:52 2.26 KiB/s in 0.10 KiB/s out
poorman's ifstat using just sh and awk. You must change "eth0" with your interface's name.
awk '{ printf "%.2f", $2/1024/1024 ; exit}' /proc/meminfo
This is sample output - yours may be different.
when you can do it , avoid pipe
whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; }
This is sample output - yours may be different.
rany:~$ whichpath md5sum cat ls nc sat
/bin:/home/rany/bin:/usr/bin
rany:~$
I find it useful when I want to add another crontab entry and I need to specify the appropriate PATH.
I give ''whichpath'' a list of programs that I use inside my script and it gives me the PATH I need to use for this script.
''whichpath'' uses associative array, therefore you should have Bash v4 in order to run it.
See sample output.
i=60;while [ $i -gt 0 ];do if [ $i -gt 9 ];then printf "\b\b$i";else printf "\b\b $i";fi;sleep 1;i=`expr $i - 1`;done
This is sample output - yours may be different.
SCALE=3; WIDTHL=10; WIDTHR=60; BAR="12345678"; BAR="${BAR//?/==========}"; while read LEFT RIGHT rest ; do RIGHT=$((RIGHT/SCALE)); printf "%${WIDTHL}s: %-${WIDTHR}s\n" "${LEFT:0:$WIDTHL}" "|${BAR:0:$RIGHT}*"; done < dataset.dat
This is sample output - yours may be different.
$ cat dataset.dat
Irene 10
Karen 37
Andreas 41
Beatrice 23
$ SCALE=1; WIDTHL=10; WIDTHR=60; BAR="12345678"; BAR="${BAR//?/==========}"; while read LEFT RIGHT rest ; do RIGHT=$((RIGHT/SCALE)); printf "%${WIDTHL}s: %-${WIDTHR}s\n" "${LEFT:0:$WIDTHL}" "|${BAR:0:$RIGHT}*"; done < dataset.dat
Irene: |==========*
Karen: |=====================================*
Andreas: |=========================================*
Beatrice: |=======================*
WIDTHL=10 and WIDTHR=60 are setting the widths of the left and the right column/bar. BAR="12345678" etc. is used to create a 80 char long string of "="s. I didn't know any shorter way.
If you want to pipe results into it, wrap the whole thing in ( ... )
I know that printing bar graphs can be done rather easily by other means. Here, I was looking for a Bash only variant.
isgd () { curl 'http://is.gd/create.php?format=simple&url='"$1" ; printf "\n" }
This is sample output - yours may be different.
% isgd 'commandlinefu.com'
http://is.gd/IjoOpX
Check the API. You shouldn't need sed. The print-newline at the end is to prevent zsh from inserting a % after the end-of-output.
Also works with http://v.gd
arp-scan -I eth0 -l | perl -ne '/((\d{1,3}\.){3}\d{1,3})/ and $ip=$1 and $_=`nmblookup -A $ip` and /([[:alnum:]-]+)\s+<00>[^<]+<ACTIVE>/m and printf "%15s %s\n",$ip,$1'
This is sample output - yours may be different.
printf "%02d:%02d\n" $(curl -s "http://search.twitter.com/search?from=zurvollenstunde&rpp=1" | grep -E '(Es ist jetzt|ago)' | sed 's/<[^>]*>//g;s/[^[:digit:]]//g' | xargs )
This is sample output - yours may be different.
Calc the rough time from Twitter. Now with leading Zeroes.
mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
This is sample output - yours may be different.
usage: mem memcache-command [arguments]
where memcache-command might be:
set
add
get[s]
append
prepend
replace
delete
incr
decr
cas
stats
verbosity
version
notes:
exptime argument is set to 0 (no expire)
flags argument is set to 1 (arbitrary)
printf "g/^/m0\nw\nq"|ed $FILE
This is sample output - yours may be different.
gawk 'BEGIN {RS="\n\n"; if (ARGV[1]=="-i"){IGNORECASE=1; ARGC=1}};{Text[NR]=$0};END {asort(Text);for (i=1;i<=NR;i++) printf "%s\n\n",Text[i] }' -i<Zip.txt
This is sample output - yours may be different.
# back up your commandlinefu contributed commands
curl http://www.commandlinefu.com/commands/by/<your username>/rss|gzip ->commandlinefu-contribs-backup-$(date +%Y-%m-%d-%H.%M.%S).rss.gz
# Backup all MySQL Databases to individual files
for db in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $db | gzip > "/backups/mysqldump-$(hostname)-$db-$(date +%Y-%m-%d-%H.%M.%S).gz"; done
# Backup all MySQL Databases to individual files
for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
# Backup files older than 1 day on /home/dir, gzip them, moving old file to a dated file.
find /home/dir -mtime +1 -print -exec gzip -9 {} \; -exec mv {}.gz {}_`date +%F`.gz \;
Among other things, this allows the sorting of comment descriptions and command lines retrieved as text from CommandLineFu.com.
ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
This is sample output - yours may be different.
mv 10.jpg 0010.jpg
mv 11.jpg 0011.jpg
mv 12.jpg 0012.jpg
mv 13.jpg 0013.jpg
mv 14.jpg 0014.jpg
mv 15.jpg 0015.jpg
mv 16.jpg 0016.jpg
mv 17.jpg 0017.jpg
mv 18.jpg 0018.jpg
mv 19.jpg 0019.jpg
mv 1.jpg 0001.jpg
mv 20.jpg 0020.jpg
mv 21.jpg 0021.jpg
mv 22.jpg 0022.jpg
mv 23.jpg 0023.jpg
mv 24.jpg 0024.jpg
mv 25.jpg 0025.jpg
mv 2.jpg 0002.jpg
mv 3.jpg 0003.jpg
mv 4.jpg 0004.jpg
mv 5.jpg 0005.jpg
mv 6.jpg 0006.jpg
mv 7.jpg 0007.jpg
mv 8.jpg 0008.jpg
mv 9.jpg 0009.jpg
rename file name with fixed length nomeric format pattern