Wednesday, June 15, 2011

Find Files By Access, Modification Date / Time Under Linux or UNIX

Ido not remember where I saved pdf and text files under Linux. I have downloaded files from the Internet a few months ago. How do I find my pdf or text files?

You need to use the find command. Each file has three time stamps, which record the last time that certain operations were performed on the file:

[a] access (read the file's contents) - atime

[b] change the status (modify the file or its attributes) - ctime

[c] modify (change the file's contents) - mtime

You can search for files whose time stamps are within a certain age range, or compare them to other time stamps.

You can use -mtime option. It returns list of file if the file was last accessed N*24 hours ago. For example to find file in last 2 months (60 days) you need to use -mtime +60 option.

  • -mtime +60 means you are looking for a file modified 60 days ago.
  • -mtime -60 means less than 60 days.
  • -mtime 60 If you skip + or - it means exactly 60 days.

So to find text files that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -print

Display content of file on screen that were last modified 60 days ago, use
$ find /home/you -iname "*.txt" -mtime -60 -exec cat {} \;

Count total number of files using wc command
$ find /home/you -iname "*.txt" -mtime -60 | wc -l

You can also use access time to find out pdf files. Following command will print the list of all pdf file that were accessed in last 60 days:
$ find /home/you -iname "*.pdf" -atime -60 -type -f

List all mp3s that were accessed exactly 10 days ago:
$ find /home/you -iname "*.mp3" -atime 10 -type -f

There is also an option called -daystart. It measure times from the beginning of today rather than from 24 hours ago. So, to list the all mp3s in your home directory that were accessed yesterday, type the command
$ find /home/you -iname "*.mp3" -daystart -type f -mtime 1

Where,

  • -type f - Only search for files and not directories

-daystart option

The -daystart option is used to measure time from the beginning of the current day instead of 24 hours ago. Find out all perl (*.pl) file modified yesterday, enter:

 find /nas/projects/mgmt/scripts/perl -mtime 1 -daystart -iname "*.pl"

You can also list perl files that were modified 8-10 days ago, enter:
To list all of the files in your home directory tree that were modified from two to four days ago, type:

 find /nas/projects/mgmt/scripts/perl -mtime 8 -mtime -10 -daystart -iname "*.pl"

-newer option

To find files in the /nas/images directory tree that are newer than the file /tmp/foo file, enter:

 find /etc -newer /tmp/foo

You can use the touch command to set date timestamp you would like to search for, and then use -newer option as follows

 touch --date "2010-01-05" /tmp/foo # Find files newer than 2010/Jan/05, in /data/images find /data/images -newer /tmp/foo  

Read the man page of find command for more information:
man find

Tuesday, June 14, 2011

Change the machine name/hostname in Linux

How to change the hostname of a Linux system

Normally we will set the hostname of a system during the installation process. Many peoples don't care about this, and don't change the hostname even if for example this was set to something really stupid by the datacenter that installed the system (most likely they will set this to "debian" on any debian installation, etc). For me, it is important to see on each one of the ssh screens I will have open at any time a different hostname that is relevant and will give me quickly the information on what system I am logged in.

Change the hostname on a running system

On any Linux system you can change its hostname with the command 'hostname'… Here are some quick usages of the command line hostname:

 hostname

without any parameter it will output the current hostname of the system.

 hostname --fqd

it will output the fully qualified domain name (or FQDN) of the system.

 hostname NEW_NAME

will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations – see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

Permanent hostname change on Debian based systems

Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh

 /etc/hostname server

So on a Debian based system we can edit the file /etc/hostname and change the name of the system and then run:

 /etc/init.d/hostname.sh start

to make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same script we used hostname.sh).

Permanent hostname change on RedHat based systems

RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

 /etc/sysconfig/network NETWORKING=yes HOSTNAME="plain.domainname.com" GATEWAY="192.168.0.1" GATEWAYDEV="eth0" FORWARD_IPV4="yes"

So in order to preserve your change on system reboot edit this file and enter the appropriate name using the HOSTNAME variable.

Use sysctl to change the hostname

Why would someone need a different method of doing the same thing as above? No idea, but here is anyway: use sysctl to change the variable kernel.hostname:
Use:

 sysctl kernel.hostname

to read the current hostname, and

 sysctl kernel.hostname=NEW_HOSTNAME

to change it.

Wednesday, June 8, 2011

InnoDB storage engine failed ?

if you happened to find in your mysql server log the same error as below when you start up the mysql server; And you are not able to see the InnoDB storage engine as one of the enabled storage engine in your mysql server via 

mysql> show engines \G

Suppose Result
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Engine: InnoDB
     Support: YES
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Server error log:
110608 22:53:00 InnoDB: Initializing buffer pool, size = 2.0G
110608 22:53:00 InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 268435456 bytes!
110608 22:53:00 [ERROR] Plugin 'InnoDB' init function returned error.
110608 22:53:00 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.


Solution:
1. Remove ibdata1 and  ib_logfile* from the location /var/lib/mysql on your server (depends on which location you install your mysql server data folder)

CAUTIONS:
Backup your innodb tables first, before you delete the ibdata1 file, in case you haven't enable the "innodb_file_per_table" option in your my.cnf

NOTES:
My case: this error happened when i have mass changes on the settings options inside the my.cnf file (mainly on the buffering, and caching values) and trying to restart the mysql server. i have existing InnoDB tables runnning. So i have to dump out the data to be restore again later


Wednesday, June 1, 2011

Can't use "locate" in your linux?

if you have getting the error as following:


locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory

simply run 
Shell > updatedb

to update your system's file indexing, then you are able to do the searching after the process completed