You are not logged in.

#2876 2017-01-20 14:54:19

Drehstromlampe
Member
From: GER
Registered: 2015-07-18
Posts: 45

Re: Post your handy self made command line utilities

When you need to know what Wifis are in range and what there signal strength is and you don't want to go through tons of information from iw

(needs su privileges)

#/bin/bash

iw dev <insert device> scan | grep -E 'DS Parameter set|SSID|BSS|signal'

Last edited by Drehstromlampe (2017-01-20 14:54:57)

Offline

#2877 2017-01-20 14:54:20

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Post your handy self made command line utilities

[Edit: in response to Alad's dependency thing]
And you might want to make that first one

expac -Q '%n\n%S' -l '\n'

to handle "provides".

Last edited by Raynman (2017-01-20 14:55:02)

Offline

#2878 2017-01-20 14:59:12

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

Re: Post your handy self made command line utilities

Drehstromlampe, I ditched iw and wireless_tools some time ago when I realized I needed wpa_supplicant regardless, and wpa_supplicant's wpa_cli acheived all the same goals anyways.  It's nice to be able to use one tool for *all* wireless networks, instead of having to switch tools when on and open vs secure network.  And wpa_cli has a nice clean output by default:

wpa_cli -i <interface> scan && wpa_cli -i <interface> scan_result

I have that aliased for when I'm out and about and some employee behind the cafe bar mumbles the name of their network without specifying anything about punctuation, capitalization, or spaces.


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

Offline

#2879 2017-01-20 15:26:13

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities

Trilby wrote:

Note that piping within awk is generally slower than piping it's output.  I've confirmed that in this case.  Also piping outside awk seems far more readable to me.

Raynman wrote:

[Edit: in response to Alad's dependency thing]
And you might want to make that first one

expac -Q '%n\n%S' -l '\n'

to handle "provides".

I've made some edits. Thanks.


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#2880 2017-01-22 22:22:03

yanthor
Member
Registered: 2015-03-22
Posts: 14

Re: Post your handy self made command line utilities

Highlight function for bash. Source it from .bashrc or .bash_aliases or wherever you like.

hl() {
  # usage: cat file | hl <pattern>
  #
  # man 1 grep
  GREP_COLORS="ms=01;36:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36" egrep --color "^|$1"
}

Offline

#2881 2017-01-23 00:07:48

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

Useless Use Of Cat award. ^ Really, just make that an alias for `GREP_COLORS="ms=01;36:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36" egrep --color`

Also, I use ag for that, which is faster than grep and automatically highlights anyway.

Last edited by eschwartz (2017-01-23 00:12:11)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#2882 2017-01-23 00:22:29

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

Re: Post your handy self made command line utilities

Why alias the whole variable?  Just set the variable like other environment variables.  If you want you can alias grep to `grep --color`.


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

Offline

#2883 2017-01-23 00:43:54

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

Maybe it's for special occasions, like birthdays or first dates.

Last edited by Awebb (2017-01-23 00:57:05)

Offline

#2884 2017-01-23 00:46:30

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

Well, maybe there is some sort of inscrutable reason to prefer that only in this specific invocation should the default colors not be used. The fact that the variable is not already set would imply that yanthor is not using it elsewhere, but what do I know?

(Anyway, exporting an environment variable mildly bloats the environment of all processes, and incurs some sort of miniscule startup penalty, so...)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#2885 2017-01-23 00:59:11

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

It's two in the morning, but am I correct in the assumption, that changing a single "1" into an "@" might chase the cat away?

Offline

#2886 2017-01-23 20:26:26

dmerej
Member
From: Paris
Registered: 2016-04-09
Posts: 101
Website

Re: Post your handy self made command line utilities

Just to show how you can 'customize' git, assuming ~/bin is in your PATH:

# ~/bin/git-commit-and-push
#!/bin/env python

import sys
import subprocess

def main():
    message = sys.argv[1]
    subprocess.check_call(["git", "commit", "--all",
                           "--message", message])
    subprocess.check_call(["git", "push"])

if __name__ == "__main__":
    main()

And then

# in ~/.config/git/config
alias cp = git-commit-and-push

Then use git cp "this is the message" to commit everything and push in one go.

I have a few other git commands written this way in my dotfiles repo


Responsible Coder, Python Fan, Rust enthusiast

Offline

#2887 2017-01-23 20:38:52

yanthor
Member
Registered: 2015-03-22
Posts: 14

Re: Post your handy self made command line utilities

Hey folks, thanks for the replies. smile
The hl() is result of my effort to use $1 into an alias, which turns out is impossible. I can't recall now, it was long time ago, I want to see all the output, not cutting(grep) any lines, and only mark what i needed to.

Just found about GREP_COLORS minutes before posting here. If one likes - can customize it right there.

$@ will be "expanded" "into" "words". Using $1 like pattern1\|pattern2\|or\ more works better for me.

Cheers!

Offline

#2888 2017-01-24 07:03:08

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

dmerej wrote:

Just to show how you can 'customize' git, assuming ~/bin is in your PATH:

# ~/bin/git-commit-and-push
#!/bin/env python

import sys
import subprocess

def main():
    message = sys.argv[1]
    subprocess.check_call(["git", "commit", "--all",
                           "--message", message])
    subprocess.check_call(["git", "push"])

if __name__ == "__main__":
    main()

And then

# in ~/.config/git/config
alias cp = git-commit-and-push

Then use git cp "this is the message" to commit everything and push in one go.

I have a few other git commands written this way in my dotfiles repo

This is a really bad example, since it is simple enough to be written as a git alias and in any event you just required loading python.

Looking at your other scripts, most of them could really be aliases (git aliases can even invoke the shell if needed).
git-cco appears to essentially do exactly what git natively does, but in python and assuming remotes can only ever be called "origin".
I cannot think why one would want to commit-and-rebase, but -a -v --no-verify seems outside the name of that command and it should still be an alias.
push-all-and-fetch seems to push the master branch to a remote named "all" which is oddly specific, then does an overly-verbose "git remote update", which should never be necessary unless someone else is pushing from elsewhere (and you didn't correctly push all local branches).
fpush duplicates the builtin git error reporting, then encourages useless commit messages. Please just use `git commit "$file" -m "" --allow-empty-message && git push` instead. And maybe skip the automatic pushes.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#2889 2017-01-24 07:10:37

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

yanthor wrote:

Hey folks, thanks for the replies. smile
The hl() is result of my effort to use $1 into an alias, which turns out is impossible. I can't recall now, it was long time ago, I want to see all the output, not cutting(grep) any lines, and only mark what i needed to.

Just found about GREP_COLORS minutes before posting here. If one likes - can customize it right there.

$@ will be "expanded" "into" "words". Using $1 like pattern1\|pattern2\|or\ more works better for me.

Cheers!

... and aliases can't exclude the space between the expansion and the following parameters, I see what you mean.
BTW, using $1 instead of $@ simply drops the second and later words, the point of using $@ is would be to pass the file to grep and thereby avoid cats.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#2890 2017-01-25 01:22:18

Imadch
Member
Registered: 2017-01-25
Posts: 5

Re: Post your handy self made command line utilities

Hi!
this is my first post here , am new to arch linux and i hope i will be welcomed here smile

i wrote a simple script to organize my files

#!/usr/bin/python


#*******************script for organizing files *********************#

import os 
import shutil
import sys 

############ public variables ###################################



if (len(sys.argv) == 2):
	list_of_files = os.listdir(sys.argv[1])
	os.chdir(sys.argv[1])
else:
	list_of_files =  os.listdir(os.getcwd()) #getting the working directory's list of files 
list_of_ext = ['.txt', '.pdf', '.jpg'] # add more!

###########functions bodies for the processing #################

#checking if the direrctory exists, if not then making it, then moving the file 
# to the directory
def move_file(file_name,dir_name):
	if (not os.path.exists(dir_name)):
		os.mkdir(dir_name)
	shutil.move(file_name, dir_name)
	

#checking the extension then returns the name of the directory so it will be as
# an arg to the #move_file function 
def get_dir_name(ext):
	if (ext == '.txt'):
		return 'text_files'
	elif (ext == '.pdf'):
		return 'pdf_docs'
	elif (ext == '.jpg'):
		return 'jpg_images'
	else:
		return 0 
		
		
#combining the two functions #move_file and #get_dir_name together 
def check_and_move(file_list):
	for file_name in file_list :
		for ext in list_of_ext :
			if (os.path.isfile(file_name)):
				if (os.path.splitext(file_name)[1] == ext):
					print "%s is a %s file" % (file_name, ext)
					move_file(file_name, get_dir_name(ext))
			
	
	
	


###########checking and processing##################



check_and_move(list_of_files)
	

Last edited by Imadch (2017-01-25 01:25:15)

Offline

#2891 2017-01-25 01:41:10

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

Re: Post your handy self made command line utilities

Welcome Imadch.  That seems an odd way to organize files to me, but if it works for you, great.  I suspect the code would be much cleaner and more efficient if you used an associative array of extensions -> directories.  For each extension glob for all files of that type, then move those to the associated directory instead of getting *all* files and checking each on with string functions.  As an added bonus, this associative array could be moved to a config file that is sourced so your code would be much easier to extend: just add a new entry to the array without having to rewrite the get_dir_name function.

I know all this is quite doable in python, but I'm crap at python, so here it is in bash:

#!/bin/bash

declare -A dir=(
   [txt]="text_files"
   [pdf]="pdf_docs"
   [jpg]="jpg_images"
)

[[ -n $1 ]] && cd $1
for ext in "${!dir[@]}"; do
   mkdir -p ${dir[$ext]}
   mv *.$ext ${dir[$ext]}
done

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

Offline

#2892 2017-01-25 02:21:26

Imadch
Member
Registered: 2017-01-25
Posts: 5

Re: Post your handy self made command line utilities

Thank you so much for the replay and the advice  , you made it so simple , i will use your script until i make mine works better tongue

this is a simple try, i don't know if it will work i didn't try it

import os 
import shutil
import sys 

if (len(sys.argv) == 2):
     if (os.path.exists(sys.argv[1])):
	     list_of_files = os.listdir(sys.argv[1])
	     os.chdir(sys.argv[1])
else:
	list_of_files =  os.listdir(os.getcwd())
	
	
dirs = {".txt":"text_files", ".pdf":"pdf_docs", ".jpg":"jpg_images" }
for file_name in list_of_files :
    for ext, dir_name in dirs.items() :
         os.mkdir(dir_name)
         if (os.path.splitext(file_name)[1] == ext):
               shutil.move(file_name, dir_name)

Offline

#2893 2017-01-25 05:28:56

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Post your handy self made command line utilities

os.chdir is kind of dirty, you should usually prefer to use absolute paths in python.
You also have no error handling for the case where dir_name already exists, which is not unlikely (mkdir -p in the shell), and treat files as though they are directories (both of which are a regression from your original script).

An alternative which uses os.walk to recurse into subdirectories:

import os 
import shutil
import sys 

EXTENSION_DIRS = {".txt":"text_files", ".pdf":"pdf_docs", ".jpg":"jpg_images" }

# Discard extra args. We assume the shell has done expanduser for us.
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
    workdir = sys.argv[1]
else:
    workdir = '.'

for root, dirs, files in os.walk(workdir):
    for file in files:
        ext = os.path.splitext(file)[1]
        if ext in EXTENSION_DIRS:
            destdir = os.path.join(workdir, EXTENSION_DIRS.get(ext))
            os.makedirs(destdir, exist_ok=True)
            shutil.move(os.path.join(root, file), destdir)

Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#2894 2017-01-25 10:24:49

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,437

Re: Post your handy self made command line utilities

This is bascially an open-url-with script. Newer version of lariza (at present in dev-external-handler branch) has feature to open url with external script, and the script is invoked with "lariza-external-handler -u $url". So I have created this script named lariza-external-handler. When invoked directly it opens url from clipboard, and when invoked by lariza it works with url forwarded by lariza.

#!/bin/bash

#which clipboard to use, valid options are "primary" and "clipboard"
SELECTION=clipboard
#choose your terminal
TERM=sakura
shopt -s lastpipe

ops=(
    w3m
    mpv
    MPV
    youtube-dl
    YOUTUBE_DL
    transmission
    curl-download
    curl-save
    wget-saveall
    add-bookmark
)

getopts u: name
case $name in
u) url=$OPTARG ;;
*) url="$(xclip -o -selection $SELECTION)" ;;
esac

[[ ! $url ]] && exit
# to use with rofi ##
option=$(for i in "${ops[@]}"; do echo "$i"; done |  rofi -dmenu -location 1 -l 10 -width 100 -p : -font "Inconsolata 14" -mesg "open <u>$url</u> with...")
## uncomment below line to use with dmenu , and comment out line before this ##
# option=$(for i in "${ops[@]}"; do echo "$i"; done | /usr/bin/dmenu -b )

[[ ! $option ]] && exit

case "$option" in

w3m) $TERM -e w3m "$url" ;;
mpv) $TERM -e w3mpv "$url" ;;
MPV) $TERM -e mpv "$url"  ;;
youtube-dl) $TERM -e w3ytdl "$url" ;;
YOUTUBE-DL) $TERM -e youtube-dl "$url" ;;
## download torrent using transmission-cli
transmission) $TERM -e transmission-remote-cli "$url" ;;
## downlod with curl in current dir (home)
curl-download) $TERM -e curl -L -O --user-agent Mozilla/5.0 "$url" ;;
## save page as curl.html in Downloads
curl-save) $TERM -e curl -L -o ~/Downloads/curl.html --user-agent Mozilla/5.0 "$url" ;;
## save full page with wget, saved in Downloads
wget-saveall) $TERM -e wget --user-agent=Mozilla/5.0 --mirror -p --convert-links -P ~/Downloads "$url" ;;
# add bookmark
add-bookmark) 
	    echo -e "news\nlinux\ncycling\nsocial\nshopping\ntravel" | rofi -dmenu -location 1 -l 6 -width 100 -p : -font "Inconsolata 14" -mesg "choose tag for bookmark" | read tag
	    ## uncomment below for use with dmenu and comment out the upper line ##
	    # echo -e "news\nlinux\ncycling\nsocial\nshopping\ntravel" | /usr/bin/dmenu -fn "Inconsolata 14" -p "choose tag" | read tag
	    echo "$url $tag" >> ~/.config/lariza/bookmarks ;; 
## use any other external program chromium or firefox etc.
*) $option "$url" ;;
esac 

note: w3mpv and w3ytdl are mpv and youtube-dl configured to choose smaller size (low resolution vids)

Offline

#2895 2017-01-25 11:58:43

Imadch
Member
Registered: 2017-01-25
Posts: 5

Re: Post your handy self made command line utilities

Eschwartz wrote:

os.chdir is kind of dirty, you should usually prefer to use absolute paths in python.
You also have no error handling for the case where dir_name already exists, which is not unlikely (mkdir -p in the shell), and treat files as though they are directories (both of which are a regression from your original script).

An alternative which uses os.walk to recurse into subdirectories:

import os 
import shutil
import sys 

EXTENSION_DIRS = {".txt":"text_files", ".pdf":"pdf_docs", ".jpg":"jpg_images" }

# Discard extra args. We assume the shell has done expanduser for us.
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
    workdir = sys.argv[1]
else:
    workdir = '.'

for root, dirs, files in os.walk(workdir):
    for file in files:
        ext = os.path.splitext(file)[1]
        if ext in EXTENSION_DIRS:
            destdir = os.path.join(workdir, EXTENSION_DIRS.get(ext))
            os.makedirs(destdir, exist_ok=True)
            shutil.move(os.path.join(root, file), destdir)

thank you for the help , i couldn't find a way for sub directories before, now the script is more helpful for me :)

Offline

#2896 2017-01-25 12:32:57

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

Trilby wrote:

I know all this is quite doable in python, but I'm crap at python, so here it is in bash

Nice! You just taught me "declare" in bash.

Offline

#2897 2017-01-25 15:04:42

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities

Awebb wrote:
Trilby wrote:

I know all this is quite doable in python, but I'm crap at python, so here it is in bash

Nice! You just taught me "declare" in bash.

https://bbs.archlinux.org/viewtopic.php … 8#p1603478

tongue


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#2898 2017-01-25 15:46:15

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

Alad wrote:
Awebb wrote:
Trilby wrote:

I know all this is quite doable in python, but I'm crap at python, so here it is in bash

Nice! You just taught me "declare" in bash.

https://bbs.archlinux.org/viewtopic.php … 8#p1603478

tongue

Woah! I'm speechless. How could I forget something so useful twice?

Offline

#2899 2017-01-26 13:07:37

bleach
Member
Registered: 2013-07-26
Posts: 264

Re: Post your handy self made command line utilities

scripts to mount and unmount  a specific removable drive and bind it to specific folders this drive is inserted and removed regularly.


mount

#!/bin/sh
FILE="/dev/mapper/it"

if [ -e $FILE &> "/dev/null" ]; then
echo "already mounted";
exit 1
else
sudo cryptsetup --type luks open UUID=xxx it;
sudo mount -t filesystem /dev/mapper/it /mnt/it;
sudo mount --bind /here /here;
sudo mount --bind  /here /here;
echo "succefully mounted"
exit 0
fi

umount

#!/bin/sh
FILE="/dev/mapper/it"

if pgrep process &> "/dev/null"; then
pkill proccess;(a specific thing that might be open but not background)
sudo umount "/here";
sudo umount "/here";
echo "successfully unmounted"
exit 0
elif [ -e $FILE &> "/dev/null" ]; then
sudo umount "/here";
sudo umount "/here";
echo "successfully unmounted"
exit 0
else
echo "not mounted";
exit 1
fi

edited some syntax

Last edited by bleach (2017-01-26 17:14:58)

Offline

#2900 2017-01-26 15:39:15

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: Post your handy self made command line utilities


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

Board footer

Powered by FluxBB