You are not logged in.

#1751 2012-03-27 11:59:05

bloom
Member
Registered: 2010-08-18
Posts: 749
Website

Re: Post your handy self made command line utilities

Colored find results:

f() {
    find . -iregex ".*$@.*" -printf '%P\0' | xargs -r0 ls --color=auto -1d
}

Last edited by bloom (2012-03-28 12:48:53)


gh · da · ds

Offline

#1752 2012-03-27 20:40:03

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Post your handy self made command line utilities

I couldn't find a program that merges PDFs and makes a toc using their titles. So I made this script.

pgcount=1
cmd='gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=realworldhaskell.pdf'
for pdf in "$@"
do
   pages="$( pdfinfo "$pdf" | grep '^Pages: *' | sed 's,^Pages: *\([:digit:]*\),\1,' )"
   title="$( pdfinfo "$pdf" | grep '^Title: *' | sed 's,^Title: *\([^ ].*\),\1,' | tr ' ' ' ' )"
   cmd="$cmd -c '[/Page $pgcount /View [/XYZ null null null] /Title ($title) /OUT pdfmark'"
   pgcount=$(( pgcount + pages ))
done
cmd="$cmd -f $@"
eval "$cmd"

Downsides, gs is sloooooow, the output is larger than if I used pdftk for merging (no idea how to add toc in pdftk) and I'm getting some weird font errors that are not in the component PDFs.

Last edited by fsckd (2012-03-28 01:48:35)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#1753 2012-03-28 01:19:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

fsckd, one miniscule improvement - unless I'm missing something - would be to just ditch the 'pdfs="$pdfs $pdf"' line as that just REBUILDS the $@ line.  Take that line out of the loop, then just use 'cmd="$cmd -f $@"' on the second to last line.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#1754 2012-03-28 01:47:50

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Post your handy self made command line utilities

Trilby, you're correct. In the original script the input is not a list of pdfs and so that separate list has to be built while they are processed. But in this case the input is a list of pdfs to be merged. Therefore that line 'pdfs="$pdfs $pdf"' is redundant. I removed it above.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#1755 2012-03-28 03:03:11

mtbrooks33
Member
Registered: 2011-06-08
Posts: 52

Re: Post your handy self made command line utilities

Ment to post in a different forum! Deleted.

Last edited by mtbrooks33 (2012-03-28 03:03:51)

Offline

#1756 2012-04-04 18:07:02

kuraku
Member
Registered: 2012-01-03
Posts: 202

Re: Post your handy self made command line utilities

I use a few short and simple scripts, that i wrote many months (even years) ago, which i use on daily basis.

frename.sh - it will rename any file to lowercase and it will use underscores instead of spaces:

#!/usr/bin/env bash

if [ $# -gt 0 ]
then
 dir="$(echo $1)"
 cd $dir

  for f in *; do
    mv "$f" "$(echo "$f" | tr -s " " "_" | tr "A-Z" "a-z")" 2>/dev/null
  done

else
 echo "$0 - renamer that removes spaces and upper case letters from files
Usage: $0 [directory]" >&2
fi

pardel.sh - it will remove all searched files in given directory. I created it when i edited icons where i had to remove same file (example: "gtk-edit.png") from many subdirectories in given directory:

#!/usr/bin/env bash

if [ $# -gt 1 ]; then
  directory="$(echo $1)"
  query="$(echo $2)"

  #echo search results
  echo -e "Files found: \n"
  #search
  find "$directory" -iname "$query" 2>/dev/null | grep --color=auto "$query"
  #sanity check
  echo "Are you sure that you want to delete these files? (y/N)"; read answer
  if [ "$answer" = "[Y|y]" ]; then
    rm -f $(find $DIR -iname "$FILE")
  fi

else

  #"help"
  echo "$0 - delete all files from defined directory and all sub directories in it
Usage: $0 [directory] [file search query]

Examples:
  $0 . bad_file.txt
  $0 /home/foo/temp_dir IMG_0457.JPEG" >&2

fi

imageconvert.sh - it will resize and optimize images (it uses imagemagick). It is not perfect at all but i'm doing my best to improve it over time:

#!/usr/bin/env bash

if [ $# -gt 3 ]
then
 directory="$(echo $1)"
 quality="$(echo $2)"
 geometry="$(echo $3)"
 file_format="$(echo $4)"

  cd $directory && echo 'Processing:'
  mkdir -p converted_images/src/
  for f in *
    do
      convert -geometry $geometry "$f" converted_images/src/"$f"
      convert -quality $quality converted_images/src/"$f" converted_images/"${f%%.*}.$file_format"
      echo "- $f"
    done
  rm -Rf converted_images/src/
else
 echo "Usage: $0 [directory] [quality in % (0-100%)] [resolution by X axis (800, 990...)] [image format (jpeg, png...)]

Examples:
  $0 . 85 650 jpeg
  $0 ~/Images 95 1024 png" >&2
fi

Offline

#1757 2012-04-04 18:16:05

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

kuraku wrote:

frename.sh - it will rename any file to lowercase and it will use underscores instead of spaces:

If you have files abc and ABC, the latter will overwrite the former.
You should modify the script so that it saves the renamed files somewhere else.

Last edited by karol (2012-04-04 18:17:06)

Offline

#1758 2012-04-04 19:02:24

bloom
Member
Registered: 2010-08-18
Posts: 749
Website

Re: Post your handy self made command line utilities

civilised_screensaver

This script has to be backgrounded and its purpose is to prevent Xscreensaver's ignition when the active window's state is fullscreen:

#! /bin/dash

set -- $(grep -i '^timeout:' ~/.xscreensaver | sed 's/[^0-9]\+\([0-9]\+\):\([0-9]\+\):\([0-9]\+\)/\1 \2 \3/')

if [ $# -eq 0 ] ; then
    printf '%s' "no timeout data could be extracted from Xscreensaver's configuration file" >&2
    exit 1
fi

delete_zeros() {
    printf '%s' "$@" | sed 's/0*\([0-9].*\)/\1/'
}

hours=$(delete_zeros "$1")
minutes=$(delete_zeros "$2")
seconds=$(delete_zeros "$3")

timeout=$((hours * 60 * 60 + minutes * 60 + seconds - 5))

if [ $timeout -lt 1 ] ; then
    printf '%s' "Xscreensaver's sleep timeout is too short" >&2
    exit 1
fi

while true ; do
    sleep $timeout
    awid=$(xdotool getactivewindow 2>/dev/null)
    if [ $? -eq 0 ] ; then
        wstate=$(xprop -id "$awid" _NET_WM_STATE)
        case "$wstate" in
            *_NET_WM_STATE_FULLSCREEN)
                xscreensaver-command -deactivate >&- 2>&- &
                ;;
        esac
    fi
done

Last edited by bloom (2012-04-11 13:21:45)


gh · da · ds

Offline

#1759 2012-04-06 13:26:08

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Re: Post your handy self made command line utilities

Starts Fallout, that needs a wine virtual desktop, fullscreen. I set the game resolution equals the desktop resolution, so there is not need of xrandr.

#!/bin/sh
set -m

function getWindowIdImpl() {
    PANEL="`wmctrl -l | sed -n -e '/ Wine desktop$/ s_ .*$__ p'`"
}

function getWindowId() {
    PANEL=""
    getWindowIdImpl
    while [ x = x$PANEL ] ;do
        sleep 0.5s
        getWindowIdImpl
    done
}

cd "`dirname "$0"`"

export WINEPREFIX="`readlink -e .`"/wine
export XDG_DATA_HOME="$WINEPREFIX"/env
export WINEDLLOVERRIDES="mshtml="
export WINEARCH="win32"

cd wine/drive_c/Fallout

#xrandr -s 640x480
wine falloutw.exe &
getWindowId
wmctrl -i -r "$PANEL" -b add,fullscreen

fg
#xrandr -s 0

I made this one because after years I could try Fallout since it is now free on gog dot com.

Last edited by ezzetabi (2012-04-07 08:49:56)

Offline

#1760 2012-04-10 14:41:23

bloom
Member
Registered: 2010-08-18
Posts: 749
Website

Re: Post your handy self made command line utilities

chomp

#! /bin/dash

bbcodes=$(mktemp)
tmpout=$(mktemp)

while [ $# -gt 0 ] ; do
    if [ -t 1 ] ; then
        printf '%s \033[1;36m%s\033[0m\n' "Chomping" "$1"
    else
        echo "Chomping $1"
    fi
    curl -# -F "file1=@$1" http://ompldr.org/upload -o "$tmpout"
    if [ $? -eq 0 ] ; then
        awk '/BBCode:/ { gsub(/<[^<]*?\/?>/, ""); $1=$1; sub(/^[^:]*:/, ""); print }' "$tmpout" >> "$bbcodes"
        if [ -t 1 ] ; then
            awk '/Info:|File:|Thumbnail:|BBCode:/ { gsub(/<[^<]*?\/?>/, ""); $1=$1; sub(/^/, "\033[1;30m"); sub(/:/, "\033[0m: "); print }' "$tmpout"
        else
            awk '/Info:|File:|Thumbnail:|BBCode:/ { gsub(/<[^<]*?\/?>/, ""); $1=$1; sub(/:/, ": "); print }' "$tmpout"
        fi
    fi
    shift
done

if [ -s "$bbcodes" ] ; then
    cat "$bbcodes" | xsel -bi
fi

rm "$tmpout" "$bbcodes"

Takes files as arguments, uploads each file through omploader and copies all the returned BBCodes to the clipboard.

Last edited by bloom (2012-04-10 14:43:07)


gh · da · ds

Offline

#1761 2012-04-10 16:14:42

dodo3773
Member
Registered: 2011-03-17
Posts: 818

Re: Post your handy self made command line utilities

bloom wrote:

chomp

Takes files as arguments, uploads each file through omploader and copies all the returned BBCodes to the clipboard.

Just added to my $PATH. Awesome script. Thanks.

Offline

#1762 2012-04-12 18:10:25

nomilieu
Member
Registered: 2010-07-03
Posts: 133

Re: Post your handy self made command line utilities

Sweet, I had forgotten some quotation marks.
Anyways, my next post contains an extremely dirty script (eh, I suck at bash currently) to make forum posts here.

Also, I thought I had gotten rid of that "-c" option. Oh well.
Anyway, you get the idea. The obvious use for this script (properly rewritten) would be to easily post more scripts (or other file contents).

I'm not sure how to take input from a pipe, but that would be cool as well.

Last edited by nomilieu (2012-04-12 18:34:21)

Offline

#1763 2012-04-12 18:30:10

nomilieu
Member
Registered: 2010-07-03
Posts: 133

Re: Post your handy self made command line utilities

#!/bin/bash
#
# archbbs - a script to post to the Arch Linux forums
#
# It still needs some work though.
# For example, the -m and -f options should be mutually exclusive,
# and obviously it shouldn't bit spitting out web pages to the console.
#

USERNAME='username'
PASSWORD='password'
TOPIC='invalid'
MESSAGE='message'

while getopts ":u:p:t:m:f:c"  opt; do
	case $opt in
	u)
		USERNAME=$OPTARG
	;;
	p)
		PASSWORD=$OPTARG
	;;
	t)
		TOPIC=$OPTARG
	;;
	m)
		MESSAGE=$OPTARG
	;;
	f)
		MESSAGE=$(< $OPTARG)
	;;
	c)	MESSAGE="[code]$MESSAGE[/code]"
	;;
	\?)
		echo "Invalid option: -$OPTARG" && exit
	esac
done

MESSAGE=$(echo "$MESSAGE" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')

curl -d "form_sent=1&redirect_url=index.php&req_username=$USERNAME&req_password=$PASSWORD&login=Login" -c cookie "https://bbs.archlinux.org/login.php?action=in"
curl -L -b cookie -d "form_sent=1&req_message=[code]$MESSAGE[/code]" "https://bbs.archlinux.org/post.php?tid=$TOPIC"

Offline

#1764 2012-04-12 23:48:23

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

Check if any lines in a file, read from stdin, are longer than a maximum (currently 80).

linecount.lhs

LineCount
=========

The maximum length of a line.

> maxlen = 80

Read each line from stdin and print out an error if it is longer than the
maximum length.

> main :: IO ()
> main = checkLine 1
>     where checkLine n = do line <- getLine
>                            let len = length line
>
>                            putStr $ if len > maxlen
>                              then "Length of line " ++ show n ++ 
>                                    " is "  ++ show len ++ "\n"
>                              else ""
>
>                            checkLine (n + 1)

Compile with ghc --make linecount.lhs

Offline

#1765 2012-04-13 00:02:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

Barrucadu, you could also just use `wc -L`


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#1766 2012-04-13 00:17:33

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

Argh, I did not know wc did that tongue

edit: Actually, wc -L prints the length of the longest line. I wanted to print a message about every line over a certain length.

Last edited by Barrucadu (2012-04-13 00:18:21)

Offline

#1767 2012-04-13 00:23:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

Yup, that's correct.  I suppose I would have approached that task with a bash script using wc, or awk.  Haskell works too.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#1768 2012-04-13 00:59:16

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Post your handy self made command line utilities

Trilby wrote:

Yup, that's correct.  I suppose I would have approached that task with a bash script using wc, or awk.  Haskell works too.

awk 'length>80' file

Offline

#1769 2012-04-14 18:56:16

MatejLach
Member
From: United Kingdom
Registered: 2011-06-22
Posts: 314
Website

Re: Post your handy self made command line utilities

So here's a little, dirty bash function, that lets you search the web from cl.
This is (mostly) for UK web as I live in England, but you can easily replace urls. 
Just add this to your .bashrc.

## Search interfaces

# Google search bash function
google() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.google.co.uk/search?q=$search"
}

# Google images search bash function
images() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.google.co.uk/search?tbm=isch&hl=en&source=hp&biw=1366&bih=679&q=$search"
}

# YouTube search bash function
videos() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.youtube.com/results?search_query=$search"
}

# Google books search bash function
book() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.google.co.uk/search?q=$search&btnG=Search+Books&tbm=bks&tbo=1"
}

#Bing search bash function
bing() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.bing.com/search?q=$search"
}

# DuckDuckGo search bash function
ddg() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://duckduckgo.com/?q=$search"
}

# Wikipedia English search bash function
wiki() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://en.wikipedia.org/w/index.php?search=$search"
}

# Android Play Store search bash function
android() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://play.google.com/store/search?q=$search"
}

# Grooveshark music bash search function
music() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://grooveshark.com/#!/search?q=$search"
}

# Amazon UK search bash function
amazon() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=$search&x=0&y=0"
}

# Amazon Kindle search bash function
kindle() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Ddigital-text&field-keywords=$search&x=0&y=0"
}

# Amazon MP3 search bash function
mp3store() {
    search=""
    for term in $*; do
        search="$search%20$term"
    done
    xdg-open "http://www.amazon.co.uk/s/ref=nb_sb_ss_i_0_8?url=search-alias%3Ddigital-music&field-keywords=$search&x=0&y=0"
}

Note: The search results will open in your default browser.
P.S. I have couple more there, but this should be enough, as an example.

Last edited by MatejLach (2012-04-14 18:59:45)

Offline

#1770 2012-04-14 19:03:47

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: Post your handy self made command line utilities

@MatejLach: You may want to test surfraw in your spare time. :-)


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#1771 2012-04-14 19:06:17

MatejLach
Member
From: United Kingdom
Registered: 2011-06-22
Posts: 314
Website

Re: Post your handy self made command line utilities

bohoomil wrote:

@MatejLach: You may want to test surfraw in your spare time. :-)

Looks promising...
I'll give it a shot.
Thanks!

Offline

#1772 2012-04-14 23:28:47

PReP
Member
From: Sweden
Registered: 2010-06-13
Posts: 359
Website

Re: Post your handy self made command line utilities

server_send.sh:

#usage: server_send [file]

scp -r -P xxx $1 prep@192.168.x.x:/home/xxx/$2

Quite simple and naive semi-script-could-be-an-alias-just
And it is quite crude at the moment.

But it does save time for me,
but i will probably make it more useful later.

I was just glad that i got the idea smile

I have a crush on bash/sh/coreutils because of things like this.

Last edited by PReP (2012-04-14 23:30:05)


. Main: Intel Core i5 6600k @ 4.4 Ghz, 16 GB DDR4 XMP, Gefore GTX 970 (Gainward Phantom) - Arch Linux 64-Bit
. Server: Intel Core i5 2500k @ 3.9 Ghz, 8 GB DDR2-XMP RAM @ 1600 Mhz, Geforce GTX 570 (Gainward Phantom) - Arch Linux 64-Bit
. Body: Estrogen @ 90%, Testestorone @ 10% (Not scientific just out-of-my-guesstimate-brain)

Offline

#1773 2012-04-16 18:29:54

stealthy
Member
Registered: 2011-05-02
Posts: 67

Re: Post your handy self made command line utilities

Simple automatic conky dock configuration, https://github.com/Afterburn/conky_auto … oconfig.py

Last edited by stealthy (2012-04-16 18:30:20)


clipodder-git A small simple cron-friendly podcast downloader, with support for arbitrary user defined media types (pdf, html, etc...)

Offline

#1774 2012-04-16 22:45:12

Paaskehare
Member
From: Denmark
Registered: 2008-11-09
Posts: 59
Website

Re: Post your handy self made command line utilities

I made these two simple functions to keep track of which episode im at of a given TV Show.

mplayer "alias" to append the filename to a playlist

mplayer() {
  echo "$@" >> ~/.mplayer/playlist
  /usr/bin/mplayer "$@"
}

And "cm" function to search through the playlist with grep, e.g.

`cm family` would return anything from the playlist containg family

cm () {
  if [ -z "$1" ]; then
    cat ~/.mplayer/playlist
  else
    cat ~/.mplayer/playlist | grep -i "$@"
  fi
}

im.ole

Offline

#1775 2012-04-17 18:53:19

scjudd
Member
Registered: 2010-08-18
Posts: 14

Re: Post your handy self made command line utilities

not strictly a "command line utility," but here is my Scramble with Friends solver smile https://github.com/scjudd/descramble

guiscreenshot.png

There's Arch package for it, so if any of you use it and like it, please upvote it! http://aur.archlinux.org/packages.php?ID=58343

I've also recently made a cosketch.com bot smile https://github.com/scjudd/cosketch. No Arch package for this at the moment.

Last edited by scjudd (2012-04-17 18:58:06)

Offline

Board footer

Powered by FluxBB