
Terminal - Commands using printf - 139 results
alias clearscrollback='clear;printf %b "\033[3J"'
This is sample output - yours may be different.
count='1'; for i in *.jpg; do mv $i $(printf '%01d'.jpg $count); (( count++ )); done
This is sample output - yours may be different.
The '1' in '%01d' changes the amounts of digits in the integer, eg. 1 vs 0001.
awk '{for (i=9;i<=NF;i++) {printf "%s",$i; printf "%s", " ";}; printf "\n"}'
This is sample output - yours may be different.
$ ls -l
total 476
-rw-rw-r-- 1 aditya aditya 38543 Jan 7 17:06 a.pdf
-rw-rw-r-- 1 aditya aditya 50665 Dec 9 11:42 H.pdf
-rw-rw-r-- 1 aditya aditya 5769 Dec 9 11:18 h b.pdf
-rw-rw-r-- 1 aditya aditya 98084 Dec 9 11:28 M.pdf
-rw-rw-r-- 1 aditya aditya 53475 Jan 6 15:45 R E.pdf
-rw-rw-r-- 1 aditya aditya 39996 Dec 9 11:23 R E1.pdf
-rw-rw-r-- 1 aditya aditya 124566 Nov 20 08:58 R I.pdf
-rw-rw-r-- 1 aditya aditya 59557 Dec 7 23:29 Welcome.pdf
$ ls -l | awk '{for (i=9;i<=NF;i++) {printf "%s",$i; printf "%s", " ";}; printf "\n"}'
a.pdf
H.pdf
h b.pdf
M.pdf
R E.pdf
R E1.pdf
R I.pdf
Welcome.pdf
It'll print the file names preserving the spaces in their names and adding new line after every new filename.
I wrote this to quickly find out how many files in any directory is owned by a particular user. This can be extended using pipe and grep to do much more.
find . -name '*.jpg' | awk 'BEGIN{ a=0 }{ printf "mv %s name%01d.jpg\n", $0, a++ }' | bash
This is sample output - yours may be different.
This command will take the files in a directory, rename them, and then number them from 1...N.
Black belt stuff.
Hell of a time saver.
today() { printf '%(%Y-%m-%d)T\n' -1; } ## bash-4
This is sample output - yours may be different.
function up { cd $(eval printf '../'%.0s {1..$1}) && pwd; }
This is sample output - yours may be different.
Usage:
up N
I did not like two things in the submitted commands and fixed it here:
1) If I do cd - afterwards, I want to go back to the directory I've been before
2) If I call up without argument, I expect to go up one level
It is sad, that I need eval (at least in bash), but I think it's safe here.
eval is required, because in bash brace expansion happens before variable substitution, see http://rosettacode.org/wiki/Repeat_a_string#Using_printf
while(true); do printf "%s\f" $(date +%T); sleep 1; done | sm -
This is sample output - yours may be different.
for code in $(seq -w 0 255); do for attr in 0 1; do printf "%s-%03s %bTest%b\n" "${attr}" "${code}" "\e[${attr};38;05;${code}m" "\e[m"; done; done | column -c $((COLUMNS*2))
This is sample output - yours may be different.
Shows the ?rendering? for each of the 256 colours in both the bold and normal variant. Using seq is helpful to get even lines, passing $((COLUMNS*2)) to column sort-of-handles the nonprintable characters.
tshark -r *.eth -S -R "ajp13" -d tcp.port==9009,ajp13 -s 0 -l -V | awk '/Apache JServ/ {p=1} /^ *$/ {p=0;printf "\n"} (p){printf "%s\n", $0} /^(Frame|Internet Pro|Transmission Control)/ {print $0}'
This is sample output - yours may be different.
Frame 9: 928 bytes on wire (7424 bits), 928 bytes captured (7424 bits)
Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0.1)
Transmission Control Protocol, Src Port: 39823 (39823), Dst Port: pichat (9009), Seq: 1, Ack: 1, Len: 860
Apache JServ Protocol v1.3
Magic: 1234
Length: 856
Code: (2) FORWARD REQUEST
Method: (2) GET
Version: HTTP/1.1
URI: /abcd
RADDR: 10.50.12.3
RHOST:
SRV: s.com
PORT: 443
SSLP: 1
NHDR: 10
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CL
R 3.5.21022)
accept-encoding: gzip, deflate
accept-language: en-us
accept: */*
connection: Keep-Alive
host: s.com:8235
cookie: JSESSIONID=D7FD2C7E64995549ABE05776BE01E2EF
if you have a capture file *.eth, and ajp protocol is in use on port 9009, you can paste the above command. You can change the fiile and port name
du . | sort -nr | awk '{split("KB MB GB TB", arr); idx=1; while ( $1 > 1024 ) { $1/=1024; idx++} printf "%10.2f",$1; print " " arr[idx] "\t" $2}' | head -25
This is sample output - yours may be different.
Lists the size in human readable form and lists the top 25 biggest directories/files
while true; do printf "\e[32m%X\e[0m" $((RANDOM%2)); for ((i=0; i<$((RANDOM%128)); i++)) do printf " "; done; done
This is sample output - yours may be different.
0 0 0 0 0
01 0 1 1 00 0 1
1 0 0 0 0 1
0 0 0 0 0 0
1 1 0 1 1 1 0
1 0 1 0 0 0 1
1 0 1
Unlike other alternatives, this command only relies on bash builtins and should also work on windows platforms with the bash executable.
Sparseness corresponds to the number 128 and can be adjusted. To print all possible digits instead of only 0 and 1 replace RANDOM%2 by RANDOM%10 or RANDOM%16 to add letters [A-F].
hl-nonprinting () { local C=$(printf '\033[0;36m') B=$(printf '\033[0;46m') R=$(printf '\033[0m') np=$(env printf "\u00A0\uFEFF"); sed -e "s/\t/${C}▹&$R/g" -e "s/$/${C}⁋$R/" -e "s/[$np]/${B}& $R/g";}
This is sample output - yours may be different.
$ env printf '\uFEFFfoo\u00A0bar\nfie\tfum'|hl-nonprinting
foo? bar⁋
fie▹ fum⁋
Can't see it here, but the non-breaking space is highlighted :)
Of course,
cat -t -e
achieves something similar, but less colourful.
Could add more code points from https://en.wikipedia.org/wiki/Space_%28punctuation%29#Spaces_in_Unicode
hl-nonprinting () { local C=$(printf '\033[0;36m') R=$(printf '\033[0m'); sed -e "s/\t/${C}▹&$R/g" -e "s/$/${C}⁋$R/";}
This is sample output - yours may be different.
$ echo -e 'foo\tbar\nfie\tfum'|hl-nonprinting
foo▹ bar⁋
fie▹ fum⁋
I don't think it's possible to give a (background) colour to the tab itself, since a tab is, IIUC, simply a command to the terminal to move to the right. Nevertheless, this "highlighting" can be helpful when working with tab-separated files.
for i in `ls /var/log/sa/|grep -E "sa[0-9][0-9]"`;do echo -ne "$i -- ";sar -r -f /var/log/sa/$i|awk '{ printf "%3.2f\n",($4-$6-$7)*100/(3+$4)}'|grep -Eiv "average|linux|^ --|0.00|^-" |awk '{sum+=$1 }END{printf "Average = %3.2f%%\n",sum/NR}';done
This is sample output - yours may be different.
# for i in `ls /var/log/sa/|grep -E "sa[0-9][0-9]"`;do echo -ne "$i -- ";sar -r -f /var/log/sa/$i|awk '{ printf "%3.2f\n",($4-$6-$7)*100/(3+$4)}'|grep -Eiv "average|linux|^ --|0.00|^-" |awk '{sum+=$1 }END{printf "Average = %3.2f%%\n",sum/NR}';done
sa01 -- Average = 91.10%
sa02 -- Average = 95.02%
sa03 -- Average = 83.10%
sa04 -- Average = 78.05%
sa05 -- Average = 77.79%
sa06 -- Average = 77.70%
sa07 -- Average = 77.58%
sa08 -- Average = 77.60%
sa09 -- Average = 77.64%
sa10 -- Average = 80.94%
sa11 -- Average = 84.72%
/sbin/ifconfig |awk '/bond|eth/{getline i;printf $0" ";printf "%s\n", i" "}'|awk '{print $1,substr($7,6)}'
This is sample output - yours may be different.
find -maxdepth 3 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done
This is sample output - yours may be different.
./Data: 5011
./Data/Intensities: 3
./Data/Intensities/BaseCalls: 2
./Data/Status_Files: 28
./Data/Status_Files/RefreshBtn: 4
./Data/reports: 4978
echo -e "\e[32m"; while :; do printf '%*c' $(($RANDOM % 30)) $(($RANDOM % 2)); done
This is sample output - yours may be different.
Prints 0's and 1's in The Matrix style. You can easily modify to print 0-9 digits using $RANDOM %10 insted of %2.
grep -H voluntary_ctxt /proc/*/status |gawk '{ split($1,proc,"/"); if ( $2 > 10000000 ) { printf $2 " - Process : "; system("ps h -o cmd -p "proc[3]) } }' | sort -nk1,1 | sed 's/^/Context Switches: /g'
This is sample output - yours may be different.
Context Switches: 1097211 - Process : [migration/12]
Context Switches: 1500058 - Process : [migration/10]
Context Switches: 1558813 - Process : [migration/13]
Context Switches: 2207748 - Process : [migration/11]
Context Switches: 2516720 - Process : /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/server.err --open-files-limit=12288 --pid-file=/var/lib/mysql/server.pid
This command will find the highest context switches on a server and give you the process listing.
for ((i=65;i<91;i++)); do printf "\\$(printf '%03o' $i) "; done
This is sample output - yours may be different.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Could easily be used for lowercase --> ((i=97;i
alarmclock() { [ $1 ] || echo Parameter TIME is missing. 1>&2 && return 1 ; ( sleep $1 ; for x in 9 8 7 6 5 4 3 2 1 ; do for y in `seq 0 $[ 10 - $x ] ` ; do printf "\a"; sleep 0.$x ; done ; done ) & }
This is sample output - yours may be different.
usage: alarmclock TIME
TIME is a sleep(1) parameter which tells function how long to wait until raise the alarm.
printf "%s," "${LIST[@]}" | cut -d "," -f 1-${#LIST[@]}
This is sample output - yours may be different.
printf reapeats the format as longer as it has arguments. Then the idea is to make cut retain as much fields as we have elements in the array.
As usual with such join/split string manipulation, you have to make sure you don't have conflicts between your separator and your array content.
find /proc/sys/vm -maxdepth 1 -type f | while read i ; do printf "%-35s\t%s\n" "$i" "$(<$i)" ; done | sort -t/ -k4
This is sample output - yours may be different.
/proc/sys/vm/block_dump 0
/proc/sys/vm/dirty_background_bytes 0
/proc/sys/vm/dirty_background_ratio 5
/proc/sys/vm/dirty_bytes 0
/proc/sys/vm/dirty_expire_centisecs 3000
/proc/sys/vm/dirty_ratio 10
/proc/sys/vm/dirty_writeback_centisecs 500
/proc/sys/vm/drop_caches 0
/proc/sys/vm/hugepages_treat_as_movable 0
/proc/sys/vm/hugetlb_shm_group 0
/proc/sys/vm/laptop_mode 0
/proc/sys/vm/legacy_va_layout 0
/proc/sys/vm/lowmem_reserve_ratio 256 256 32
/proc/sys/vm/max_map_count 65530
... snip ...
Sometimes you want to see all of the systcls for a given $thing. I happened to need to easily look at all of the vm sysctls between two boxes and compare them. This is what I came up with.
members () { dscl . -list /Users | while read user; do printf "$user "; dsmemberutil checkmembership -U "$user" -G "$*"; done | grep "is a member" | cut -d " " -f 1; };
This is sample output - yours may be different.
members groupname
user1
user2
user5
Group membership in OS X is a mish-mash of standards that end up meaning there's almost a half-dozen of ways to belong to a group, what with group inheritance and automatic assignment. This means there's no easy command to find out all groups a user belongs to. The only sensible way then is to list all users and then query each user for membership.
NOTE: This is a function. Once input you can execute it by calling with a groupname.
printf "%$(tput cols)s\n"|tr ' ' '='
This is sample output - yours may be different.
================================
Use tput cols to find the width of the terminal and set it as the minimum field width.
yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
This is sample output - yours may be different.