Blog

  • 将数据文件的指定域读取到shell脚本中

    这个例子说明了怎样在 shell 脚本中从数据文件读取特定的域(field)并进行操作。例如,假设文件 employees.txt 的格式是{employee-name}:{employee-id}:{department-name},以冒号进行划分,如下所示。

    $ cat employees.txt

    Emma Thomas:100:Marketing
    Alex Jason:200:Sales
    Madison Randy:300:Product Development
    Sanjay Gupta:400:Support
    Nisha Singh:500:Sales

    下面的 shell 脚本说明了如何从这个 employee.txt 文件中读取特定的域(field)。

     $ vi read-employees.sh
    #!/bin/bash
    IFS=:
    echo "Employee Names:"
    echo "---------------"
    while read name empid dept
    do 
       echo "$name is part of $dept department"
    done < ~/employees.txt

    赋予脚本可执行权限后执行该脚本

     $ chmod u+x read-employees.sh 
     $ ./read-employees.sh 

    Employee Names:
    —————
    Emma Thomas is part of Marketing department
    Alex Jason is part of Sales department
    Madison Randy is part of Product Development department
    Sanjay Gupta is part of Support department
    Nisha Singh is part of Sales department

    来自:http://www.linuxnote.org/the-data-file-to-the-specified-domain-to-read-shell-scripts.html

  • 使用su使某用户执行某命令

    linux有20个普通的web用户,每次都要 cd ~/web,然后执行svn update,太麻烦了

    使用如下命令可以使user执行command命令,要注意这位user要有执行command的权限

    su -c "command" user

    下面的命令意思是进入到jpuyy的主目录,执行svn update

    su - -c "cd ~/web/ ; svn update" jpuyy

    su 与 su – 有挺大区别的,涉及到环境变量的问题,请参考 http://www.ha97.com/4001.html ,因此上面的命令用 su –

     

  • find命令

    linux里最重要的命令之一,可使用的选项之多令人无法记住,还好可以用man find来查询,下面以例子来学习,掌握最基础的先

    查找文件名为foobar

    find -name "foobar"

    查找不同文件后缀, 用 -o 连接

    find . -name "*.tf" -o -name "*.hcl" -exec cat {} \;

    不区分大小写查找 foobar

    find -iname "foobar"

    从 / 查找文件并限制查找的深度

    find / -maxdepth 2 -name passwd

    查找并计算 md5sum 值,后面参数不能省略

    find -iname "MyCprogram.c" -exec md5sum {} \;
    find -iname "MyCprogram.c" -exec md5sum {} +

    查到不包含某字符串的文件(反查)

    find -maxdepth 1 -not -iname "MyCProgram.c"

    根据 inode number 查找文件

    ls -li *
    find ./ -inum 17735

    对于乱码文件根据 inum 查找并删除

    find ./ -inum 17744 -exec rm {} +

    查找等于10KB的文件:

    find . -size 10k

    查找大于100KB的文件:

    find . -type f -size +100k
    查找大于100MB的文件:
    find . -type f -size +100M
    查找大于1GB的文件:
    find . -type f -size +1G

    查找空文件 并删除

    find ~ -empty
    find /path/to/dir -empty -type f -delete
    

    查找空目录 并删除

    find ~ -empty -type d
    find /path/to/dir -empty -type d -delete
    

    查找home目录中的所有文件

    find $HOME -print
    或
    find ~ -print

    查找当前目录中权限是644的文件

    find . -type f -perm 644 -exec ls -l {} \;

    查找系统中所有文件长度为0的普通文件,并列出它们的完整路径

    find / -type f -size 0 -exec ls -l {} \;

    查找/var/log/目录中更改时间在7日以前的普通文件(即保留最近7天的),并在删除之前询问

    find /var/log/ -type f -mtime +7 -ok rm {} \;

    查找/var/log目录中访问时间在7天以内的文件

    find /var/log/ -type f -mtime -7

    查找比某个文件更新的文件

    find -newer filename

    查找/var/log目录中messages.*.gz文件

    find /var/log/ -name "messages.*.gz"

    查找当前目录下,属组是root的文件

    find . -group root -exec ls -l {} \;

    对当前目录(包含子目录)进行排序

    find . -type d | sort

    与其他命令结合统计代码量

    find ./ -name '*' | xargs wc -l

    做一些漂亮的 alias 删除程序生成的临时文件或某种类型文件

    alias rmao="find . -iname a.out -exec rm {} \;"
    alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
    alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
    alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
    alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
    
  • linux的分区格式化,挂载

    在服务器使用过程中,某分区不够用,服务器只有一个硬盘,当时装系统之时,有一个sda3分区未使用。

    现将其格式化为ext3格式,挂载到/data下面,并且开机要自动挂载。

    这里先使用parted,它是一个调整分区的工具,不过现在使用它的显示功能,并不对硬盘做修改。p 代表print ,查看当前分区情况:

    #parted

    GNU Parted 2.1
    Using /dev/sda
    Welcome to GNU Parted! Type ‘help’ to view a list of commands.

    (parted) p

    Model: ATA ST31000528AS (scsi)
    Disk /dev/sda: 1000GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number Start End Size Type File system Flags
    1 1049kB 211MB 210MB primary ext3 boot
    2 211MB 2358MB 2147MB primary linux-swap(v1)
    3 2358MB 1000GB 998GB primary

    (parted) quit

    可以发现sda3没有显示file system,说明未格式化,现使用mkfs.ext3来把sda3格式化

    mkfs.ext3 /dev/sda3

    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    60907520 inodes, 243614264 blocks
    12180713 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=4294967296
    7435 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
    102400000, 214990848

    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 33 mounts or
    180 days, whichever comes first. Use tune2fs -c or -i to override.

    现在重新查看分区情况,已经是ext3

    3 2358MB 1000GB 998GB primary ext3

    创建目录/data,作为sda3的挂载点

    mkdir /data
    mount -v /dev/sda3 /data/

    为了之后开机挂载,在/etc/fstab写入如下一行

    /dev/sda3               /data                   ext3    defaults        1 1
  • jpuyy核心工具

    jpuyy核心工具

    1.jpuyy.com

    2.gmail

    3.dropbox

    4.evernote

    4.1 知识点
    4.2 生活常用记录
    4.3 工作记录

    5.QQ

    6.chrome网络数据

    7.apple账号

    8.Ohlife日记简单的记录

  • linux工具之chkconfig

    linux的启动级别:

    #   0 - halt (Do NOT set initdefault to this) 
    #   1 - Single user mode 
    #   2 - Multiuser, without NFS (The same as 3, if you do not have networking) 
    #   3 - Full multiuser mode 
    #   4 - unused 
    #   5 - X11 
    #   6 - reboot (Do NOT set initdefault to this)
    我们平时用的服务器就是3了,桌面就用5。
    chkconfig可以更改某服务的启动级别,在ubuntu默认是没有的,需要手动安装一下。
    查看所有开机启动的服务
    chkconfig --list
    设置iptables在2345级别开机启动
    chkconfig --level 2345 iptables on
    增加httpd服务
    chkconfig --add httpd
    删除httpd服务
     chkconfig --del httpd