Linux Command To Rename The Multiple Files

[sujin@localhost blog]$ ls -l
total 0
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file1
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file2
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file3
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file4
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file5
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file6
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file7
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file8


From the above  list of files i want change all the filename  starting with centos6 to centos7. I mean file centos6file2 changed to centos7file2. 

It is bit time consuming using mv command to change it one by one. Persong who using mv command for do this not a good linux user. 

By using a linux command called rename we can achieve this.


         rename centos6 centos7 centos6*


After applying above command file name list would be like below.

[sujin@localhost blog]$ rename centos6 centos7 centos6*
[sujin@localhost blog]$ 
[sujin@localhost blog]$ ls -l
total 0
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file1
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file2
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file3
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file4
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file5
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file6
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file7
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file8


!!!

Operating System Used : Fedora release 20 (Heisenbug)



How to change HOSTNAME in Linux ( CentOS6 / RHEL6 )

Hostname in Linux helps to identify the computer by name over network. When installing operating system hostname will set to default name. We can change the hostname later by changing configuration files.

We can check the current hostname by a command 


[fedo@localhost ~]$ hostname
localhost.localdomain


By editing "HOSTNAME" field in /etc/sysconfig/network file we can change the hostname.

default configuration 

[fedo@localhost ~]$ cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
NTPSERVERARGS=iburst

To edit this file we need root permission. So change from normal user to root user using su command and open the file using vim or your favourite editor and change the HOSTNAME field.

[root@localhost fedo]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=centos89
NTPSERVERARGS=iburst

I updated the hostname to centos89

reboot the computer then login and check the hostname.

[fedo@centos89 ~]$ hostname
centos89

Now you can see the hostname changed to centos89.

!!!

Operating System Used : CentOS release 6.5 (Final)




Linux command to get page size


By using getconf command we can get the size of Virtual Memory page in bytes.


[root@host-7-41 fedo]# getconf  PAGESIZE
4096


Here PAGESIZE is the argument passed to the getconf to get virtual memory page size. It gives the output as 4096 bytes.

!!!

Operating System Used : CentOS release 6.5 (Final)


How To Install GNOME Desktop Environment in Ubuntu 14.04


Introduction:


Desktop Environment is a Graphical User Interface (GUI). It  help us to easily access different application, edit files, display time, many other things we often use from desktop. 

After installing Ubuntu 14.04 or any other ubuntu version the default desktop environment would be unity. It looks nice but it is bit slow for less hardware. There is many other desktop environment available like XFCEGNOME, LXDE, etc., available and it is very easy to install. This article gives you how to install GNOME desktop in UBUNTU.

Open the Terminal using Ctrl + Alt + T or any other way you like


Step 1Update the System

        sudo apt-get update                                   

Step 2
Install gnome shell

        sudo apt-get install gnome-shell                      

Step 3: Install gnome desktop environment


        sudo apt-get install ubuntu-gnome-desktop             

Step 4: Logout from current session.

Step 5: Select the desktop type from login screen. Desktop Selection button  available just above the password box in login screen. Below images shows how to select desktop

(i) Click on the icon (Ubuntu symbol) right near to the user name





(ii) Select GNOME(Desktop) from list and then login 



Step 6: If it not worked properly then try it after restarting the Computer.

Enjoy !!!


Writing Your First Kernel Module Programming In Linux

Introduction :

Kernel module is the piece of code which extent the functionality of the linux kernel, without recompiling full kernel and not even doing any reboot.

This kernel module can be loaded and unloaded on demand. Kernel module will not run in user space instead it will run in kernel space.

Initial setup to compile kernel module:

To compile and run kernel module we need to have linux kernel header. Installing linux kernel header will vary on different linux distros.

Below command used to install kernel header for Centos 6.5. This command is same for redhat and fedora and its variant.

yum install kernel-devel

you can see the installed kernel header in /usr/src/kernels directory.



If we want to learn any new programming language we always go for Hello world program. So here i going to explain how to compile and run Hello World kernel module program.

Writing module program is different than user space program. Because in user space C program start at main() but it is not module programming.

There is two function called init_module() and cleanup_module(). This init_module() function will called when module inserted into the kernel. similarly cleanup_module will be called when module unloaded from the kernel. I will demonstrate this later.


hello.c

#include <linux/module.h> /* It used for all modules */
#include <linux/kernel.h> /* It used for KERN_INFO */

int init_module(void) /* Called when insmod */
{
printk (KERN_INFO "Hello World\n");
return 0;
}

void cleanup_module(void) /* Called when rmmod */
{
printk (KERN_INFO "Bye Bye\n");
}

We need to write the make file for making compilation in easy manner


Makefile:

obj-m += hello.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Now we have written kernel module C program and Makefile for compilation

[root@localhost hello]# ls -l
total 8
-rw-rw-r--. 1 sujin sujin 301 Aug 17 17:08 hello.c
-rw-rw-r--. 1 sujin sujin 156 Aug 17 16:57 Makefile


To compile module program simple run make command in directory where hello.c and Makefile available.

[root@localhost hello]# make
make -C /lib/modules/2.6.32-431.23.3.el6.x86_64/build M=/home/sujin/kernel_dev/module/hello modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-431.23.3.el6.x86_64'
  CC [M]  /home/sujin/kernel_dev/module/hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/sujin/kernel_dev/module/hello/hello.mod.o
  LD [M]  /home/sujin/kernel_dev/module/hello/hello.ko.unsigned
  NO SIGN [M] /home/sujin/kernel_dev/module/hello/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.32-431.23.3.el6.x86_64'


Now you will see the file called hello.ko in directory where you compiled the module.

[root@localhost hello]# ls -l
total 308
-rw-rw-r--. 1 sujin sujin   301 Aug 17 17:08 hello.c
-rw-r--r--. 1 root  root  95415 Aug 17 17:18 hello.ko
-rw-r--r--. 1 root  root  95415 Aug 17 17:18 hello.ko.unsigned
-rw-r--r--. 1 root  root    818 Aug 17 17:18 hello.mod.c
-rw-r--r--. 1 root  root  54816 Aug 17 17:18 hello.mod.o
-rw-r--r--. 1 root  root  42210 Aug 17 17:18 hello.o
-rw-rw-r--. 1 sujin sujin   156 Aug 17 16:57 Makefile
-rw-r--r--. 1 root  root     52 Aug 17 17:19 modules.order
-rw-r--r--. 1 root  root      0 Aug 17 17:18 Module.symvers

Insert the kernel module using insmod. insmod command will call init_module() function from program. You should be sudo user to insert module to the kernel

[root@localhost hello]# insmod hello.ko

You can see the installed kernel module using lsmod command
[root@localhost hello]# lsmod | head
Module                  Size  Used by
hello                    892  0
ipt_addrtype            2153  2
xt_conntrack            2776  1
ipt_MASQUERADE          2466  1
iptable_nat             6158  1
nf_nat                 22759  2 ipt_MASQUERADE,iptable_nat
bridge                 83689  0
dm_thin_pool           46566  1
dm_bio_prison           6346  1 dm_thin_pool


It is not possible to see the output of printk in terminal, because module loaded in kernel space not in user space. Instead kernel put printk output in logfile, you can see those message from log file using dmesg command.

[root@localhost hello]# dmesg | tail
SELinux: initialized (dev cgroup, type cgroup), uses genfs_contexts
SELinux: initialized (dev cgroup, type cgroup), uses genfs_contexts
Bridge firewalling registered
8021q: adding VLAN 0 to HW filter on device docker0
SELinux: initialized (dev fuse, type fuse), uses genfs_contexts
eth0: no IPv6 routers present
docker0: no IPv6 routers present
hello: module license 'unspecified' taints kernel.
Disabling lock debugging due to kernel taint
Hello World

By using rmmod command you can remove the module from kernel. It will call the cleanup_module() function in the module program

[root@localhost hello]# rmmod hello

Now you can see the cleanup message from logfile using dmesg command.

[root@localhost hello]# dmesg | tail
SELinux: initialized (dev cgroup, type cgroup), uses genfs_contexts
Bridge firewalling registered
8021q: adding VLAN 0 to HW filter on device docker0
SELinux: initialized (dev fuse, type fuse), uses genfs_contexts
eth0: no IPv6 routers present
docker0: no IPv6 routers present
hello: module license 'unspecified' taints kernel.
Disabling lock debugging due to kernel taint
Hello World
Bye Bye


That's it !!! Well done. Finally we done hello world kernel module programming.

I am not the owner of this article. I grabbed details from other articles. Full credits goes to author of The Linux Kernel Module Programming Guide.


Enjoy Linux