You are not logged in.

#2026 2013-02-11 17:01:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

Merging with the One-Liners thread...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2027 2013-02-14 22:10:19

benmills
Member
From: Chicago, IL
Registered: 2013-01-11
Posts: 6
Website

Re: Post your handy self made command line utilities

I find myself using tmux sessions for each project I'm working on and I like to have a consistent window layout (term in window 0 and code in window 1). I initially wrote a very simple script to rename my current tmux session and created the windows. I recently refactored it to handle cases where I'm not in tmux or if the session already has more than one window.

#!/bin/bash
session=$(basename $PWD)

if [ -z $TMUX ]; then
  tmux new-session -s $session \; rename-window "term" \; new-window -n "code"
else
  tmux rename-session $session
  tmux set -t $session default-path $PWD
  tmux rename-window -t $session:0 "term"

  if [ $(tmux list-windows -t $session | wc -l) -gt 1 ]; then
    tmux rename-window -t $session:1 "code"
  else
    tmux new-window -t $session:1 -n "code"
  fi
fi

Offline

#2028 2013-02-15 19:46:48

gugah
Member
Registered: 2013-01-02
Posts: 50

Re: Post your handy self made command line utilities

I usually repeat bash commands quite a lot (eg. when debugging a program I run the same command to compile it many times). I've made this script to erase duplicated consecutive lines from the user's .bash_history so as to navigate smoothly through all the commands in the history. It also creates a backup of the former .bash_history file.
Hope it helps wink

#! /usr/bin/python2

from os.path import expanduser
import os

home = expanduser('~') # Get home directory
try:
    f_in = open(home + '/.bash_history', 'r')
except:
    raise IOError('No .bash_history file found')
f_out = open(home + '/.bash_history.new', 'w')
last = None
for line in f_in.readlines():
    if line != last:
        last = line
        f_out.write(line)
f_in.close()
f_out.close()

cmd = 'mv ' + home + '/.bash_history ' + home + '/.bash_history.backup'
os.system(cmd)
cmd = 'mv ' + home + '/.bash_history.new ' + home + '/.bash_history'
os.system(cmd)

Edit: I know it could be done with a single bash script line, I've made it just for fun (and because one of the scripts I've found didn't work at all).

Last edited by gugah (2013-02-15 19:48:45)


"The problem with quotes on the Internet is that it is hard to verify their authenticity." ~ Abraham Lincoln

Offline

#2029 2013-02-16 19:37:38

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Post your handy self made command line utilities

@gugah
bash has it's own method for ignoring consecutive duplicate entries that you can add to .bashrc

export HISTCONTROL=ignoredups

not as much fun as making your own python script though... smile


You're just jealous because the voices only talk to me.

Offline

#2030 2013-02-16 22:48:26

gugah
Member
Registered: 2013-01-02
Posts: 50

Re: Post your handy self made command line utilities

@moetunes

thanks for that tip!


"The problem with quotes on the Internet is that it is hard to verify their authenticity." ~ Abraham Lincoln

Offline

#2031 2013-02-19 22:23:33

deepsoul
Member
From: Earth
Registered: 2012-12-23
Posts: 67
Website

Re: Post your handy self made command line utilities

Two almost trivial utilities that have saved me quite some typing over the years are the following shell functions:

# mkdir and cd into new dir:
# for bash:
function mkcd() { mkdir "$@" && cd "${!#}"; }
# for zsh:
function mkcd() { mkdir "$@" && cd "${argv[${#argv}]}"; }

# leave and remove empty directory:
function rmcd() { cd .. && rmdir "${OLDPWD##*/}"; }

Using the last argument as the target directory in mkcd allows to use the -p option of mkdir, or to create multiple directories if you want.


Officer, I had to drive home - I was way too drunk to teleport!

Offline

#2032 2013-02-19 22:47:04

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

Re: Post your handy self made command line utilities

In bash 'mkdir -p a/b/c && cd $_' works too and it's slightly shorter.

Offline

#2033 2013-02-19 23:12:06

deepsoul
Member
From: Earth
Registered: 2012-12-23
Posts: 67
Website

Re: Post your handy self made command line utilities

Yes, you are right.  I had never used $_, but rather Alt-. to retrieve the last argument interactively.  Now that I try it out, the same works for zsh too.  Yay, size optimisation! big_smile


Officer, I had to drive home - I was way too drunk to teleport!

Offline

#2034 2013-02-23 07:39:50

yoanar
Member
Registered: 2012-06-10
Posts: 24

Re: Post your handy self made command line utilities

Convert from tabs to n-space, n-spaces to n-spaces, and space-n to tabs. I call it PeaceKeeper, because it's purpose is to keep peace between programmers.

Example:

pkeep -f s2s -s 4 -t 2 -n INFILE.TXT > OUTFILE.TXT

This would translate n-spaces to n-spaces (-f s2s.) It would take every 2-space (-t 2) indent, and turn it into 4-space (-s 4.) Then the output is simply redirected to OUTFILE.TXT. If you give the --self/-i flag, the output is written to the original file instead (-n INFILE.TXT)

It be downloaded from here, released under the GPL smile
SourceForge repo


Meh.

Offline

#2035 2013-02-24 10:22:10

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

Re: Post your handy self made command line utilities

I wanted to open terminals in the most recently visited directory by default.

Since I'm using z, I thought of reusing its data file for that purpose:

#! /bin/dash

pgrep -x urxvtd > /dev/null || urxvtd -q -o -f
_Z_DATA=${_Z_DATA:-"$HOME/.z"}

if [ $# -eq 0 ] ; then
    recpth=$(awk -F '|' '{print $3, $1}' "$_Z_DATA" | sort -n | tail -n 1 | cut -d ' ' -f 2-)
    [ -d "$recpth" ] || recpth=$HOME
    urxvtc -cd "$recpth"
else
    urxvtc $@
fi

Last edited by bloom (2013-02-26 11:01:37)


gh · da · ds

Offline

#2036 2013-02-24 16:23:32

kleim
Member
From: Lyon, France
Registered: 2011-12-14
Posts: 12

Re: Post your handy self made command line utilities

My father receives many stupid and funny powerpoints from his friends. The best and quickest solution to read them is still using the Powerpoint Viewer distributed by Microsoft through Wine. Sadly there is no way to make Thunderbird open powerpoints directly with this program. So I tricked Thunderbird by telling it to open powerpoints using a script that opens the last powerpoint copied in the /tmp folder:

#!/bin/bash
PPTFILE=$(ls -t /tmp | grep .pp | head -1)
cd /tmp
wine "~/.wine/drive_c/Program Files (x86)/Microsoft Office/Office12/PPTVIEW.EXE" "$PPTFILE"

Offline

#2037 2013-02-24 19:28:52

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Post your handy self made command line utilities

kleim wrote:

My father receives many stupid and funny powerpoints from his friends.

I can't believe there are enough stupid and funny Powerpoint presentations to necessitate a script. yikes

Anyway, nice hack. smile

Offline

#2038 2013-03-02 01:16:46

Beg
Member
Registered: 2013-02-24
Posts: 18

Re: Post your handy self made command line utilities

while true; do xsetroot -name $(( $(cat ~/keyboard1.txt | wc -c) / 96)); sleep 1;  done &
sudo cat /dev/input/by-id/usb-046d_G15_Gaming_Keyboard-event-kbd > ~/keyboard1.txt &

Change the usb-046d_G15_Gaming_Keyboard-event-kbd value to your keyboard. This will count keys in DWM xrootset variable. I stole most of the code from somewhere, but credit to that guy!

*Note:I figured out this might not be optimal for SSDs, you could expand this into a small ramfs.*


add cat .hipsterarch to your bashrc, or zshrc

Copy this file to .hipsterarch

 @@@ @@@  @@@@@@  @@@       @@@@@@                                      
 @@! !@@ @@!  @@@ @@!      @@!  @@@                                     
  !@!@!  @!@  !@! @!!      @!@  !@!                                     
   !!:   !!:  !!! !!:      !!:  !!!                                     
   .:     : :. :  : ::.: :  : :. :                                      
  @@@@@@ @@@  @@@  @@@  @@@@@@   @@@@@@@                                
 !@@     @@!  @@!  @@! @@!  @@@ !@@                                     
  !@@!!  @!!  !!@  @!@ @!@!@!@! !@! @!@!@                               
     !:!  !:  !!:  !!  !!:  !!! :!!   !!:                               
 ::.: :    ::.:  :::    :   : :  :: :: :                                
 @@@@@@@ @@@  @@@ @@@  @@@  @@@@@@@  @@@      @@@ @@@@@@@@ @@@@@@@@     
   @@!   @@!  @@@ @@!  @@@ !@@       @@!      @@! @@!      @@!          
   @!!   @!@!@!@! @!@  !@! !@! @!@!@ @!!      !!@ @!!!:!   @!!!:!       
   !!:   !!:  !!! !!:  !!! :!!   !!: !!:      !!: !!:      !!:          
    :     :   : :  :.:: :   :: :: :  : ::.: : :    :       : :: :::     
  @@@@@@@   @@@@@@  @@@  @@@  @@@@@@@   @@@@@@ @@@@@@@ @@@@@@@@ @@@@@@@ 
 !@@       @@!  @@@ @@!@!@@@ !@@       !@@       @@!   @@!      @@!  @@@
 !@! @!@!@ @!@!@!@! @!@@!!@! !@! @!@!@  !@@!!    @!!   @!!!:!   @!@!!@! 
 :!!   !!: !!:  !!! !!:  !!! :!!   !!:     !:!   !!:   !!:      !!: :!! 
  :: :: :   :   : : ::    :   :: :: :  ::.: :     :    : :: :::  :   : :

Last edited by Beg (2013-03-02 11:37:37)

Offline

#2039 2013-03-19 19:41:34

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

Re: Post your handy self made command line utilities

Creates a make friendly header dependency graph for C sources:

#! /bin/dash

_deps() {
    printf "%s\n" "$1"
    local names="$(grep '^#include *"[^"]\+"' "$1" 2> /dev/null | sed 's/[^"]\+"\([^"]\+\)".*/\1/')"
    for nm in $names ; do
        _deps "$nm"
    done
}

suffix=.o

while getopts 'hs:' opt ; do
    case $opt in
        s)
            suffix=$OPTARG
            ;;
        h)
            printf "%s [-h|-s SUFFIX] CSOURCE ...\n" "${0##*/}"
            ;;
    esac
done

shift $((OPTIND - 1))

while [ $# -gt 0 ] ; do
    printf "%s:" "${1%.*}$suffix"
    for nm in $(_deps "$1" | sort -t . -k 2 | uniq) ; do
        printf " %s" "$nm"
    done
    printf "\n"
    shift
done

Can be used like this:

deps *.c > Dependencies

And then, in your Makefile:

include Dependencies

EDIT

In fact, gcc -MM is roughly equivalent to deps!

Last edited by bloom (2013-03-20 08:14:55)


gh · da · ds

Offline

#2040 2013-03-20 01:48:05

sethradio
Member
From: /dev/null
Registered: 2012-01-16
Posts: 53
Website

Re: Post your handy self made command line utilities

"is" written in C.
Is basically checks to see if files exist.
Part of my lovely Sethutils (I'm so egotistical).
http://kingdomofseth.com/projects.html

Last edited by sethradio (2013-04-01 21:20:35)


And lo, it came to pass, that the neophyte encountered the Beplattered One and humbly posed the question, "Oh great master, is it a sin to use vi?" And St. IGNUcius didst thus reply unto him, "No, my young hacker friend, it is not a sin. It is a penance."

Offline

#2041 2013-03-20 10:05:39

i_love_penguins
Member
From: Germany
Registered: 2010-03-30
Posts: 46
Website

Re: Post your handy self made command line utilities

sethradio wrote:

"is" written in C.
Is basically checks to see if files exist.
Part of my lovely Sethutils (I'm so egotistical).
http://kingdomofseth.com/projects

What's the purpose of this? Why don't you just use the built-in functions of bash to check for file existence? Apart from you're using printf wrong - you can combine these three commands into one by just writing

printf("File \"%s\" exists.\n", argv[1]);

That's why the program throws a warning when compiling. Also the program always returns 0 - this implies that you have to compare for strings if you would want to use this program in a shell script - only that will take longer than solely checking for file existence.

Consider this as well-meant criticism smile

Offline

#2042 2013-03-23 10:37:22

ea
Member
Registered: 2012-10-11
Posts: 2

Re: Post your handy self made command line utilities

associates each lines of piped content to a number :

#! /bin/bash

# file with saved shortenings
SAVE_FILE="/tmp/shorten"

# locking directory with saved shortenings
LOCK_DIR="/tmp/shorten.lock"

# lock
function LOCKING_DIR() {

  if mkdir "$LOCK_DIR"
  then
    echo 0
  else
    echo 1
  fi

}

# shorten piped lines if no option or -a
if [ "$1" = "" ] || [ "$1" == "-a" ]
then

  # lock directory
  [ $(LOCKING_DIR) = 1 ] && exit 1
  trap 'rm -rf "$LOCK_DIR"' 0

  # get lines
  [ -f "$SAVE_FILE" ] && lines=$(<"$SAVE_FILE") || lines=""

  # get number of lines
  [ -z "$lines" ] && num=0 || num=$(echo "$lines" | wc -l)

  # read piped content line by line
  while read line
  do
  
    [ "$line" == "" ] && continue
 
    # prepended pwd if option -a
    [ "$1" = "-a" ] && line=$(pwd)/$line

    # test if line already in file
    match=$(echo "$lines" | grep -nFm1 "$line")
    if [ "$match" != "" ]
    then
      if [ "$line" = "$(echo "$match" | cut -f2- -d:)" ]
      then
        echo "$match"
        continue
      fi
    fi 
     
    num=$(( $num + 1 ))
   
    # add to file 
    echo "$line" >> "$SAVE_FILE"

    # add to lines
    [ -z "$lines" ] && lines="$line" || lines=$lines$(echo -e "\n")$line

    # echo with shortening
    echo "$num:$line"

  done

  exit 0    

fi

# list shortenings if option -l
if [ "$1" = -l ] 
then
  num=0

  [ -f "$SAVE_FILE" ] && grep -n "" "$SAVE_FILE"

  exit 0
fi

# clear shortenings if option -c
if [ "$1" = -c ] 
then

  # lock directory
  [ $(LOCKING_DIR) = 1 ] && exit 1
  trap 'rm -rf "$LOCK_DIR"' 0
  
  # removing file
  rm -f "$SAVE_FILE"

  exit 0

fi

# otherway, echo lines with numbers specified in parameters
[ -f "$SAVE_FILE" ] && for var in "$@"
do
  case $var in
      ''|*[!0-9]*) ;;
      *) sed -n "${var}p" "$SAVE_FILE" ;;
  esac
done

can be used like this ( with the file in /usr/bin/shorten ) :

# ls
bar  foo.d  日本
# ls | shorten
1:bar
2:foo.d
3:日本
# ls */ -d | shorten -a
4:/tmp/test/foo.d/
# shorten -l
1:bar
2:foo.d
3:日本
4:/tmp/test/foo.d/
# shorten
Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
5:Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
# shorten {1..3}
bar
foo
日本
# rm $(shorten 3)
# ls
bar  foo.d
# shorten -c
# shorten -l
#

it's a cleaned version with some changes of what i use

Last edited by ea (2013-03-23 14:27:03)

Offline

#2043 2013-03-23 13:01:51

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

You may be interested in vidir from moreutils (not trapdoor's).

Offline

#2044 2013-04-01 21:18:19

sethradio
Member
From: /dev/null
Registered: 2012-01-16
Posts: 53
Website

Re: Post your handy self made command line utilities

i_love_penguins wrote:

What's the purpose of this? Why don't you just use the built-in functions of bash to check for file existence?

Hmm.. these utilities are made by a novice programmer, that obviously has not yet learned everything about bash.

Apart from you're using printf wrong - you can combine these three commands into one by just writing

printf("File \"%s\" exists.\n", argv[1]);

That's why the program throws a warning when compiling. Also the program always returns 0 - this implies that you have to compare for strings if you would want to use this program in a shell script - only that will take longer than solely checking for file existence.

Consider this as well-meant criticism smile

Yeah, I've been meaning to fix this for a long time, but I don't see any warnings from the compiler (even with -Wall). Thanks for the reminder though. smile

Last edited by sethradio (2013-04-01 21:25:04)


And lo, it came to pass, that the neophyte encountered the Beplattered One and humbly posed the question, "Oh great master, is it a sin to use vi?" And St. IGNUcius didst thus reply unto him, "No, my young hacker friend, it is not a sin. It is a penance."

Offline

#2045 2013-04-10 21:27:12

marazmista
Member
From: Poland
Registered: 2010-08-04
Posts: 15
Website

Re: Post your handy self made command line utilities

I have made a little app, that log network traffic:
https://github.com/marazmista/simple-netstat


.: github :. ||  radeon-profile - radeon oss driver manager  ||  tar-backup - gui for create backups using tar

Offline

#2046 2013-04-13 01:12:44

felipense
Member
Registered: 2013-04-05
Posts: 62

Re: Post your handy self made command line utilities

pyUTF a python 3 script to convert text files in iso-8859-1 to UTF-8 . Useful to convert sub files downloaded with subdl.

usage : pyUTF <originalFile> <newFile>

#!/usr/bin/python

__author__ = 'felipe'

import sys

def main():
    target = sys.argv[1]
    output = sys.argv[2]
    f = open(target, 'rb')
    f2 = open(output, 'wb')

    for line in f:
        aux = line.decode('iso-8859-1')
        line = aux.encode('utf-8')
        f2.write(line)
    f.close()
    f2.close()

if __name__ == '__main__':
    main()

Offline

#2047 2013-04-13 03:15:23

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

felipense wrote:

pyUTF a python 3 script to convert text files in iso-8859-1 to UTF-8 . Useful to convert sub files downloaded with subdl.

iconv - character set conversion

iconv -f ISO-8859-1 -t UTF-8 filefoo

http://www.gnu.org/software/libiconv/

Offline

#2048 2013-04-13 05:34:23

Awesoham
Member
Registered: 2013-04-09
Posts: 4

Re: Post your handy self made command line utilities

My addition. It's a one-liner to create aliases from the command line, re-source ~/.zshrc and clear the screen.
Note that you must add a source line for the aliases.zsh file manually if you're not an Oh My ZSH user.

function al() {
  echo 'alias' $1'='\'$2\' >> ~/.oh-my-zsh/custom/aliases.zsh
  source ~/.zshrc
  clear
}

Usage:

al aliasname commandname

Note that commandname must be in quotes if it has spaces.

E.g.

al src 'source ~/.zshrc && clear'

Oh, and after you've put this function somewhere, run this for me:

al rmlck 'sudo rm /var/lib/pacman/db.lck'

big_smile

Last edited by Awesoham (2013-04-16 16:55:36)

Offline

#2049 2013-04-13 11:07:17

illusionist
Member
From: localhost
Registered: 2012-04-03
Posts: 498

Re: Post your handy self made command line utilities

@awesoham
Why do you want us to remove our lock file ?


  Never argue with stupid people,They will drag you down to their level and then beat you with experience.--Mark Twain
@github

Offline

#2050 2013-04-15 12:21:05

Resistance
Member
Registered: 2012-09-04
Posts: 26

Re: Post your handy self made command line utilities

Hi,
I recently switched to zsh and I found myself frequently editing .zshrc, and wanting to keep it clean (not echo >> stuff), I wrote this script:

 #!/bin/zsh
[ "$#" -ne 2 ] && echo "Wrong number of arguments!" && exit 1;

filterSpaces() {
    printf '%q' "$1"
}
param1=$(filterSpaces "$1")
param2=$(filterSpaces "$2")


sed -i '/'"$param1"'/ { a\	'"$param2"'
	:a;n;ba;
}' .zshrc

cat ~/.zshrc
source ~/.zshrc 

It seeks a pattern (the first parameter) in the file and append the second parameter to the following line. Example .zshrc:

#set options
    setopt complete_in_word
#autoload stuff
    autoload -U compinit && compinit
#aliases

/usr/local/bin/addOptionsZshrc.sh 'alias' 'alias  "add"="/usr/local/bin/addOptionsZshrc.sh "'

#set options
    setopt complete_in_word
#autoload stuff
    autoload -U compinit && compinit
#aliases
    alias  "add"="/usr/local/bin/addOptionsZshrc.sh "

The next script is basically the same for deleting stuff (takes a pattern in the line to delete as argument):

#!/bin/zsh
[ "$#" -ne 1 ] && echo "Wrong number of arguments!" && exit 1;

filterSpaces() {
    printf '%q' "$1"
}
param1=$(filterSpaces "$1")

sed -i '/'$param1'/ {
	//d
}' .zshrc

cat ~/.zshrc
source ~/.zshrc

To alias it: add alias 'alias "del"="/usr/local/bin/delOptionsZshrc.sh "' (quotes are only necessary around strings with spaces) (:

I use it to edit my .zshrc but one could modify the script to take the name of another file in arguments wink.

Last edited by Resistance (2013-04-15 12:26:32)

Offline

Board footer

Powered by FluxBB