functions 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #!/bin/bash
  2. function mkd {
  3. mkdir -p "$@" && cd "$_";
  4. }
  5. # for webm videos to mp4
  6. function webm2mp4 {
  7. for file in *.webm
  8. do
  9. ffmpeg -i "$file" "`basename "$file" .webm` .mp4"
  10. done
  11. }
  12. # mp4 to mp3
  13. function mp42mp3 {
  14. CURRENTMP4HERE=$(ls *.mp4 | wc -l)
  15. notify-send "Found $CURRENTMP4HERE mp4s, gonna convert them now, be patient"
  16. for file in *.mp4
  17. do
  18. ffmpeg -y -i "$file" "`basename "$file" .mp4`.mp3"
  19. done
  20. notify-send "All $CURRENTMP4HERE mp4s have been converted to mp3s and now we are moving them to your music dir"
  21. CURRENTMP3INMPD=$(ls $HOME/mus/*.mp3 | wc -l)
  22. notify-send "You have $CURRENTMP3INMPD mp3s in your database"
  23. mv *.mp3 ~/mus/ -v
  24. notify-send "All mp3s have been moved to music dir."
  25. TOTAL=$(($CURRENTMP3INMPD + $CURRENTMP4HERE))
  26. notify-send "You have got $CURRENTMP4HERE new mp3s. Now you have $TOTAL songs in database. Enjoy your music"
  27. }
  28. # mp3 to aac
  29. mp3toaac () {
  30. for file in *.mp3
  31. do
  32. ffmpeg -y -i "$file" "`basename "$file" .mp3`.aac"
  33. done
  34. }
  35. function speedup {
  36. base=$(basename $1)
  37. ext="${base##*.}"
  38. base="${base%.*}"
  39. ffmpeg -i $1 -filter:v "setpts=0.5*PTS" $base'_speed.'$ext
  40. notify-send "your video has got speed. Enjoy"
  41. }
  42. # aac + image = mp4
  43. aactomkv ()
  44. {
  45. for files in *.aac
  46. do
  47. ffmpeg -y -loop 1 -framerate 1/25 -i image.jpg -i "$file" -vf "scale='min(1280,iw)':-2, format=yuv420p" -c:v libx264 -preset veryslow -crf 0 -c:a copy "`basename "$file" .aac`.mkv"
  48. done
  49. }
  50. function hdimg () {
  51. # converts images into HD wallpapers 1920x1080
  52. # Accepts globbing as first argument e.g. jpg as first argument will convert all
  53. # jpgs into HD images and saves them into new dir resized in same directory
  54. ext="$1"
  55. mkdir resized 2>/dev/null
  56. for file in *."$ext"
  57. do
  58. convert $file -resize 1920x1080! resized/`basename $file .$ext`.$ext
  59. done
  60. }
  61. function mp3tomp4 ()
  62. # Makes youtube compliant mp4 from a image( must be a .jpg and its size should be a multiple of 2. Use imagemagic's identify to measure it `identify image.jpg`
  63. # if not a multiple of 2, `convert image.jpg -resize 1024x512! imag2.jpg`
  64. # Usage:
  65. # mp3tomp4 image.jpg audio.mp3 output.mp4
  66. {
  67. ffmpeg -loop 1 -r 1 -i $1 -i $2 -vcodec libx264 -acodec copy -shortest $3
  68. }
  69. function pl ()
  70. {
  71. mpc searchplay $1
  72. }
  73. function mergeaudio ()
  74. # Create a .txt file in same directory and add files to be merged there as "file file1 and on new line, file file2"
  75. # Usage:
  76. # mergeaudio files.txt output.mp3
  77. {
  78. ffmpeg -f concat -safe 0 -i $1 -c copy $2
  79. }
  80. function mergeaudiotovideo ()
  81. # maps audio with video, first arg should be video, second should be audio and
  82. # third should be the output
  83. {
  84. ffmpeg -i $1 -i $2 -c copy -map 0:v:0 -map 1:a:0 $3
  85. }
  86. # Jobs mail
  87. function apply {
  88. gpg -dq $HOME/bin/misc/job.gpg | mutt -s "Applying for Job" $1
  89. }
  90. # Encrypt a file to myself
  91. function ei {
  92. gpg -er C19D1580 $1
  93. }
  94. # Decrypt a file
  95. function di {
  96. gpg -do $1 $2
  97. }
  98. # Bakchod
  99. function bakchod {
  100. echo "$@" | tr a-zA-Z n-za-mN-ZA-M
  101. }
  102. # mail
  103. function alish {
  104. mutt -s Important bar <<<"$@";
  105. }
  106. # mailto
  107. function mailto {
  108. $TERMINAL -e mutt "$@"
  109. }
  110. function aa_256 ()
  111. {
  112. local o= i= x=`tput op` cols=`tput cols` y= oo= yy=;
  113. y=`printf %$(($cols-6))s`;
  114. yy=${y// /=};
  115. for i in {0..256};
  116. do
  117. o=00${i};
  118. oo=`echo -en "setaf ${i}\nsetab ${i}\n"|tput -S`;
  119. echo -e "${o:${#o}-3:3} ${oo}${yy}${x}";
  120. done
  121. }
  122. function aa_c666 ()
  123. {
  124. local r= g= b= c= CR="`tput sgr0;tput init`" C="`tput op`" n="\n\n\n" t=" " s=" ";
  125. echo -e "${CR}${n}";
  126. function c666 ()
  127. {
  128. local b= g=$1 r=$2;
  129. for ((b=0; b<6; b++))
  130. do
  131. c=$(( 16 + ($r*36) + ($g*6) + $b ));
  132. echo -en "setaf ${c}\nsetab ${c}\n" | tput -S;
  133. echo -en "${s}";
  134. done
  135. };
  136. function c666b ()
  137. {
  138. local g=$1 r=;
  139. for ((r=0; r<6; r++))
  140. do
  141. echo -en " `c666 $g $r`${C} ";
  142. done
  143. };
  144. for ((g=0; g<6; g++))
  145. do
  146. c666b=`c666b $g`;
  147. echo -e " ${c666b}";
  148. echo -e " ${c666b}";
  149. echo -e " ${c666b}";
  150. echo -e " ${c666b}";
  151. echo -e " ${c666b}";
  152. done;
  153. echo -e "${CR}${n}${n}"
  154. }
  155. ht() {
  156. a=$(cat); curl -X POST -s -d "$a" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}' | xclip ;
  157. }
  158. function fs() {
  159. if du -b /dev/null > /dev/null 2>&1; then
  160. local arg=-sbh;
  161. else
  162. local arg=-sh;
  163. fi
  164. if [[ -n "$@" ]]; then
  165. du $arg -- "$@";
  166. else
  167. du $arg .[^.]* ./*;
  168. fi;
  169. }
  170. function dataurl() {
  171. local mimeType=$(file -b --mime-type "$1");
  172. if [[ $mimeType == text/* ]]; then
  173. mimeType="${mimeType};charset=utf-8";
  174. fi
  175. echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
  176. }
  177. function escape() {
  178. printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u);
  179. # print a newline unless we’re piping the output to another program
  180. if [ -t 1 ]; then
  181. echo ""; # newline
  182. fi;
  183. }
  184. function v() {
  185. if [ $# -eq 0 ]; then
  186. vim .;
  187. else
  188. vim "$@";
  189. fi;
  190. }
  191. function o() {
  192. if [ $# -eq 0 ]; then
  193. ranger .;
  194. else
  195. ranger "$@";
  196. fi;
  197. }
  198. function tre() {
  199. tree -aC -I '.git|node_modules|bower_components' --dirsfirst "$@" | less -FRNX;
  200. }
  201. ix() {
  202. local opts
  203. local OPTIND
  204. [ -f "$HOME/.netrc" ] && opts='-n'
  205. while getopts ":hd:i:n:" x; do
  206. case $x in
  207. h) echo "ix [-d ID] [-i ID] [-n N] [opts]"; return;;
  208. d) $echo curl $opts -X DELETE ix.io/$OPTARG; return;;
  209. i) opts="$opts -X PUT"; local id="$OPTARG";;
  210. n) opts="$opts -F read:1=$OPTARG";;
  211. esac
  212. done
  213. shift $(($OPTIND - 1))
  214. [ -t 0 ] && {
  215. local filename="$1"
  216. shift
  217. [ "$filename" ] && {
  218. curl $opts -F f:1=@"$filename" $* ix.io/$id
  219. return
  220. }
  221. echo "^C to cancel, ^D to send."
  222. }
  223. curl $opts -F f:1='<-' $* ix.io/$id
  224. }
  225. shebang() {
  226. if i=$(which $1);
  227. then
  228. printf '#!/usr/bin/env %s\n\n' $1 > $2 && chmod 755 $2 && vim + $2 && chmod 755 $2;
  229. else
  230. echo "'which' could not find $1, is it in your \$PATH?";
  231. fi;
  232. # in case the new script is in path, this throw out the command hash table and
  233. # start over (man zshbuiltins)
  234. rehash
  235. }
  236. function webcam () {
  237. mplayer -cache 128 -tv driver=v4l2:width=350:height=350 -vo xv tv:// -noborder -geometry "+1340+445" -ontop -quiet 2>/dev/null >/dev/null
  238. }
  239. function psg () {
  240. pass generate "$1" --clip
  241. cd ~/.password-store
  242. git push
  243. cd -
  244. }
  245. function weather() {
  246. curl wttr.in/"$1"
  247. }
  248. function cnsp () {
  249. sudo /etc/rc.d/crond stop
  250. sleep 1
  251. sudo /etc/rc.d/crond status
  252. }
  253. function cnst () {
  254. sudo /etc/rc.d/crond start
  255. sleep 1
  256. sudo /etc/rc.d/crond status
  257. }
  258. function ko () {
  259. a=$(cat)
  260. curl -X POST -s -d "raw:$a" http://kopy.io/documents | awk -F '"' '{print "http://kopy.io/"$4}'
  261. }
  262. function pb()
  263. {
  264. local url='https://paste.c-net.org/'
  265. if (( $# )); then
  266. local file
  267. for file; do
  268. curl -s \
  269. --data-binary @"$file" \
  270. --header "X-FileName: ${file##*/}" \
  271. "$url"
  272. done
  273. else
  274. curl -s --data-binary @- "$url"
  275. fi
  276. }
  277. function pg()
  278. {
  279. local url='https://paste.c-net.org/'
  280. if (( $# )); then
  281. local arg
  282. for arg; do
  283. curl -s "${url}${arg##*/}"
  284. done
  285. else
  286. local arg
  287. while read -r arg; do
  288. curl -s "${url}${arg##*/}"
  289. done
  290. fi
  291. }
  292. function ap()
  293. {
  294. sudo apt update && sudo apt full-upgrade -V
  295. }
  296. function gc()
  297. {
  298. git commit $* -m $1
  299. }
  300. function gct()
  301. {
  302. token=$(pass gist/github)
  303. curl -u Awan:$token -X POST https://api.github.com/user/repos -d '{"name":"'$1'"}'
  304. git init
  305. git remote add origin git@github.com:Awan/$1.git
  306. }
  307. endot ()
  308. {
  309. cd ~/cfg
  310. tar czf encrypted.tar.gz ncmpcpp mpd etc/.local/share/misc misc
  311. gpg -er 5CBC7BC7 encrypted.tar.gz
  312. rm encrypted.tar.gz
  313. }
  314. dedot ()
  315. {
  316. cd ~/cfg
  317. gpg -do encrypted.tar.gz encrypted.tar.gz.gpg
  318. tar xvf encrypted.tar.gz
  319. rm encrypted.tar.gz
  320. }
  321. sne ()
  322. {
  323. sudo systemctl --now enable $1
  324. }
  325. snd ()
  326. {
  327. sudo systemctl stop $1
  328. }
  329. gadd ()
  330. {
  331. sudo gpasswd -a "$USER" "$1"
  332. }
  333. ed ()
  334. {
  335. "$EDITOR" "$@"
  336. }
  337. mpgo ()
  338. {
  339. mkdir /tmp/mpv -p
  340. if [[ -n $1 ]]; then
  341. clipboard=$1
  342. else
  343. clipboard=$(xsel -b)
  344. fi
  345. if [[ $clipboard =~ ^http ]] || [[ -f $clipboard ]]; then
  346. echo "$clipboard" > /tmp/mpv/last_link
  347. # ytdl messes up direct links for some reason (slow)
  348. mpv --no-ytdl --screenshot-template="./%tY.%tm.%td_%tH:%tM:%tS" "$clipboard"
  349. elif [[ $clipboard =~ ^magnet ]]; then
  350. echo "$clipboard" > /tmp/mpv/last_link
  351. peerflix "$clipboard" --mpv -- --no-ytdl \
  352. --screenshot-template="./%tY.%tm.%td_%tH:%tM:%tS"
  353. fi
  354. }
  355. mplast ()
  356. {
  357. mpgo "$(< /tmp/mpv/last_link)"
  358. }
  359. arec ()
  360. {
  361. if [[ $# -ne 1 ]]; then
  362. return 1
  363. fi
  364. arecord -vv -f wav "$1"
  365. }
  366. ram ()
  367. {
  368. local sum
  369. local items
  370. local app="$1"
  371. if [ -z "$app" ]; then
  372. echo "First argument - pattern to grep from processes"
  373. else
  374. sum=0
  375. for i in `ps aux | grep -i "$app" | grep -v "grep" | awk '{print $6}'`; do
  376. sum=$(($i + $sum))
  377. done
  378. sum=$(echo "scale=2; $sum / 1024.0" | bc)
  379. if [[ $sum != "0" ]]; then
  380. echo "${fg[blue]}${app}${reset_color} uses ${fg[green]}${sum}${reset_color} MBs of RAM."
  381. else
  382. echo "There are no processes with pattern '${fg[blue]}${app}${reset_color}' are running."
  383. fi
  384. fi
  385. }
  386. pdfmerge ()
  387. {
  388. local tomerge
  389. tomerge=""
  390. for file in "$@"; do
  391. tomerge="$tomerge $file"
  392. done
  393. pdftk "$tomerge" cat output mergd.pdf
  394. }
  395. bkmeup ()
  396. {
  397. cp -riv $1 ${1}-$(date +%d-%m-%Y-%H:%M).bak
  398. }
  399. sprunge ()
  400. {
  401. tail -n +1 -- "$@" | curl -F 'sprunge=<-' http://sprunge.us
  402. }
  403. removeaudio ()
  404. {
  405. ffmpeg -i $1 -vcodec copy -an $2
  406. }
  407. apkname ()
  408. {
  409. package="$1"
  410. adb shell pm list packages | awk -F: -v pkg=$package 'index($0, pkg) {print $2}'
  411. }
  412. apkpath ()
  413. {
  414. package="$1"
  415. adb shell pm path $package | awk -F: -v pkg=$package 'index($0, pkg) {print $2}'
  416. }
  417. playandroid ()
  418. # play music files on android. first argument should be the path and second
  419. # should be the file format, like, audio/mp3, video/mp4
  420. {
  421. music_file="$1"
  422. format="$2"
  423. adb shell am start -a android.intent.action.VIEW -d file://$music_file -t $format
  424. }
  425. apkrun ()
  426. # starts an apk which you give to it as first argument. You can get the name of
  427. # the package by using `adb shell pm list packages`
  428. {
  429. package_name="$1"
  430. adb shell monkey -p $package_name 1
  431. }
  432. apkstop ()
  433. {
  434. package_name="$1"
  435. adb shell am force-stop $package_name
  436. }
  437. apkun ()
  438. {
  439. adb shell pm uninstall "$1"
  440. }
  441. lightmin ()
  442. # Set brightness to minimum
  443. {
  444. echo 100 | sudo tee /sys/class/backlight/intel_backlight/brightness
  445. }
  446. lightmax ()
  447. # Set brightness to maximum
  448. {
  449. echo 852 | sudo tee /sys/class/backlight/intel_backlight/brightness
  450. }
  451. #light ()
  452. # Set brightness as first argument
  453. #{
  454. # new_brightness="$1"
  455. # echo $new_brightness | sudo tee
  456. # /sys/class/backlight/intel_backlight/#brightness
  457. #}
  458. bulkrename ()
  459. # replace spaces with underscores, change upper to lower case, remove extra # underscores.
  460. {
  461. find "$1" -depth | while read line; do
  462. dir="$(dirname "$line")"
  463. old="$(basename "$line")"
  464. new="$(echo $old | tr ' ' '_' \
  465. | tr -d '()[]{},?!' | tr -d "'" \
  466. | tr '[[:upper:]]' '[[:lower:]]' \
  467. | sed 's/__/_/g' | sed 's/_-_/-/g' )"
  468. [[ "$old" != "$new" ]] && mv -iv "$dir/$old" "$dir/$new"
  469. done
  470. }
  471. tf ()
  472. {
  473. string="$1"
  474. toilet -f smmono12.tlf $string | lolcat
  475. }
  476. aur ()
  477. {
  478. cd $HOME/git
  479. package_name="$1"
  480. aur_url="https://aur.archlinux.org"
  481. git clone $aur_url/$package_name
  482. cd $package_name
  483. vim PKGBUILD
  484. }
  485. #haq ()
  486. ## Listen to Holy Quran's Ayah by numbers.
  487. #{
  488. #zero_pad(){
  489. # # zero_pad <string> <length>
  490. # [ ${#1} -lt $2 ] && printf "%0$(($2-${#1}))d" ''
  491. # printf "%s\n" "$1"
  492. #}
  493. #which_surah="$1"
  494. #which_ayah="$2"
  495. #qari="$3"
  496. #shuraim_dir=$HOME/kit/verses/shuraim
  497. #sudais_dir=$HOME/kit/verses/sudais
  498. #surah=$(zero_pad $which_surah 3)
  499. #ayah=$(zero_pad $which_ayah 3)
  500. #
  501. #play_shuraim(){
  502. # /usr/bin/mplayer $shuraim_dir/$surah$ayah.mp3
  503. #}
  504. #play_sudais(){
  505. # /usr/bin/mplayer $sudais_dir/$surah$ayah.mp3
  506. #}
  507. #if [[ $qari == 'shuraim' ]]; then
  508. # play_shuraim
  509. #fi
  510. #
  511. #if [[ $qari == 'sudais' ]]; then
  512. # play_sudais
  513. #fi
  514. #}
  515. gfc ()
  516. # git initial commit
  517. {
  518. arabic="بِسْمِ ٱللّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيم"
  519. english='In the name of Allah SWT, the most Gracious, the most Merciful.'
  520. git commit -m "$arabic - $english"
  521. }
  522. ports ()
  523. {
  524. sudo lsof -nPi | grep -i listen | awk '{print substr($1, 1, 7), substr($3, 1, 7), $5, $8, $9}' | sort | uniq | tr ' ' '\t'
  525. }
  526. listcon ()
  527. {
  528. port=$1
  529. host=$(uname)
  530. netstat -atn | grep "$port" | sort -k5
  531. }
  532. r ()
  533. {
  534. if [[ "$TERM" == rxvt-unicode-256color ]]; then
  535. ranger
  536. else
  537. urxvtc -e ranger 2>/dev/null || urxvt -e ranger &
  538. fi
  539. }
  540. replace_all() { ag "$1" -l | xargs sed -i -e "s/$1/$2/g" }
  541. aw ()
  542. # search arch wiki
  543. {
  544. query="$1"
  545. site="https://wiki.archlinux.org/index.php?search="
  546. $BROWSER $site$query
  547. }