Archive

Archive for the ‘bash’ Category

edit crontab with emacs

March 8, 2015 Leave a comment

Problem
You can edit your crontab file with “crontab -e“. It worked fine as long as I used vim. However, when I switched to emacs and set the EDITOR environment variable to point to emacs, I couldn’t edit my crontab file normally. Emacs is started in the background and somehow it caused problems.

Solution
I made a script for editing my crontab file. This script sets EDITOR back to vim, thus “crontab -e” opens vim. Here is the script:

#!/usr/bin/env bash

# crontab_e

EDITOR=/usr/bin/vim
crontab -e

I call this script “crontab_e”, so it’s easy to remember its name.

Categories: bash Tags: , ,

Which process opened a file?

March 1, 2015 Leave a comment

Ever wondered who (which process) opened a given file? There is a Unix command to answer this question called lsof (“list open files”). Read more about it at wikipedia.

Demo
Execute the following script:

#!/usr/bin/env python3

# who.py

def main():
    f = open("/tmp/who.txt", "w")
    input("Press ENTER to continue...")
    f.close()

#####

if __name__ == "__main__":
    main()

Make it executable and run it. In another terminal issue the command “lsof /tmp/who.txt“. You’ll get a similar output:

$ cd /tmp

$ lsof who.txt 
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 1288  jabba    3w   REG   0,32        0   27 who.txt

$ ps 1288
  PID TTY      STAT   TIME COMMAND
 1288 pts/0    S+     0:00 python3 ./who.py

The file who.txt was opened by the process with PID 1288, which is the who.py script.

Categories: bash Tags: , , ,

dropbox: remove conflicted copies

January 3, 2015 Leave a comment

Problem
If you have Dropbox installed on several machines, sometimes one of the clients litter your Dropbox folder with conflicted copies. Their number can be up to hundred(s). How to get rid of them?

Solution
First, locate the conflicted copies:

$ cd ~/Dropbox  # or wherever your Dropbox folder is
$ find . | grep "conflicted copy"

Investigate the result and make sure they can be deleted.

# one more check:
$ find . | grep "conflicted copy" | while read i; do echo $i; done

# if you are absolutely sure, delete them:
$ find . | grep "conflicted copy" | while read i; do echo $i; \rm "$i"; done

Notice that “$i” is between quotes since these filenames contain spaces.

Categories: bash Tags: ,

media info

December 28, 2014 Leave a comment

Problem
You have a media file (audio, video, image) and you want to get some detailed information about it: duration, bit rate, resolution, etc. How to extract these pieces of information?

Solution
Use the MediaInfo project. You can install it via your package manager and you will get a command called “mediainfo”.

Example:

$ mediainfo movie.avi

Output:

General
Complete name                            : movie.avi
Format                                   : AVI
Format/Info                              : Audio Video Interleave
Format profile                           : OpenDML
Format settings                          : rec
File size                                : 1.27 GiB
Duration                                 : 2h 29mn
Overall bit rate                         : 1 218 Kbps
Writing application                      : AVI-Mux GUI 1.17.8.3, Feb 16 201019:42:50

Video
ID                                       : 0
Format                                   : MPEG-4 Visual
Format profile                           : Advanced Simple@L5
Format settings, BVOP                    : 2
Format settings, QPel                    : No
Format settings, GMC                     : No warppoints
Format settings, Matrix                  : Default (H.263)
Muxing mode                              : Packed bitstream
Codec ID                                 : XVID
Codec ID/Hint                            : XviD
Duration                                 : 2h 29mn
Bit rate                                 : 1 086 Kbps
Width                                    : 720 pixels
Height                                   : 304 pixels
Display aspect ratio                     : 2.35:1
Frame rate                               : 23.976 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Compression mode                         : Lossy
Bits/(Pixel*Frame)                       : 0.207
Stream size                              : 1.13 GiB (89%)
Writing library                          : XviD 64

Audio
ID                                       : 1
Format                                   : MPEG Audio
Format version                           : Version 1
Format profile                           : Layer 3
Mode                                     : Joint stereo
Mode extension                           : MS Stereo
Codec ID                                 : 55
Codec ID/Hint                            : MP3
Duration                                 : 2h 29mn
Bit rate mode                            : Constant
Bit rate                                 : 128 Kbps
Channel(s)                               : 2 channels
Sampling rate                            : 48.0 KHz
Compression mode                         : Lossy
Stream size                              : 136 MiB (11%)
Alignment                                : Aligned on interleaves
Interleave, duration                     : 747 ms (17.92 video frames)
Interleave, preload duration             : 216 ms
Title                                    : mp3
Writing library                          : LAME3.99r
Encoding settings                        : -m j -V 4 -q 2 -lowpass 17 -b 128

Python
There is a simple Python wrapper for it called pymediainfo. Its github page is here.

Categories: bash, python Tags: , , ,

convert .txt to .pdf

December 15, 2014 Leave a comment
pandoc input.txt -o output.pdf

By the way, pandoc can convert from / to lots of file formats.

Categories: bash Tags: , ,

shuffle the lines of a file

December 15, 2014 Leave a comment
cat file.txt | shuf

For more alternatives, see the comments at the bottom of this page.

Categories: bash Tags: ,

print content of a file from line X to line Y

December 10, 2014 Leave a comment

Problem
You have a long text file and you want to process it parts by parts. For instance, you want to work with the first 50 lines, then with lines 51-100, etc.

Solution

cat file.txt | sed -n '1,50p'

# or: sed -n '1,50p' file.txt
Categories: bash Tags: ,
Follow

Get every new post delivered to your Inbox.

Join 78 other followers