Archive for the ‘bash’ Category

dropbox: remove conflicted copies

January 3, 2015 Leave a comment

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?

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

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?

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


$ mediainfo movie.avi


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, Feb 16 201019:42:50

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

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

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

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.


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

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

shopt: change additional shell optional behavior

November 22, 2014 1 comment

There is a command called shopt that allows you to change additional shell optional behavior.

I use both Ubuntu and Manjaro and I noticed that sometimes bash behaved differently on Manjaro. For instance, I had a folder called “Test_me”. Once accidentally I wrote “cd test_me” and Manjaro entered the folder “Test_me” (notice the capital ‘T’) without any problem. Under Ubuntu it was impossible :)

As it turned out, the different behaviour is due to different shopt settings. In the example above, “cdspell” was on in Manjaro, while in Ubuntu it was off by default.

Since I use both systems and I want bash to behave similarly, I added these lines to the end of my ~/.bashrc:

# shopt settings (normalize Ubuntu and Manjaro)
shopt -s cdspell
shopt -s dotglob
shopt -s hostcomplete
shopt -s nocaseglob
shopt -u sourcepath

I switched “sourcepath” off. Read this post if you want to know why.

Categories: bash, manjaro, ubuntu Tags: ,

the command “source” doesn’t exactly work the way you thought

November 22, 2014 1 comment

We know that “source” is a bash shell built-in command that executes the content of the file passed as argument in the current shell. It has a synonym: ‘.‘ (dot).

The classical use case is “source .bashrc” in your HOME directory. You do it when you modify your ~/.bashrc file and you want to activate the changes in the current terminal.

TIL something new. I had a discussion about it at reddit and the user geirha gave such a clear answer that I would quote him:

“The . (source is an alternative name for . in bash) command behaves more or less the same as when executing a command.

In POSIX sh, the following two will search for a file named foo in PATH (only).

    . foo

while the following two will specifically run and source, respectively, the file named foo in the current directory (only)

    . ./foo

Bash differs slightly (when not running in posix mode) in that when you give . or source an argument without any slash (/) characters (e.g. source foo), it searches through the PATH variable, like POSIX sh, but then also checks the current directory, if it was not found in PATH.” (end of quote)

Because of this I ran into a strange problem. I had a file called “.venv” that I wanted to source (source .venv) but I got some error. If I renamed the file, it worked well. But I wanted to call it “.venv”. What’s wrong?

As explained above, it turned out that “source” started to look for “.venv” in the PATH first. Somehow it found a different file with the very same name and that caused the problem. When I renamed the file to “not_hidden” or “.jabba”, it worked.

Solution #1
The trivial way is to use “./” by telling source to take the argument from the current directory:

source ./.venv

However, in my entire life I thought that “source .bashrc” takes the file .bashrc from the current directory. It should, in my opinion :)

Solution #2
If you also think that “source” shouldn’t do anything with the PATH variable, there is a cure. This feature can be switched off. The command “shopt” is a builtin that allows you to change additional shell optional behavior.

The guilty option is this one:


    If set, the source builtin uses the value of PATH to find 
    the directory containing the file supplied as an argument. 
    This option is enabled by default.

Simply run “shopt” to see if “sourcepath” is on for you. You can switch it off easily:

shopt -u sourcepath

To make it permanent, add this line to the end of your ~/.bashrc.

After all this ado, I could finally write this:

. .venv

And this is the beauty of Linux. You learn something new every day :)


Categories: bash Tags: , , , ,

Get every new post delivered to your Inbox.

Join 76 other followers