You are not logged in.

#1351 2010-10-31 20:15:14

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: Post your handy self made command line utilities

I recently did Deadbeef playlist parser, that parses the filelist of songs from playlist file and outputs the size of the files in total and gives you option to copy those files somewhere (eg. mp3 player).

The parser itself is modified C code from the Deadbeef playlist loader itself.

Download

Usage:

# Creates filelist of the playlist
./Parse <deabeef playlist>

# Copies all the files to desired folder
./Copy /output/folder

example output:

┌─(.jari//LOLI)────────────────────────────────────────────────────────────────(/media/Storage/DeadBeef Playlists)────┐
└─(22:06)─> ./Parse Top.dbpl 
---------------------
  Deadbeef Playlist  
        Parser       
---------------------

Attempting to read the file.
Reading..

Processing filelist..
Processing done.

The total size of playlist is :
851 GiB

------------------------------------
  Run script with command
  Copy /output/folder
  to copy the songs somewhere.
------------------------------------


Done!

Source for the C code is also included, tho it's basically same as the loading part on Deadbeef.
Forgot, the C binary is 64bit so you need to compile it for 32bit or other arch.

Last edited by Cloudef (2010-11-07 16:03:06)

Offline

#1352 2010-11-01 10:43:02

SiD
Member
From: Germany
Registered: 2006-09-21
Posts: 729

Re: Post your handy self made command line utilities

simple bash script to burn CD iso files

#!/bin/bash
#
# burn CD-R from .iso files using wodim
#

function usage() {
cat << FIN
burncd-iso: Error: missing or too many paramters!

        usage: burncd-iso <iso file to brun>
        
FIN
exit 1
}

if [ $# -ne 1 ]; then
        usage
fi

if [ -e $1 ]; then
        wodim -dao -eject dev=/dev/scd0 $1
else
        echo "File" $1 "not found!"
        exit 1
fi

exit 0

Offline

#1353 2010-11-07 13:57:59

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

I created a script to be able to search PDFs from the commandline (as my favorite PDF reader, MuPDF, lacks the ability to search an entire PDF).

#!/bin/bash

# Extract text
pdf2line "$1" > /tmp/"$1.txt"

# To remove the (empty) text file pdf2line creates
FILE=`ls | grep "$1" | awk -F ".pdf" '{print $1}'`
rm "$FILE".txt

# Search pdf
echo -n "Type a search term: "
read -e TEXT

# View output
cat /tmp/"$1".txt | grep --color=always -i "$TEXT" | less -R

Would appreciate advice for improvements, seeing as my bash skills are elementary. Oh, and if I export in my bashrc the grep option "--color=always", then I can't remove the empty file (because of the reliance on grep, and if it outputs in color, this will cause color codes to appear that mess up the removing of the file). Hope that makes sense.

This script also relies on pdf2line, available in the AUR. I've titled the script "pdfsearch". Invoke the script by pdfsearch $FILENAME, replacing $FILENAME with the name of the pdf (include the .pdf extension).

Last edited by Sara (2010-11-07 14:01:03)


Registed Linux User 483618

Offline

#1354 2010-11-07 14:05:06

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

Re: Post your handy self made command line utilities

FILE=`ls | grep "$1" | awk -F ".pdf" '{print $1}'`

Now that's ugly ;P
I'm not sure you understand how the dot in 'awk -F ".pdf"' works.

Have you tired this: http://aur.archlinux.org/packages.php?ID=36488 ?

Last edited by karol (2010-11-07 14:06:12)

Offline

#1355 2010-11-07 14:09:28

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

karol wrote:
FILE=`ls | grep "$1" | awk -F ".pdf" '{print $1}'`

Now that's ugly ;P
I'm not sure you understand how the dot in 'awk -F ".pdf"' works.

Have you tired this: http://aur.archlinux.org/packages.php?ID=36488 ?

I used -F ".pdf" to specify the delimiter, so that everything before the .pdf would be printed (as pdf2line prints an empty file to $FILE.txt, without the .pdf extension). I would definitely appreciate knowing a better way to accomplish this.

I hadn't known about pdfgrep. Thanks for letting me know (but even with it, I would like to improve my script). Also, that program relies on poppler (while MuPDF does not).

Last edited by Sara (2010-11-07 14:13:50)


Registed Linux User 483618

Offline

#1356 2010-11-07 14:46:36

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

Re: Post your handy self made command line utilities

Sara wrote:

I used -F ".pdf" to specify the delimiter

But I think it's ERE - extended regular expression - so the dot means any character and not the literal dot:

[karol@black ~]$ echo 123.pdf | awk -F ".pdf" '{print $1}'
123
[karol@black ~]$ echo 123pdf | awk -F ".pdf" '{print $1}'
12

If for every pdf file pdf2line creates a txt file, like this: 123.pdf -> 123.txt, you can clean up all of them at once after you're done grepping / once a day etc.:

for i in "*.pdf"; do rm -i ${i%%.*}.txt; done

Last edited by karol (2010-11-07 14:53:04)

Offline

#1357 2010-11-07 14:57:02

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

karol wrote:
Sara wrote:

I used -F ".pdf" to specify the delimiter

But I think it's ERE - extended regular expression - so the dot means any character and not the literal dot:

[karol@black ~]$ echo 123.pdf | awk -F ".pdf" '{print $1}'
123
[karol@black ~]$ echo 123pdf | awk -F ".pdf" '{print $1}'
12

So for every pdf file pdf2line creates a txt file, like this: 123.pdf -> 123.txt? If so, you can clean up all of them at once after you're done grepping / once a day etc.:

for i in *.pdf; do rm -i ${i%%.*}.txt; done

Thank you for the explanation.

The only problem with your suggestion is that it doesn't work if the filename itself has dots in it.

Works when I put *.pdf in quotes, e.g. "*.pdf". Thanks again for the suggestion, for even if I continue to use pdfgrep (I like how it provides page numbers in the PDF), this has been a nice exercise.

Last edited by Sara (2010-11-07 15:21:10)


Registed Linux User 483618

Offline

#1358 2010-11-07 15:05:11

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

Re: Post your handy self made command line utilities

Sara wrote:

Thank you for the explanation.

The only problem with your suggestion is that it doesn't work if the filename itself has dots in it.

Actually, there may be more problems ;P

for i in "*.pdf"; do rm -i ${i%.*}.txt; done

(just one '%' inside the '{...}') is better and works with the dots.

This will remove empty *.txt files in the dir and subdirs:

find . -type f -size 0 -iname "*.txt" -exec rm -i '{}' \;

As you specify the file that you want to grep

pdf2line "$1" > /tmp/"$1.txt"

an even simpler (and one that hopefully works correctly) way to remove that one empty file is

rm -i ${1%.*}.txt

Last edited by karol (2010-11-07 15:16:25)

Offline

#1359 2010-11-07 15:25:04

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

karol wrote:

As you specify the file that you want to grep

pdf2line "$1" > /tmp/"$1.txt"

an even simpler (and one that hopefully works correctly) way to remove that one empty file is

rm -i ${1%.*}.txt

Now that's nice. Tested it, and works perfectly. My updated script is now as follows:

#!/bin/bash

# Extract text
pdf2line "$1" > /tmp/"$1.txt"

# To remove the (empty) text file pdf2line creates
rm -i "${1%.*}".txt

# Search pdf
echo -n "Type a search term: "
read -e TEXT

# View output
cat /tmp/"$1".txt | grep --color=always -i "$TEXT" | less -R

I need the quotes, this time because of the spaces I have in many of my PDF filenames. I removed the "-i" in my personal usage as I don't ever title text files identical to my PDF names, but as I'm posting it online, I kept the "-i" to be on the safe side. Thanks a lot!


Registed Linux User 483618

Offline

#1360 2010-11-07 15:38:06

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

Re: Post your handy self made command line utilities

OK, let's start again :-)
You want to grep a pdf converted to txt and clean up afterwards, so why not

pdftotext $1; grep --color=always -i $2 ${1%.*}.txt | less -R

in one go? You create a file in /tmp but you have to delete the empty file anyway. I'm using pdftotext, but I hope pdf2line behaves the same way - the file won't be empty this time, but will contain the pdf converted to text.
The file will be removed

rm -i ${1%.*}.txt

all the same.

Tell me if it's working as I'm to lazy to install all that haskell packages ;P

Edit: the -'i' switch in grep means 'case insensitive' so 'grep -i hardware' will find 'hardware', 'Hardware', 'HardWare' etc.
If you want to search the same file multiple times, you can avoid converting the file to text every time.

Last edited by karol (2010-11-07 15:43:28)

Offline

#1361 2010-11-07 16:08:42

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

karol wrote:

OK, let's start again :-)
You want to grep a pdf converted to txt and clean up afterwards, so why not

pdftotext $1; grep --color=always -i $2 ${1%.*}.txt | less -R

in one go? You create a file in /tmp but you have to delete the empty file anyway. I'm using pdftotext, but I hope pdf2line behaves the same way - the file won't be empty this time, but will contain the pdf converted to text.
The file will be removed

rm -i ${1%.*}.txt

all the same.

Tell me if it's working as I'm to lazy to install all that haskell packages ;P

Edit: the -'i' switch in grep means 'case insensitive' so 'grep -i hardware' will find 'hardware', 'Hardware', 'HardWare' etc.
If you want to search the same file multiple times, you can avoid converting the file to text every time.

Ah, I already have a lot of Haskell packages installed, which I didn't notice the dependency load. I think the Haskell packages are only needed to install (not use) pdf2line.

It's just printing the entire book (rather than just the text I searched for). Trying to figure out where the error is...

Edit: Ah, here's the problem. pdf2line, even when given the choice, doesn't use the text file it creates. Rather, it just echoes it all out.  The following works (but is probably not elegant):

pdf2line "$1" > "$1.txt"; grep --color=always -i $2 "${1%.pdf.*}.txt" | less -R

Quotes again are needed because of spaces in my file names.

Thanks for helping me with this script.

Last edited by Sara (2010-11-07 16:17:03)


Registed Linux User 483618

Offline

#1362 2010-11-07 16:16:57

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

Re: Post your handy self made command line utilities

Sara wrote:

Ah, I already have a lot of Haskell packages installed, which I didn't notice the dependency load. I think the Haskell packages are only needed to install (not use) pdf2line.

Thats right, they're only makedepends, but downloading and installing it all took some time.

Sara wrote:

It's just printing the entire book (rather than just the text I searched for). Trying to figure out where the error is...

It works like this <scriptname> <filename> <searchterm>, e.g.:

pdfsearch cpumemory.pdf hardware

Offline

#1363 2010-11-07 16:18:30

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Post your handy self made command line utilities

karol wrote:
Sara wrote:

Ah, I already have a lot of Haskell packages installed, which I didn't notice the dependency load. I think the Haskell packages are only needed to install (not use) pdf2line.

Thats right, they're only makedepends, but downloading and installing it all took some time.

Sara wrote:

It's just printing the entire book (rather than just the text I searched for). Trying to figure out where the error is...

It works like this <scriptname> <filename> <searchterm>, e.g.:

pdfsearch cpumemory.pdf hardware

Yes, I used it correctly, but the error was because pdf2line didn't put the text in the text file it created (rather silly of it). See my above edit.


Registed Linux User 483618

Offline

#1364 2010-11-07 16:33:54

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

Re: Post your handy self made command line utilities

pdf2line: Bad parse: 32. Failed reading at byte position 392980

<sigh> Seems I won' test it here.

Instead of

pdf2line "$1" > "$1.txt"; grep --color=always -i $2 "${1%.pdf.*}.txt" | less -R

try

pdf2line "$1" | grep --color=always -i $2 | less -R

but this way you have to convert pdf to txt every time you want to grep for something.
The best way would be to create three separate (and thus optional) steps: convert pdf to txt, grep the txt and remove it.

Last edited by karol (2010-11-07 16:36:32)

Offline

#1365 2010-11-08 04:49:31

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: Post your handy self made command line utilities

Currently assigning commands to the function keys in ViM. Have this one liner as an alias for when I forget which command is bound to which key:

grep 'map <F[0-9]\{1,2\}>' ~/.vimrc

Offline

#1366 2010-11-08 22:29:15

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: Post your handy self made command line utilities

yet another random wallpaper setter. neutral wrote this impromptu as an experiment for another script using braced variable expansion. here's a little rundown of how it works..

1. execute (with no arguments or with desired image director{y:-ies})
2. finds image(s) within specified director{y:-ies} [via command line arg(s) else falls back to script variable FOTODIR]
3. generates a random integer until it's within boundaries of director{y:-ies}
4. uses first valid integer as selection value within director{y:-ies}
5. sets wallpaper to this value. done!

nothing special, like i said before, just an experiment. figured i'd update my ancient wallpaper script in the process to something halfway useful. if you decide to use this, you can specify custom commands within the script itself with XSETCMD and XSETARG. i guess i could've added command line options for this but i didn't want to overcomplicate a testing script. tongue

#!/bin/zsh
#################################################
# author: milomouse <vincent[at]fea.st>         #
# detail: set a random wallpaper upon execution #
#################################################
# depend: find + grep + ${XSETCMD} +~ xdpyinfo  #
#################################################
unsetopt nomatch

#################################################
# NOTE: KEEP VARIABLES INSIDE BRACES: var=(arg) #
#################################################

# find proper dimensions (optional):
_xy=(${$(xdpyinfo|grep dimensions)[2]})

# image setter commands:
XSETCMD=(display)
XSETARG=(-window root -resize ${_xy}!)

# base directory(s) for images:
FOTODIR=(~$USER/foto/wall ~$USER/foto/unix)

# allowed file types:
ALLOWED=(.png .jpg .jpeg .tiff .svg .bmp .gif)

############################################# {{{
# DO NOT EDIT UNLESS YOU KNOW WHAT YOU'RE DOING #
#################################################

[[ -n ${DISPLAY} ]] && (
[[ -x $(which ${XSETCMD}) ]] && (
[[ -n $@ ]] && FOTODIR=($@)

_alist=$(find ${FOTODIR} -type f 2>/dev/null \
  | grep -ie ${${(f)ALLOWED}// /\\|})
[[ -z $_alist ]] && exit 1
_count=${(fw)#_alist}

until [[ ${__} -le ${_count} && ${__} -gt 0 ]] {
  __=${RANDOM[1,${_count}]}
  foto=${${(fw)_alist}[$__]} }

${XSETCMD} ${XSETARG} ${foto} ))

############################################# }}}

Last edited by milomouse (2010-11-08 22:30:27)

Offline

#1367 2010-11-09 04:49:35

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

@mikesd
This would work as well

grep -E 'map <F[0-9]+>' $HOME/.vimrc

Last edited by steve___ (2010-11-09 04:50:30)

Offline

#1368 2010-11-09 14:29:38

valvet
Member
From: Denmark
Registered: 2009-06-06
Posts: 147

Re: Post your handy self made command line utilities

#!/bin/sh
unrar p -inul $1 | mplayer -

to-alias tongue

Offline

#1369 2010-11-10 17:50:01

M_ller
Member
Registered: 2010-04-17
Posts: 80
Website

Re: Post your handy self made command line utilities

I just needed a simple shell script to resize all the images in a folder to atleast 500 in width (unless if the image was higher than its width, it would resize the height to 500), and afterwards, create a thumbnail of 75x75.

Might be buggy, as this is my first shell script ever.

echo "Creates folder 'new' if not already made"
mkdir new
for i in *.JPG *.jpg
do
height="$(identify -format '%h' $i)"
width="$(identify -format '%w' $i)"

if (( "$(identify -format '%w' $i)" > "499" )); then
{
    if (( $height > $width )); then
    {
        echo "Height is larger than width on $i"
        echo "> Resize ($i)..."
        convert -resize x500 $i new/$i
    }
    else
    {
        echo "> Resize ($i)..."
        convert -resize 500 $i new/$i
    }
    fi
}
else
{
    echo "Image is too small to be resized"
}
fi

echo "Crop ($i)..."
convert -resize x75 $i temp_$i
convert -crop 75x75+0+0 temp_$i new/th_$i
rm temp_$i
done
echo "Finish"

Offline

#1370 2010-11-11 09:49:04

jlcordeiro
Member
From: Portugal
Registered: 2009-05-23
Posts: 76

Re: Post your handy self made command line utilities

I think you have a problem there. If you have an image with width=499, heigth=501 it wont be resized since it wont enter the first if condition.

Basically you have to change the first if to check for width OR height values being above 499. Also, since you saved width and height beforehand, why don't you use it in the first if? That's one less operation to be run (you do it right in the second if).


I'm guessing here, but don't you want to copy files with width<500 AND height<500 to the new folder? If you have a folder with 10 "big" and 10 "small" images, your script will place 20 thumbnails in new folder, but only 10 (resized) images.

Last edited by jlcordeiro (2010-11-11 09:52:57)

Offline

#1371 2010-11-11 10:27:36

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

valvet wrote:
#!/bin/sh
unrar p -inul $1 | mplayer -

to-alias tongue

I do this... which also marks seen movies/tv shows as watched for me:

#!/usr/bin/perl
# chmod, unpack, watch, delete
use strict;
use Cwd;
use File::Find;

my $mp = 'mplayer -msgcolor';
my $pwd = shift // getcwd();

#my $permdir  = 01742; # -rwxr---wT
my $permdir = 01700; # -rwx------T

chmod($permdir, $pwd);

my $tempdir;
$pwd =~ m/^((?:\/[^\/]+){2}\/).*/ and $tempdir = "$1.punpack-temp";

$tempdir = '/tmp/' if(@ARGV);

use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

File::Find::find({wanted  => \&wanted}, $pwd);

sub wanted {
  /^.+(?:part00)?\.rar/
  && doexec(0, "unrar x \"{}\" $tempdir/ && $mp $tempdir/* && rm -rv $tempdir/");
}

sub doexec ($@) {
    my $ok = shift;
    my @command = @_; # copy so we don't try to s/// aliases to constants
    for my $word (@command) {
      $word =~ s#{}#$name#g;
    }
    if ($ok) {
        my $old = select(STDOUT);
        $| = 1;
        print "@command";
        select($old);
        return 0 unless <STDIN> =~ /^y/;
    }
    chdir($pwd); #sigh
    system(@command);
    chdir $File::Find::dir;
    return !$?;
}

Offline

#1372 2010-11-11 11:16:39

vadmium
Member
Registered: 2010-11-02
Posts: 63

Re: Post your handy self made command line utilities

valvet wrote:
#!/bin/sh
unrar p -inul $1 | mplayer -

I used to do something similar. The problem is you can't seek in the file. Nowadays I use a Fuse program called rarfs, and this little script. No extraction necessary!

#! /bin/sh -eu
set -o errexit -o nounset
MNT="$HOME/mpwd"
mkdir -p "$MNT"

target="${1-.}"

# Workaround for not being able to fusermount from a fusermount directory
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=584541
target="$(readlink -f "$target")"
cd /

play_rar() {
    rarfs "$1" "$MNT"
    umount() {
        fusermount -u "$MNT"
    }
    trap umount EXIT
    mplayer "$MNT"/*
    trap - EXIT
    umount
}

if test ! -d "$target"; then
    play_rar "$target"
elif test "$(find "$target" -maxdepth 1 -name "*.part01.rar" | wc -l)" -ge 1
then
    play_rar "$target"/*.part01.rar
elif test ! -e "$target"/*.rar; then
    echo "$target: No file found" >&2
else
    play_rar "$target"/*.rar
fi

Offline

#1373 2010-11-11 15:50:01

M_ller
Member
Registered: 2010-04-17
Posts: 80
Website

Re: Post your handy self made command line utilities

jlcordeiro wrote:

I think you have a problem there. If you have an image with width=499, heigth=501 it wont be resized since it wont enter the first if condition.

Basically you have to change the first if to check for width OR height values being above 499. Also, since you saved width and height beforehand, why don't you use it in the first if? That's one less operation to be run (you do it right in the second if).


I'm guessing here, but don't you want to copy files with width<500 AND height<500 to the new folder? If you have a folder with 10 "big" and 10 "small" images, your script will place 20 thumbnails in new folder, but only 10 (resized) images.

Thanks for your comments.

The reason that I have not cared about if w=499 and h=501, is that I know that none of the images I use for the galleri, isn't that small. But yeah, you are right.

If I had images less than 500 w/h, I'd just do the job manually. But yes, my script needs a lot of work to be _really_ useable (: I just needed a simple script to do my current needs.

Offline

#1374 2010-11-12 11:47:17

valvet
Member
From: Denmark
Registered: 2009-06-06
Posts: 147

Re: Post your handy self made command line utilities

@dmz and @vadmium: That's great thanks :-). Good stuff! Much better.

Offline

#1375 2010-11-14 08:53:11

JezdziecBezNicka
Member
From: Cracow, Poland
Registered: 2009-12-03
Posts: 89

Re: Post your handy self made command line utilities

Since synclient lacks toggle functionality, and I wanted to be able to toggle my touchpad with a single keybinding, I wrote this:

#!/usr/bin/env ruby
touchpad_off = ( `synclient`.match(/TouchpadOff.*$/).to_s.match(/0|1/).to_s.to_i - 1 ).abs
system("synclient TouchpadOff=#{touchpad_off}")

in ruby. Hope that's useful to someone.

EDIT: I feel stupid, I should have looked into arch wiki before deciding to write that. There is a bash version already there.

Last edited by JezdziecBezNicka (2010-11-14 09:03:22)


This is my signature. If you want to read it, first you need to agree to the EULA.

Offline

Board footer

Powered by FluxBB