Home –  commands
Tag Archives: commands

How to find Linux distribution name and version?

uname

unameSometimes we need to find out Linux distribution name and version of current installed Linux OS. Here is a tutorial addressing this problem.

There are three methods to find out Linux distribution and version:
1. /etc/*-release
2. lsb_release
3. /proc/version

Method 1: /etc/*-release

Issue following command to find out distro and version of installed Linux-

$ cat /etc/*-release

Sample output:

DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=16
DISTRIB_CODENAME=petra
DISTRIB_DESCRIPTION="Linux Mint 16 Petra"
NAME="Ubuntu"
VERSION="13.10, Saucy Salamander"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 13.10"
VERSION_ID="13.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

Method 2: lsb_release

lsb in lsb_release stands for Linux Standard Base

$ lsb_release -a

Sample output:

No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 16 Petra
Release: 16
Codename: petra

Method 3: /proc/version

This command returns kernel version and gcc version which was used to build it.

$ cat /proc/version

Sample output:

Linux version 3.11.0-12-generic (buildd@allspice) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu7) ) #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013

Another way to find kernel version:

This command returns kernel version of currently installed Linux OS.

$ uname -r

or

$ uname -mrs

Sample output:

Linux 3.11.0-12-generic x86_64

Thank you.

Backup your Linux remotely using rsync command

rsync is a command in Linux which stands for remote sync. It is used for backing up the data. It synchronizes the directories and files from one location to another in a good way. The backup destination could be either on local or on remote server.

Features

  • Speed: rsync replicates all the data from source to destination and then it transfers only the changed blocks/bytes to the destination. This makes the transfer fast.
  • Security: It uses encryption using ssh technique.
  • Bandwidth: It compresses the block at source and then at the receiving end it decompresses the block. It transfers the data block by block using compression and decompression at source and destination side respectively. Hence uses less bandwidth.
  • Privileges: No extra privileges are required to use rsync.

Syntax

$ rsync options source destination

Case 1: Synchronize two directories in local server

use rsync -zvr command (z to enable compression, v for verbose, r for recursive)

$ rsync -zvr /var/opt/installation/inventory/ /root/temp

The above command does not preserves the timestamp.

Case 2: Preserve timestamps using rsync -a

The -a options preserves symbolic links, permissions, timestamp, owner and group.

$ rsync -azv /var/opt/installation/inventory/ /root/temp/

Case 3: Synchronize only a single file

Specify the file name to the rsync command

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/

Case 4: Synchronize files Local->Remote

$ rsync -avz /root/temp/ chankey@192.168.200.10:/home/chankey/temp/

Case 5: Synchronize files Remote->Local

$ rsync -avz chankey@192.168.200.10:/var/lib/rpm /root/temp

Case 6: Select remote shell

$ rsync -avz -e ssh chankey@192.168.200.10:/var/lib/rpm /root/temp

rsync -e ssh => uses the ssh shell for remote sync

Case 7: Do not overwrite modified file at destination

If the file at the destination is modified and if we don’t want to change it back to the old one again then using rsync -u option.

$ rsync -avzu chankey@192.168.200.10:/var/lib/rpm /root/temp

Case 8: Synchronize only the directory Tree structure (not files)

use rsync -d option for this purpose.

$ rsync -v -d chankey@192.168.200.10:/var/lib/ .

Case 9: Do not create new file at destination, just update existing files

use --existing option

$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .

Case 10: Transfer the entire file

use rsync -W option

#  rsync -avzW  chankey@192.168.200.10:/var/lib/rpm/ /root/temp

How to view contents of a file in Linux

view file in linux

You can use any of the method given below to view contents of a file in Linux. Let us say the file name is “filename”. Then you can view its content…

1. using cat command

chankey@linuxstall:$ cat filename

or

chankey@linuxstall:$ cat < filename

2. using head command

chankey@linuxstall:$ head filename

3. using tail command

chankey@linuxstall:$ tail filename

4. using pg command

chankey@linuxstall:$ pg filename

5. using more command

chankey@linuxstall:$ more filename

6. using paste command

chankey@linuxstall:$ paste filename

7. using grep command

chankey@linuxstall:$ grep '.*' filename

8. using cut command

chankey@linuxstall:$ cut -c 1- filename

9. using while loop

chankey@linuxstall:$ while read line
>do
>echo $line
>done < filename

10. using xargs command

chankey@linuxstall:$ xargs -L1 < filename

11. using sed

chankey@linuxstall:$ sed '' filename

or

chankey@linuxstall:$ sed -n 'p' filename

or

chankey@linuxstall:$ sed -n '1, $p' filename

12. using awk

chankey@linuxstall:$ awk '1' filename

or

chankey@linuxstall:$ awk '{print;}' filename

or

chankey@linuxstall:$ awk '{print $0;}' filename

13. using perl

chankey@linuxstall:$ perl -pne '' filename

or

chankey@linuxstall:$ perl -ne 'print;' filename

or

chankey@linuxstall:$ perl -ne 'print $_;' filename

If you know some more ways then share them in comments.

A complete ffmpeg tutorial

Introduction

ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.

Installing ffmpeg

chankey@linuxstall:$ sudo apt-get install ffmpeg

Uses

Below are some tasks which you can perform using ffmpeg.

Getting information of a video

To get information of a video file use -i option as:

chankey@linuxstall:$ ffmpeg -i /path/videofile.avi

Making video from images

You can combine the images to make a video (like movie maker in windows) using ffmpeg as:

chankey@linuxstall:$ ffmpeg -f image2 -i image%d.jpg videofile.mpg

The above command will take all the images from the current directory (image1.jpg, image2.jpg, etc) and will join them to make a video file videofile.mpg.

Making images from video file

This is just opposite of the last step. This will take a video and will generate images from it.

chankey@linuxstall:$ ffmpeg -i videofile.mpg image%d.jpg

Encoding video for iPod/iPhone

Source file: s_videofile.avi
Audio codec: aac
Audio bitrate: 128 kb/s
Video codec: mpeg4
Video bitrate: 1200 kb/s
Video size: 320×180
Generated video: f_videofile.mp4

chankey@linuxstall:$ ffpmeg -i s_videofile.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200 kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320*180 -title X f_videofile.mp4

Encoding video for PSP

Source : s_videofile.avi
Audio codec : aac
Audio bitrate : 32 kb/s
Video codec : xvid
Video bitrate : 1200 kb/s
Video size : 320×180
Generated video : f_videofile.mp4

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac f_videofile.mp4

Video to Audio

You can extract the audio from a video file and save it as mp3 format.

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 soundfile.mp3

Source video : s_video.avi
Audio bitrate : 192kb/s
output format : mp3
Generated sound : soundfile.mp3

wav to mp3

chankey@linuxstall:$ ffmpeg -i s_audiofile.wav -acodec mp3 -ab 192k f_audiofile.mp3

avi to mpg

chankey@linuxstall:$ ffmpeg -i s_videofile.avi f_videofile.mpg

mpg to avi

chankey@linuxstall:$ ffmpeg -i s_videofile.mpg f_videofile.avi

avi to flv

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv f_videofile.flv

avi to dv

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 f_videofile.dv

avi to gif

chankey@linuxstall:$ ffmpeg -i s_videofile.avi f_giffile.gif

Mixing video with a sound file

chankey@linuxstall:$ ffmpeg -i s_audiofile.wav -i s_videofile.avi f_videofile.mpg

avi to mpeg (for dvd players)

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target pal-dvd -ps 2000000000 -aspect 16:9 f_videofile.mpeg

ps 2000000000 is the maximum size for the output file in bits hence it is 2 GB here.

Compress avi to divx

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -s 320x240 -vcodec msmpeg4v2 f_videofile.avi

Compress Ogg Theora to mpeg dvd

chankey@linuxstall:$ ffmpeg -i s_videofile.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 f_videofile.mpg

Compress avi to SVCD mpeg2

NTSC format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target ntsc-svcd f_videofile.mpg

PAL format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target pal-svcd f_videofile.mpg

Compress avi to VCD mpeg2

NTSC format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target ntsc-vcd f_videofile.mpg

PAL format:

chankey@linuxstall:$ ffmpeg -i s_videofile.avi -target pal-vcd f_videofile.mpg

Multipass encoding

chankey@linuxstall:$ ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

sudo command

sudo command

sudo command

sudo command is required when executing a command with some other user’s permission. Other user can either be root or else. Why we need to execute command with root’s permission? There are some commands that do some change in the system which are dangerous and can bring the system down, only root has such permissions.

sudo command

WITH GREAT POWER COMES GREAT RESPONSIBILITY!

One can also switch user via su command and then execute the desired command but sudo is much more secure than su. Commands (along with arguments) executed with sudo are logged in a file. For Red Hat distros, /var/log/secure saves all the logs and for debian based, its /var/log/auth.log.

The file /etc/sudoers can be edited for configuring sudo and giving privileges to individual users or group of users.

Setting up sudo

For an individual user

Syntax for allowing an individual user (here Ayush) is:

ayush ALL=(ALL) ALL

“ayush” here is username of the user you want to allow
“ALL” specifies that sudo is accessible from all terminals
“(ALL)” specifies all users
“ALL” for all commands

For a group

Entry in sudoers file goes like:

%linuxstall ALL=(ALL) ALL

“linuxstall” can be replaced by the name of group.

Execute commands as root

Once the sudoers file is all set, you can now execute commands as root. For an instance, mount command can only be executed by root, but things go well with sudo.

ayush@linuxstall:$ sudo mount /dev/sda1 /mnt

 

Use sudo command on the fly while editing in vim

There are some files that only root can save (say /etc/group) but having sudo in hand, it can be done as follows:

ayush@linuxstall:$ sudo vim /etc/group

But, if you have opened the file as a normal user and wish to save but unable as only root can do that- sudo is your friend:

:w !sudo tee %

Above command will the save the file for you as a root even when you didn’t use it while opening. sudo just doesn’t mind!

Security

If a user is not listed in sudoers file and tries to use sudo, admin will be notified by making a log entry and user will get following error:

<user> is not in the sudoers file. This incident will be reported.