Search This Blog

Friday, March 11, 2011

linux read line one by one from a file

[root@localhost ~]# cat delete_old_capture_file.sh
#!/bin/bash

while read line
do
echo "Delete $line" >> /var/log/delete_old_captured_file.log
find /opt/ntr/file_capture/ -name $line -exec rm -rf {} \;
done < /tmp/file_capture_delete.list

Save MySQL query results into a text file

There are 2 ways to do it.
1, save the result to a file in the sql query directly
select file_name
from files
limit 3
into OUTFILE '/tmp/file_capture_delete.list';


[root@localhost file_capture]# cat /tmp/file_capture_delete.list
20110311-154002_0001.bin
20110311-154002_0002.bin
20110311-154002_0003.bin

2, dump the results to a file
A, create a mysql file
[root@localhost delete_old_capture_file]# cat delete_old_capture_file.mysql
select file_name
from capture_files
limit 5

B, Run the mysql and dump the results to a file
mysql -u username -ppassword DBname < delete_old_capture_file.mysql > delete_old_capture_file.dump

Linux Delete Files/Folder Older Than n Days

[root@localhost /]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

Test it:
1, create a new folder
mkdir 20090909

[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Mar 11 16:25 20090909

2, change folder time
touch -t 200909090909 20090909/

3, check time
[root@localhost xxx]# touch -t 200909090909 20090909/
[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Sep 9 2009 20090909

4, delete files and folder older than 30 days

[root@localhost xxx]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

[root@localhost xxx]# ll
drwxr-xr-x 2 esl esl 20480 Mar 11 16:21 20110311

linux change folder date

Need to change the folder date

I created a folder on 20110311

[root@localhost file_capture]# mkdir 20110101
[root@localhost file_capture]# ll
drwxr-xr-x 2 root root 4096 Mar 11 16:13 20110101


Change the date using touch

[root@localhost file_capture]# touch -t 201101010101
touch: missing file operand
Try `touch --help' for more information.
[root@localhost file_capture]# touch -t 201101010101 20110101/
[root@localhost file_capture]# ll
total 24
drwxr-xr-x 2 root root 4096 Jan 1 01:01 20110101

[root@localhost file_capture]# touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit

Note that the -d and -t options accept different time-date formats.

If a FILE is -, touch standard output.

Report bugs to .

mysql select entryby date range

select * from capture_files where date(created_on) < DATE_SUB(CURdate(), INTERVAL 30 DAY) and date(updated_on) < DATE_SUB(CURdate(), INTERVAL 30 DAY);

Assign Values to variables from a text file

[root@localhost xxx]# cat test.sh

#!/bin/bash

read a < process.log
echo $a


results:
[root@localhost xx]# ./test.sh
mysql-bin.000005