Archive for the ‘bash’ Category

monitor the CPU/memory usage of one single process

November 23, 2015 Leave a comment

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


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

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

Tip from here.

Categories: bash Tags: , , , ,

convert a LibreOffice Calc/Excel stylesheet to .csv in command-line

November 21, 2015 Leave a comment

I have a stylesheet made with LibreOffice Calc / Excel. The extension of the file is .ods or .xls / .xlsx. How to convert it to .csv? From the command line, of course.

Convert an .ods file:

libreoffice --headless --convert-to csv file.ods

Convert an Excel file:

libreoffice --headless --convert-to csv file.xlsx

Tip from here.

If you use --convert-to html, then you get an HTML output.

Categories: bash Tags: , , ,

cut a column where the delimiters are whitespaces

October 26, 2015 Leave a comment

You have a text where the delimiter between the columns is just one character. In this case you can use the “cut” command:

$ cat /etc/passwd | head -2

$ cat /etc/passwd | head -2 | cut -d: -f1

However, what to do if the delimiters are whitespaces (notice the plural form)? Take for instance the output of “ps ux“. How to cut the 2nd column with the PIDs?

Use the right tool. In this case, “awk” will rescue us:

$ ps ux | tail -2
jabba    28302  0.1  0.0 136216  6436 ?        S    14:20   0:09 /usr/lib/virtualbox/VBoxXPCOMIPCD
jabba    28307  0.2  0.1 785908 10568 ?        Sl   14:20   0:22 /usr/lib/virtualbox/VBoxSVC --auto-shutdown

$ ps ux | tail -2 | awk '{print $2}'
Categories: bash Tags: , ,

feh: a fast and light image viewer

October 18, 2015 Leave a comment

feh is an X11 image viewer aimed mostly at console users. Unlike most other viewers, it does not have a fancy GUI, but simply displays images. It is controlled via commandline arguments and configurable key/mouse actions.” (source)

feh has lots of features, thus it has lots of command line options too. Here I sum up the most important options. For more info refer to the manual.

feh [options] [files | directories | URLs ...]

command line options
-F             fullscreen
-x             borderless window
-Z             auto zoom to fit window size
--draw-exif    display some EXIF info
-d             display filename
-Y             hide mouse pointer
-l             don't show images, just list their dimensions
-z             randomize
-D float       delay for slideshow
-t             show thumbnails
    scrolling: mouse or 
               Ctrl+Up/Down (soft scroll) or 
               Alt+Up/Down (bigger jumps)
-k             keep files in /tmp (useful when browsing URLs)
               (Note: it didn't seem to work for me).

commands in view mode
d           toggle filename
e           toggle exif
h           toggle pause (in slideshow mode)
m           show menu
q, ESC      quit
s           save image
v           toggle fullscreen
z           random jump
Ctrl+Del    delete file from file system
keypad +    zoom in
keypad -    zoom out
Ctrl+Up/Down/Left/Right    scroll
*           zoom 100%
/           zoom to fit window size
o           toggle mouse pointer
Home / End  jump to first / last image
PgDn        go 5% forward in file list
PgUp        go 5% backward in file list

$ feh
    browse images in the current directory
$ feh -Fd
    fullscreen, file info
$ feh -Fdz
    fullscreen, file info, random order
$ feh -Fdz -D 5
    fullscreen, file info, random order, slideshow with 5 sec. delay
$ feh -t
    show thumbnails
$ feh img.jpg
    Show this image only. Browsing other files is not possible.
$ feh . --start-at ./img.jpg
    Show img.jpg but allow browsing the other images too.
    You must write "./img.jpg" instead of "img.jpg"!
$ feh . --start-at ./img.jpg -Fd
    as before + fullscreen and file info
$ feh . --start-at ./img.jpg -FdZ
    as before + images are auto-zoomed to the window size

At the Arch Wiki you can find more info about feh. There is also a file browser image launcher script.

I also made a simple script:

#!/usr/bin/env bash


if [[ ! -f $1 ]]; then
    echo "$0: first argument is not a file" >&2
    exit 1

feh . --start-at "./$1" -Fd

Usage: “ cool.jpg“. It will open the specified image in fullscreen. File info is displayed and you can browse other images too.

Categories: bash Tags: , ,

stop frustration: navigate among directories in command line easily

October 18, 2015 2 comments

Doing file manipulations in plain command line is a pain. That’s why I use Midnight Commander for tasks like copying, deleting, etc. But I’m still in pain :) If there are more than two directories that I want to work with, then even in MC I need to browse a lot and pass time with changing directories. Help!

I started to use two utilities that are very helpful. I found both of them in this thread.

This script was originally published in a Linux Gazette article in 2004. It’s available here.

Simply download the script and add these lines to your ~/.bashrc:

source ~/bin/
alias l="cd --"

“This is a scripts which defines a CD replacement function in order to keep, display and access history of visited directories. Normally the script will be sourced at the end of .bashrc.

cd —

Shows the history list of visited directories. The list shows the most recently visited names on the top.

This command is also assigned to ctrl+w.

cd -NUM

Changes the current directory with the one at position NUM in the history list. The directory is also moved from this position to the top of the list.” (source)

Since we need to use the command “cd --” a lot, I put a shorter alias on it called “l“, which stands for “list”.

This script is great but it has a drawback. It’s local in your current terminal. So if you open a new terminal and call the “cd --” command, the list is empty. I also wanted a global solution, that maintains a list among several terminal windows too. And this is how I found:

(2) autojump
Here is a nice blog post about it that I don’t want to quote completely just its abstract:

“autojump is a self-learning directory changer for navigating your filesystem. It works by maintaining a database of the directories you use the most from the command line. As soon as it’s installed it’ll start saving you time. Making easy to switch to the directories that are most important to you.” (source)

Install the package “autojump” and add these lines to the end of your ~/.bashrc:

# autojump
source /usr/share/autojump/autojump.bash

The official wiki is here.

Good to know:

  • To see the content of its database, use the command “j -s” (or “j --stat“).
  • TAB-completion is supported.

Remove “conflicted copy” files in your Dropbox folder

October 13, 2015 Leave a comment

Here is a quick solution to remove conflicted copies:

#!/usr/bin/env bash

# just print
find . | grep "conflicted copy 2015" | while read i; do echo $i; done

# print and DELETE
#find . | grep "conflicted copy 2015" | while read i; do echo $i; /bin/rm "$i"; done

Put it in your ~/Dropbox folder and launch it. First it will just print the conflicted copies. If you want to get rid of these files (verify!), then uncomment the last line.

The string “2015” is added to narrow the produced list. But normally “conflicted copy” should be enough.

Categories: bash Tags: ,

dropbox: command-line interface

September 12, 2015 Leave a comment

I wanted to test the status of my Dropbox client from the terminal. Actually, I wanted to write a script that executes an action when my Dropbox folder is fully synced. So I wanted to test the status if it’s “working” or “synced”.

I found the solution here. It turned out that Dropbox has an official command-line script that can do this and much more. First, get it:

wget -O ~/
chmod u+x ~/
~/ status

This is a Python script, written in Python 2, thus I modified the first line to be “#!/usr/bin/env python2“.

This script can do several things for you:

Dropbox command-line interface


Note: use dropbox help  to view usage for a specific command.

 status       get current status of the dropboxd
 help         provide help
 puburl       get public url of a file in your dropbox
 stop         stop dropboxd
 running      return whether dropbox is running
 start        start dropboxd
 filestatus   get current sync status of one or more files
 ls           list directory contents with current sync status
 autostart    automatically start dropbox at login
 exclude      ignores/excludes a directory from syncing
 lansync      enables or disables LAN sync

Some years ago I wrote a simple script to get the public URL of a file in my Dropbox folder. This script can do that too with the “puburl” command.

Categories: bash, python Tags: , ,

Get every new post delivered to your Inbox.

Join 85 other followers