Useful Linux Commands Series - Part B

I haven't posted anything since October 23rd and I kind of feel uncomfortable with myself, because it's the first time I'm not typical with my obligations; well, what do you know, people change indeed.

Anyhow, I will continue with the series and hopefully in the near future I will be more reliable with time scheduling.

How to convert a flash video file to an MP3

For converting a flash video you can do so with various tools under GNU / Linux; one of them is with FFmpeg or libav tools. The latter is a fork of the former for political reasons that are way beyond this blog's concept or content.

In our case I will use libav tools as it comes with GNU / Linux testing (currently jessie) and it's available in place of ffmpeg.

Here's the command:

avconv -i <flash_video_file>.flv -f mp3 <result>.mp3

That was the simplest example to show. In the past I faced a serious issue with sound and I had to explicitly use an MP3 codec with my command:

avconv -i input.flv -acodec libmp3lame -aq 4 output.mp3

How to extract an audio file from a video

In my humble opinion, extracting the original (raw) audio file from a video is the best thing you can do if you are passionate about quality. In order to do so, you need to have MediaInfo installed.

You can use it from terminal to get all the information you need or you can use its GUI which is convenient for inexperienced users. Doing so, it helps immensely knowing the audio codec, thus making it easier for you to give the appropriate file extension.

Here is the command to extract your raw audio file from a video:

avconv -i file.mp4 -vn -acodec copy "audio_file_extracted.m4a"

How to convert MusePack (MPC) music files to MP3 files

A few years ago I remember I found this new "cool" audio codec and I wanted to rip a CD just for the sake of curiosity; I wasn't disappointed. After the conversion, the original CD disappeared from my room, and frankly I can't remember where I put it LOL! So, now I had to convert it to MP3 this time to sync it with my MP3 player and enjoy my music. The quality was exactly the same, unbelievably awesome results!

Below is the command I run to convert all my MPC files all at once:

for f in *.mpc; do avconv -i "$f" -acodec libmp3lame -aq 4 "${f%.*}".mp3; done

To explain it a bit, "for every .mpc file you find, recode it as MP3 file of quality 4, keep the original name but ignore suffix file extension; we are adding our own, that of .mp3".

How to delete multiple Debian packages

I remember once, I faced a problem with PHP5 packages. Something broke in the background while upgrading my packages, somewhere that I could not detect to fix it, that corrupted all PHP5 config files that I had to completely purge the problematic set and reinstall it.

The command goes as follows:

sudo apt-get purge $(dpkg --get-selections | grep php5 | awk '{print $1}')

How to remove orphaned packages

It's good to keep your system clean from orphaned packages that stuck on your system as remainders of already removed packages. In order to do so, you need to have deborphan package installed. Install it and do the following to clean up your system:

sudo apt-get purge $(deborphan)

Please note that you have to re-run deborphan a few more times to make sure all packages have been purged; some packages do not get purged recursively for some reason.

How to search for music files and play them on the fly with mplayer

One afternoon I was at home experimenting with shell scripting and it hit me; is there a way to search for a song at a location of my choice and as soon as I find the desired song to send to mplayer and play it on the fly? I started laughing with myself for "imagining silly things"; I guess that silly thought was not *that silly* after all. I have had figured out a way to make it happen!

Here's the "funny" command I'm talking about:

find <directory-of-your-choice> -type f -name "*.mp3" -print0 | xargs -0 mplayer

If you want to sort the songs first and then play them, do the following:

find <directory-of-your-choice> -type f -name "*.mp3" -print0 | sort -z | xargs -0 mplayer

OK, logically now you are asking yourself; what about searching for various forms of a file extension of a song? I'm sure you have songs that look something like .Mp3, .MP3, and so forth. Well, if you can search for different forms, why not for other file types as well? Yes, you can!

find <directory-of-your-choice> -type f \
    -iname "*.mp3" -o \
    -iname "*.m4a" -o \
    -iname "*.mp4" -o \
    -iname "*.aac" -o \
-print0 | sort -z | xargs -0 mplayer

How to determine multiple file types at once

A main characteristic of mine is my congenital curiosity about everything. I seek for answers vigorously, how things work, not *WHY* they work the way they do. Imagine now my anxiety to find answers to questions like "how the experts are implementing their programs, with what tools (language that is)" and so forth.

That's the case of file command. Unfortunately, for some reason it does not work properly, as it guesses the file type and that is something I wasn't willing to accept as an excuse. Fortunately for me, I have found mimetype command; just install libfile-mimeinfo-perl package and will make your life easier! You can thank me later.

Here is the command you could run to find file types of important programs located in /usr/bin/ and in /usr/sbin/:

find /usr/bin/ /usr/sbin/ -type f -exec mimetype {} \;

You can grep specific file types you are looking for and even sort them, like we did with other commands above.

That's it for now. I hope you enjoyed this new series. Any new set of commands I may find interesting, I will surely going to share it with you, have no worries about it. Until that time, cheers.