Author Archives: Tim - Page 2

shell脚本检测一个目录是否存在

#!/bin/bash
DIR="$1"

if [ $# -ne 1 ]
then
  echo "Usage: $0 {dir-name}"
  exit 1
fi

if [ -d "$DIR" ]
then
  echo "$DIR directory exists!"
else
  echo "$DIR directory not found!"
fi

linux删除重复行

sort file.txt | uniq -u

or

sort -u file.txt

shell脚本检测一个目录是否为空

#!/bin/bash
FILE=""
DIR="/tmp"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
  echo "Take action $DIR is not Empty"
else
  echo "$DIR is Empty"
fi
# rest of the logic

linux chmod 命令实例

更改目录的权限
find . -type d -exec chmod 755 {} \;

更改文件的权限
find . -type f -exec chmod 644 {} \;

更改php文件的权限
find . -type f -name ‘*.php’ -exec chmod 644 {} \;

linux copy 命令实例

在当前目录下复制文件
cp file.doc newfile.doc

复制文件到其它目录下
cp filename /tmp

复制时保留文件属性
cp -p filename /path/to/new/location/myfile

复制全部文件到其它目录下
cp * /home/backup

递归复制文件及目录
cp -R * /home/backup

禁用危险的php函数

定位php.ini

root@server [~]# php -i | grep php.ini

在php.ini中找到
disable_functions =
这一行,在其后面添加需要禁止的危险函数名,以英文逗号分隔。

disable_functions = "apache_child_terminate, apache_setenv,
define_syslog_variables, escapeshellarg, escapeshellcmd, eval,
exec, fp, fput, ftp_connect, ftp_exec, ftp_get, ftp_login,
ftp_nb_fput, ftp_put, ftp_raw, ftp_rawlist, highlight_file,
ini_alter, ini_get_all, ini_restore, inject_code, mysql_pconnect,
openlog, passthru, php_uname, phpAds_remoteInfo, phpAds_XmlRpc,
phpAds_xmlrpcDecode, phpAds_xmlrpcEncode, popen, posix_getpwuid,
posix_kill, posix_mkfifo, posix_setpgid, posix_setsid, posix_setuid,
posix_setuid, posix_uname, proc_close, proc_get_status, proc_nice,
proc_open, proc_terminate, shell_exec, system, xmlrpc_entity_decode"

重启apache服务器

root@server [~]# service restart httpd

linux tar 命令实例

打包一个目录或文件
tar -cvf filename.tar directory/file

列出tar文件的内容
tar -tvf filename.tar

抽取tar文件的内容
tar -xvf filename.tar

使用bzip来压缩文件
tar -cjvf filename.tbz file

解压bzip文件
tar -xjvf filename.tbz

使用gzip来压缩文件
tar -czvf filename.tgz file

解压gzip文件
tar -xzvf filename.tgz

检查一个php函数是否可用

<?php
if(function_exists('fsockopen')) {
echo "fsockopen function is enabled";
}
else {
echo "fsockopen is not enabled";
}
?>

其中fsockopen可以替换为其它函数

vim之删除重复行

重新排序,并且删除重复的行(只保留一行)。

:sort u

删除连续相同的行,保留最后一行。

g/^\(.*\)$\n\1$/d

删除连续相同的行,保留最初一行。

g/\%(^\1$\n\)\@<=\(.*\)$/d

linux find 命令实例

查找空目录
find /path -depth -type d -empty

查找空文件
find /path -depth -type f -empty

查找指定文件
find /path -name name_of_file

查找指定类型的文件
find /path -name “*.given_extension”

查找指定权限的txt文件
find /path -name ‘*.txt’ -perm 644

查找指定权限的文件
find /path -perm -permission_bits

查找指定文件名的任意类型文件
find /path -name ‘given_name.*’

查找最近更改的文件
find /path -mtime n

其中n为整数:
0表示最近24小时内
1表示最近48小时内
2表示最近72小时内

查找最近访问的文件
find /path -atime n

其中n为整数:
0表示最近24小时内
1表示最近48小时内
2表示最近72小时内

根据所有者查找文件
find /path -user root

查找并且删除文件
find /path -name mytestfile | xargs rm