Archive

Archive for the ‘bash’ Category

open the github page for the current git repository in your browser

February 4, 2016 Leave a comment

Problem
You have a GitHub project and you are in its local folder in bash. How to open the project’s GitHub page with one command?

Solution
I found a bash function for this (credits are at the top in comments):

# Opens the github page for the current git repository in your browser
# git@github.com:jasonneylon/dotfiles.git
# https://github.com/jasonneylon/dotfiles/
function gh() {
  giturl=$(git config --get remote.origin.url)
  if [ "$giturl" == "" ]
    then
     echo "Not a git repository or no remote.origin.url set"
     exit 1;
  fi

  giturl=${giturl/git\@github\.com\:/https://github.com/}
  giturl=${giturl/\.git/\/tree/}
  branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
  branch="(unnamed branch)"     # detached HEAD
  branch=${branch##refs/heads/}
  giturl=${giturl}${branch}
  echo "#" $giturl
  xdg-open $giturl
}

Just add it to the end of your ~/.bashrc file, open a new terminal (or source it) and use the “command” gh in your shell.

For more info on xdg-open see this post.

Categories: bash Tags:

open a web page from a bash script

February 4, 2016 Leave a comment

Problem
How to open a web page from a bash script?

Solution
Under Manjaro:

$ xdg-open http://your/url

However, it opened Chrome by default while I’m a Firefox believer. Here is the remedy:

$ xdg-settings set default-web-browser firefox.desktop
$ xdg-open http://your/url

Now it works as it is supposed to work.

Categories: bash, firefox Tags:

resize an .mkv file

December 5, 2015 Leave a comment

Problem
I wanted to play a 720p .mkv file on an old laptop but it was painfully slow. How to reduce the quality of an .mkv file?

Solution
I found the solution here. The following worked for me:

$ ffmpeg -i Movie.mkv -vf scale=-1:360 -c:v libx264 -crf 18 -preset veryslow -c:a copy MyMovie_360p.mkv

Visit the link above for an explanation of the parameters.

Categories: bash Tags: , , , ,

open PDF at a given page

December 3, 2015 Leave a comment

Problem
From the command line you want to open a PDF file at a given page.

Solution
My favourite PDF viewer was okular but I didn’t find this option. But evince supports it:

evince -p 200 ehh.pdf

Where “-p” means “page”.

Categories: bash Tags: , , ,

battle test your server with siege

December 1, 2015 Leave a comment

Siege is a multi-threaded http load testing and benchmarking utility. It was designed to let web developers measure the performance of their code under duress. It allows one to hit a web server with a configurable number of concurrent simulated users. … Performance measures include elapsed time, total data transferred, server response time, its transaction rate, its throughput, its concurrency and the number of times it returned OK.” (source)

Here is a gist that shows how to use it.

Warning! Use siege only on your sites since it can be interpreted as a DDOS attack.

Categories: bash, Uncategorized Tags: , ,

md5sum, sha1sum

November 29, 2015 Leave a comment

Problem
You always forget how to use the commands md5sum and sha1sum.

Reminder

bash$ ls -al
-rw-r--r--  1 jabba users 351 Nov 29 14:55 file.iso
=====================================================
bash$ md5sum file.iso
ca9779174955bbaaa6652bb0844ed64f  file.iso 
=====================================================
bash$ md5sum file.iso >na.md5
=====================================================
bash$ md5sum -c na.md5
file.iso: OK
=====================================================
bash$ sha1sum file.iso
76004a25fc2ba41cb805f010a0f66f41e023692d  file.iso
=====================================================
bash$ sha1sum file.iso >na.sha1
=====================================================
bash$ sha1sum -c na.sha1
file.iso: OK
=====================================================
bash$ python2
>>> len("ca9779174955bbaaa6652bb0844ed64f")
32
>>> len("ca9779174955bbaaa6652bb0844ed64f")*4
128 
>>> len("76004a25fc2ba41cb805f010a0f66f41e023692d")
40
>>> len("76004a25fc2ba41cb805f010a0f66f41e023692d")*4
160

MD5 is 128-bit, SHA1 is 160-bit. sha1sum has some stronger variations: sha224sum, sha256sum, sha384sum and sha512sum.

Categories: bash, security Tags: ,

monitor the CPU/memory usage of one single process

November 23, 2015 Leave a comment

Problem
I want to monitor a specific process. How much CPU does it use? How much memory does it eat?

Solution

$ top -p PID
# or
$ htop -p PID

The option “-p” stands for “pid’.

Tip from here.

Categories: bash Tags: , , , ,
Follow

Get every new post delivered to your Inbox.

Join 85 other followers