Basic Shell Commands

Basic Shell Commands

Jeremy Sanders

October 2011

 

  1. acroread – Read or print a PDF file.
  2. cat – Send a file to the screen in one go. Useful for piping to other programs
    cat file1                       # list file1 to screen
    cat file1 file2 file3 > outfile # add files together into outfile
    cat *.txt > outfile             # add all .txt files together
    cat file1 file2 | grep fred     # pipe files

     

  3. cc – Compile a C program
    cc test1.c                     # compile test1.c to a.out
    cc -O2 -o test2.prog test2.c   # compile test2.c to test2.prog

     

  4. cd – Change current directory
    cd                     # go to home directory
    cd ~/papers            # go to /home/user/papers
    cd ~fred               # go to /home/fred
    cd dir                 # go to directory (relative)
    cd /dir1/dir2/dir3...  # go to directory (absolute)
    cd -                   # go to last directory you were in

     

  5. cp – Copy file(s)
    cp file1 file2                      # copy file1 to file2
    cp file1 directory                  # copy file1 into directory
    cp file1 file2 file3 ... directory  # copy files into directory
    cp -R dir1 dir2/  # copy dir1 into dir2 including subdirectries
    cp -pR dir1 dir2/ # copy directory, preserving permissions

     

  6. date – Shows current date
    > date
    Sat Aug 31 17:18:53 BST 2002

     

  7. dvips – Convert a dvi file to PostScript
    dvips document.dvi        # convert document.dvi to document.ps
    dvips -Ppdf document.dvi  # convert to ps, for conversion to pdf

     

  8. emacs – The ubiquitous text editor
    emacs foo.txt             # open file in emacs
    emacsclient foo.txt       # open file in existing emacs (need to use
                              # M-x start server first)

     

  9. file – Tells you what sort of file it is
    > file temp_70.jpg 
    temp_70.jpg: JPEG image data, JFIF standard 1.01,
    resolution (DPI), 72 x 72

     

  10. firefox – Start Mozilla Firefox
  11. f77/f90 – Compile a Fortran 77/99 program
    f77 -O2 -o testprog testprog.f

     

  12. gedit – Gnome text editor
  13. gnuplot – A plotting package.
  14. grep – Look for text in files. List out lines containing text (with filename if more than one file examined).
    grep "hi there" file1 file2 ... # look for 'hi there' in files
    grep -i "hi there" filename     # ignore capitals in search
    cat filename | grep "hi there"  # use pipe
    grep -v "foo" filename          # list lines that do not include foo

     

  15. gtar – GNU version of the tar utility (also called tar on Linux). Store directories and files together into a single archive file. Use the normal tar program to backup files to a tape. See info tar for documentation.
    gtar cf out.tar dir1    # put contents of directory into out.tar
    gtar czf out.tar.gz dir1 # write compressed tar, out.tar.gz
    gtar tf in.tar          # list contents of in.tar
    gtar tzf in.tar.gz      # list contents of compressed in.tar.gz
    gtar xf in.tar          # extract contents of in.tar here
    gtar xzf in.tar.gz      # extract compressed in.tar.gz
    gtar xf in.tar file.txt ... # extract file.txt from in.tar

     

  16. gv – View a Postscript document with Ghostscript.
  17. gzip / gunzip – GNU Compress files into a smaller space, or decompress .Z or .gz files.
    gzip file.fits          # compresses file.fits into file.fits.gz
    gunzip file.fits.gz     # recovers original file.fits
    gzip *.dat              # compresses all .dat files into .dat.gz
    gunzip *.dat.gz         # decompresses all .dat.gz files into .dat
    program | gzip > out.gz # compresses program output into out.gz
    program | gunzip > out  # decompresses compressed program output

     

  18. info – A documentation system designed to replace man for GNU programs (e.g. gtar, gcc). Use cursor keys and return to go to sections. Press b to go back to previous section. A little hard to use.
    info gtar               # documentation for gtar

     

  19. kill – Kill, pause or continue a process. Can also be used for killing daemons.
    > ps -u jss
    ...
     666  pts/1        06:06:06  badprocess 
    > kill 666        # this sends a ``nice'' kill to the
                      # process. If that doesn't work do
    > kill -KILL 666   # (or equivalently)
    > kill -9 666     # which should really kill it!
    
    > kill -STOP 667  # pause (stop) process 
    > kill -CONT 667  # unpause process

     

  20. latex – Convert a tex file to dvi
  21. logout – Closes the current shell. Also try “exit”.
  22. lp – Sends files to a printer
    lp file.ps  # sends postscript file to the default printer
    lp -dlp2 file.ps           # sends file to the printer lp2
    lp -c file.ps    # copies file first, so you can delete it
    lpstat -p lp2         # get status and list of jobs on lp2
    cancel lp2-258                  # cancel print job lp2-258 
    
    lpr -Plp2 file.ps                    # send file.ps to lp2
    lpq -Plp2                        # get list of jobs on lp2
    lprm -Plp2 1234                   # delete job 1234 on lp2

     

  23. ls – Show lists of files or information on the files
    ls file     # does the file exist?
    ls -l file  # show information about the file
    ls *.txt    # show all files ending in .txt
    ls -lt      # show information about all files in date order
    ls -lrt     # above reversed in order
    ls -a       # show all files including hidden files
    ls dir      # show contents of directory
    ls -d dir   # does the directory exist?
    ls -p       # adds meaning characters to ends of filenames
    ls -R       # show files also in subdirectories of directory
    ls -1       # show one file per line

     

  24. man – Get instructions for a particular Unix command or a bit of Unix. Use space to get next page and q to exit.
    man man      # get help on man
    man grep     # get help on grep
    man -s1 sort # show documentation on sort in section 1

     

  25. more – Show a file one screen at a time
    more file                # show file one screen at a time
    grep 'frog' file | more  # Do it to output of other command

     

  26. mv – Move file(s) or rename a file
    mv file1 file2                     # rename file1 to file2
    mv dir1 dir2                       # rename directory dir1 to dir2
    mv file1 file2 file3 ... directory # move files into directory

     

  27. nano – very simple text editor. Warning – this program can introduce extra line breaks in your file if the screen is too narrow!
  28. nice – Start a process in a nice way. Nice levels run from -19 (high priority) to 19 (low priority). Jobs with a higher priority get more CPU time. See renice for more detail. You should probably be using the grid-engine to run long jobs.
    nice +19 myjob1   # run at lowest priority
    nice +8 myjob2    # run at lowish priority

     

  29. openoffice.org – a free office suite available for Linux/Unix, Windows and Mac OS X.
  30. passwd – change your password
  31. pine – A commonly used text-based mail client. It is now called alpine. Allows you to send and receive emails. Configuration options allow it to become quite powerful. Other alternatives for mail are mozilla mail and mutt, however I suggest you stick to alpine or thunderbird.
  32. printenv – Print an environment variable in tcsh
    setenv MYVARIABLE Fred
    printenv MYVARIABLE
    printenv # print all variables

     

  33. ps – List processes on system
    > ps -u jss          # list jss's processes
      934 pts/0    00:00:00 bash
    ^^^^^ ^^^^^    ^^^^^^^^ ^^^^^^^
    PID   output   CPU time name
    > ps -f      # list processes started here in full format
    > ps -AF     # list all processes in extra full format
    > ps -A -l            # list all processes in long format
    > ps -A | grep tcsh   # list all tcsh processes

     

  34. pwd – Show current working directory
    > pwd
    /home/jss/writing/lecture

     

  35. quota – Shows you how much disk space you have left
    > quota -v
    ...

     

  36. renice – Renice a running process. Make a process interact better with other processes on the system (see top to see how it is doing). Nice levels run from -19 (high priority) to 19 (low priority). Only your own processes can be niced and they can only be niced in the positive direction (unless you are root). Normal processes start at nice 0.
    > ps -u jss | grep bigprocess      # look for bigprocess
     1234 pts/0    99:00:00 bigprocess
    > renice 19 1234                   # renice PID 1234 to 19

     

  37. rm – Delete (remove) files
    rm file1     # delete a file (use -i to ask whether sure)
    rm -r dir1   # delete a directory and everything in it (CARE!)
    rm -rf dir1  # like above, but don't ask if we have a -i alias

     

  38. rmdir – Delete a directory if it is empty (rm -r dirname is useful if it is not empty)
    rmdir dirname

     

  39. staroffice – An office suite providing word processor, spreadsheet, drawing package. See Users’ Guide on how to install this. This is a commercial version of the openoffice office package – use openoffice.org on linux.
  40. setenv – Set an environment variable in tcsh.
    setenv MYVARIABLE Fred
    echo Hi there $MYVARIABLE

     

  41. tar – Combine files into one larger archive file, or extract files from that archive (same as gtar on Linux).
    tar cvf /dev/rmt/0 ./      # backup cwd into tape
    tar tvf /dev/rmt/0         # list contents of tape
    tar xvf /dev/rmt/0         # extract contents of tape

     

  42. thunderbird – Start mozilla thunderbird.
  43. top – Interactively show you the “top” processes on a system – the ones consuming the most computing (CPU) time. Press the “q” key in top to exit. Press the “k” key to kill a particular process. Press “r” to renice a process.

 

About this document …

Basic Shell Commands

This document was generated using the LaTeX2HTML translator Version 2008 (1.71)

Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were:
latex2html -split 0 -font_size 10pt -no_navigation commands_basic.tex

The translation was initiated by Jeremy Sanders on 2011-10-02


Jeremy Sanders 2011-10-02

python-django 部署

trouble shooting:

1.ImportError: Could not import settings ‘mysite.settings’ when deploying django? just add the following to the wsgi.py file:
[cce_ruby]
import sys
sys.path = [‘/home/mysite/prg/mysite/mysite_django’, ‘/home/mysite/prg/mysite’] + sys.path

[/cce_ruby]

2. you can confirm the problem by:
[cce_ruby]
python
>>>import sys
>>>print sys.path
# you will find there is no directory of your django root. that's the problem
>>> sys.path = ['/home/mysite/prg/mysite/mysite_django', '/home/mysite/prg/mysite'] + sys.path
>>>import mysite_django.settings
# this should be no errors 
[/cce_ruby]
3. a config file of apache should like this:
[cce_ruby]
<VirtualHost mysite.com:80>
    WSGIDaemonProcess mysite.com python-path=/usr/lib/python2.7/site-packages:/var/www/html/mysite/django
    WSGIScriptAlias / /var/www/html/mysite/django/mysite/wsgi.py
    ServerAdmin abc@gmail.com
    DocumentRoot /var/www/html/mysite/django
    ServerName mysite.com
    ErrorLog /var/log/httpd/apply.mysite.com-error.log
    CustomLog /var/log/httpd/apply.mysite.com-access.log combined
    <Directory /var/www/html/mysite/django>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
[/cce_ruby]

reference from : http://stackoverflow.com/questions/8052559/how-to-troubleshoot-importerror-could-not-import-settings-mysite-settings-w

95个linux小技巧

这里总结了Linux使用中的一些小技巧

1、实现RedHat非正常关机的自动磁盘修复

先登录到服务器,然后在/etc/sysconfig里增加一个文件autofsck,内容如下:
AUTOFSCK_DEF_CHECK=yes
PROMPT=yes

2、改变文件或目录之最后修改时间(变为当前时间)
执行格式:touch name ( name 可为文件或目录名称。)

3、如何设置login后欢迎信息

修改/etc/motd,往里面写入文本即可。

4、如何设置login前欢迎界面

修改/etc/issue或者issue.net,往里面写入文本。
issue的内容是出现在本机登录的用户界面上,而issue.net则是在用户通过网络telnet的时候出现。

5、如何修改网卡MAC地址

首先必须关闭网卡设备,否则会报告系统忙,无法更改。
命令是: /sbin/ifconfig eth0 down
修改 MAC 地址,这一步较 Windows 中的修改要简单。
命令是:/sbin/ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
重新启用网卡 /sbin/ifconfig eht0 up
网卡的 MAC 地址更改就完成了

6、建立别名/删除别名

alias cp=’cp -i’
unalias cp

7、adduser m -g cvsroot -s /bin/false

添加用户m,参数-s /bin/false表示不允许用户直接登录服务器
id m
显示m用户的uid和gid号。
8、 强制卸载rpm包

rpm -e –nodeps 包名称
#个别不正常情况下:
rm -f /var/lib/rpm/__*
rpm –rebuilddb

9、拒绝除root用户的其它用户登陆

touch /etc/nologin
也可以在/etc/passwd中加!对指定用户限制登陆

10、只允许某个IP登录,拒绝其他所有IP

在 /etc/hosts.allow 写:
sshd: 1.2.3.4
在 /etc/hosts.deny 写:
sshd: ALL
用 iptables 也行:
iptables -I INPUT -p tcp –dport 22 -j DROP
iptables -I INPUT -p tcp –dport 22 -s 1.2.3.4 -j ACCEPT

11、禁止某个用户通过ssh登录

在/etc/ssh/sshd_conf添加
AllowUsers 用户名
或者
AllowGroups 组名
或者
DenyUsers 用户名

12、设定登录黑名单

vi /etc/pam.d/sshd
增加
auth required /lib/security/pam_listfile.so item=user sense=deny file=/etc/sshd_user_deny_list onerr=succeed
所有/etc/sshd_user_deny_list里面的用户被拒绝ssh登录

13、检查自己所属之群组名称

执行格式:groups

14、修改文件/文件夹所属用户组(支持-R)

chown .组名 文件名(注:组名名勿忘”.”,“:”也可)
也可chgrp 组名 文件名
chown 用户名.组名 文件名(同时修改所属用户及用户组)

15、用fuser命令查看一下是哪些进程使用这个分区上的文件:

fuser –v –m /usr
如果没有什么重要的进程,用以下命令停掉它们:
fuser -k –v –m /usr
然后就可以重新挂载这些文件系统了。

16、网络唤醒主机

ether-wake 目标网卡MAC

17、如何查找大小为500K到1000K之间的文件

find / -type f -size +500k -and -size -1000k

18、让主机不响应ping

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
若想恢复就用
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
#必须是用命令改,不能是vi修改

19、自动注销ROOT

编辑你的配置文件”vi /etc/profile”,在”HISTSIZE=”后面加入下面这行:
TMOUT=300
#300,表示300秒

20、ls只列出目录

ls -lF | grep ^d
ls -lF | grep /$
ls -F | grep /$

21、让cron任务不回馈信息

* * * * * cmd > /dev/null 2>&1

22、lsof(list open files)用法

lsof -i x
lsof abc.txt 显示开启文件abc.txt的进程
lsof -i :22 知道22端口现在运行什么程序
lsof -c nsd 显示nsd进程现在打开的文件
lsof -g gid 显示归属gid的进程情况

23、改变sshd 的端口

在/etc/ssh/sshd_config 中加入一行:Port 2222,/etc/init.d/sshd restart 重启守护进程

24、防止任何人使用su 命令成为root

vi /etc/pam.d/su,在开头添加下面两行:
auth sufficient /lib/security/pam_rootok.so
auth required /lib/security/Pam_wheel.so group=wheel
然后把用户添加到“wheel”组:chmod -G10 usernam

25、如何让ssh 只允许指定的用户登录

方法1:在/etc/pam.d/sshd 文件中加入
auth required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail
然后在/etc 下建立sshusers 文件,加入允许使用ssh 服务的用户名(每一个用户名都要单独一行),重新起动sshd

26、利用ssh 复制文件

1、从A 复制B(推过去) #scp -rp /path/filename username@remoteIP:/path
2、从B 复制到A(拉过来)#scp -rp username@remoteIP:/path/filename /path

27、linux机器挂载windows上的共享文件

windows IP:192.168.1.1
mount -t smbfs -o username=massky,password=massky //192.168.1.1/dbf /mnt/share
如想机器重启自动挂载,vi /etc/fstab最后加入:
//192.168.1.1/dbf /mnt/share smbfs defaults,auto,username=massky,password=massky 0 0

28、定制linux 提示符

在bash 中提示符是通过一个环境变量$PS1 指定的。用export $PS1 查看现在的值,比较直
观常用的提示符可以设定为export PS1=“[\u@\h \W]\$”。其中\u 代表用户名,\h 代表主机
名,\W 代表当前工作目录的最后一层,如果是普通用户\$则显示$,root 用户显示#。

29、清空文件

echo > 文件名

30、DNS相关

host -a domain.com #显示相关资讯都列出来
host domain.com 202.106.0.20 #用202.106.0.20这台DNS服务器查询domain.com

31、前后台任务相关

jobs 列出属于当前用户的进程
bg 将进程搬到后台运行(Background)
fg 将进程搬到前台运行(Foreground)
万一你运行程序时忘记使用“&”了,又不想重新执行。可以先使用ctrl+z挂起程序,然后敲入bg命令,这样程序就在后台继续运行了。

32、查找当前目录下七天前的文件,并删除

find ./ -mtime +7 -type f -exec rm {} \;

33、产生指定大小的文件(bs*count)

dd if=/dev/zero of=filename bs=1000000 count=10

34、查找当前目录下文件并更改扩展名

更改所有.ss文件为.aa
# find ./ -name “*.ss” -exec rename .ss .aa ‘{}’ \;

35、修改系统时间

date -s “2005-6-4 17:26″

36、让服务器自动同步时间

0 1 * * * /usr/sbin/ntpdate 210.72.145.44
或 0 1 * * * rdate -s time.nist.gov

37、解决打开文件过多的问题

在etc/security/limits.conf 配置文件中设置进程文件描述符极限:
* soft nofile 2048
* hard nofile 4096
系统级文件描述符极限及timeout时间修改,添加如下两行到 /etc/rc.d/rc.local 启动脚本中:
# Increase system-wide file descriptor limit.
echo 65536 > /proc/sys/fs/file-max
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
#一般情况下,最大打开文件数比较合理的设置为每4M物理内存256,比如1G内存可以设为65536,
#而最大的使用的i节点的数目应该是最大打开文件数目的3倍到4倍

38、如何用tar打包一个目录时,去掉其中的某些子目录或指定文件

加参数 –exclude 即可, 可加文件名或目录名, 可多写
tar cvf –exclude {dirname,filename} #dirname不要加/

39、终端下修改服务器时区

/usr/sbin/timeconfig
或直接#/etc/sysconfig/clock

40、关闭启动时的内存不足256M提示

#vi /etc/rc.sysinit #把最后六行注释掉
或#vi /var/lib/supportinfo
把其中的 MinRAM: 256M 这个值调低点.

41、在多层目录中查找到某一指定”字符串”

grep string -R /etc/sysconfig/
find ./pathname/ -name ‘*’ | xargs grep ‘string’

42、占用CPU的一个命令

#yes string #有时候测试用得上。狂占CPU

43、Kill相关

kill -STOP [pid]
发送SIGSTOP (17,19,23)停止一个进程,而并不消灭这个进程。
kill -CONT [pid]
发送SIGCONT (19,18,25)重新开始一个停止的进程。
kill -KILL [pid]
发送SIGKILL (9)强迫进程立即停止,并且不实施清理操作。
kill -9 -1
终止你拥有的全部进程。

44、在当前目录下建个bak目录,然后 cp * bak,会提示略过bak,有其它办法可以排除指定文件(夹)?

ls -F|grep -v \/|xargs -i cp {} bak #推荐
或 find ! -name “./bak”

45、 根据进程名显示进程号

# pidof httpd
1846 1845 1844 1843 1842 1841 1840 1839 1820

46、e2fsck

检查使用 Linux ext2 档案系统的 partition 是否正常工作, 检查 /dev/hda5 是否正常,如果有异常便自动修复,并且设定若有问答,均回答[是] :
e2fsck -a -y /dev/hda5

47、反向输出

rev 反向输出(以行为单位)
tac 反向输出(全文)

48、显示终端号

tty

49、文件行数/字数统计

wc –l file 计算文件行数
wc -w file 计算文件中的单词数
wc -c file 计算文件中的字符数

50、出每行第5个到第9个字符

cut -b5-9 file.txt

51、删除文本文件中出现的行列

uniq

52、返回文件所在路径

dirname /bin/tux #将返回 /bin

53、fcitx在英文环境下正常使用

#vi ~/.bashrc
xport LC_CTYPE=”zh_CN.UTF-8″
export XMODIFIERS=”@im=fcitx”
export XIM=fcitx
export XIM_PROGRAM=fcitx
#gnome-session-properties可以把fctix加入登入后自启动

54、split分割合并文件

split -b1440k a_whopping_big_file chunk #拆
cat chunk* > a_whopping_big_file #合
55、如何知道某个命令使用了什么库文件

例如要知道ls使用了什么库文件,可以使用:
$ ldd /bin/ls

56、如何使一个用户进程在用户退出系统后仍然运行

使用nohup command &,比如:nohup wget -c ftp://test.com/test.iso
#这样即使用户退出系统,wget进程仍然继续运行直到test.iso下载完成为止

57、如何限制用户的最小密码长度

修改/etc/login.defs里面的PASS_MIN_LEN的值。比如限制用户最小密码长度是8:
PASS_MIN_LEN 8

58、如何取消root命令历史记录以增加安全性

为了设置系统不记录每个人执行过的命令,就在/etc/profile里设置:
HISTFILESIZE=0
HISTSIZE=0
或者:
ln -s /dev/null ~/.bash_history

59、如何测试硬盘性能

使用hdparm -t -T /dev/hdX就可以测试硬盘的buffer-cache reads和buffered disk reads两个数据,可以用来当作硬盘性能的参考。
同时使用hdparm -c3 /dev/hdaX还能设置硬盘以32bit传输,以加快数据传输的速度。

60、如何列出一个目录占用的空间

du或du -s或du -k
du -S | sort -n 可以迅速发现那个目录是最大的。
用df可以看到已安装的文件系统的空间大小及剩余空间大小。
quota -v查看用户的磁盘空间信息,如果你用quota限制了用户空间大小的话。

61、如何使新用户首次登陆后强制修改密码

#useradd -p ‘’ testuser; chage -d 0 testuser

62、在Linux中有时开机不自动检查新硬件,新安装的网卡找不到。请问怎么解决?

答:自动检查新硬件的服务是Kudzu,用户可以用“ntsysv”命令启动该服务。下次重启就会找到用户的新网卡。

63、如何让系统密码和samba密码一致,并可以让用户自行修改他们的密码.

使用web界面來同步更改system passwd 及 samba password
下载 http://changepassword.sourceforge.net/
安装就可以了.先看README哈.
附加:
将系统用户批量倒成samba用户.
less /etc/passwd | mksmbpasswd.sh >; /etc/samba/smbpasswd

64、更改Linux启动时用图形界面还是字符界面

cd /etc
vi inittab
将id:5:initdefault: 其中5表示默认图形界面
改id:3: initdefault: 3表示字符界面

65、配置smb可以被哪些IP所用.

cd /etc/samba
Vi smb.conf
找到hosts allow = 192.168.1. 192.168.2. 127.
修改其为哪些机器所用,注意IP之间用逗号分开
举例:
hosts allow =192.168.1.110,192.168.1.120

67、禁止在后台使用CTRL-ALT-DELETE重起机器

cd /etc/inittab
vi inittab 在文件找到下面一行
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now (注释掉这一行)
如: # Trap CTRL-ALT-DELETE
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

68、修改主机名

vi /etc/sysconfig/network
修改HOSTNAME一行为HOSTNAME=主机名

69、查看开机检测的硬件

dmesg | more

70、查看硬盘使用情况

df –m

71、查看目录的大小

du –sh dirname

72、解压小全

tar xvfj lichuanhua.tar.bz2
tar xvfz lichuanhua.tar.gz
tar xvfz lichuanhua.tgz
tar xvf lichuanhua.tar
unzip lichuanhua.zip
注:压缩 tar cvfz FileName.tar.gz DirName

73、显示内存使用情况

free –m

74、显示系统运行了多长时间

uptime

75、显示开机自检的内容命令

dmesg

76、端口的详细列表

/etc/services

77、查看物理信息

lspci

78、文本截面的中文支持

RH 9.0自带安装包 zhcon_0.2.3_1.rh9.i386.rpm
安装完成后,执行: zhcon 就可以支持中文了

79、linux 控制 windows

(1)用RH9.0自己带rdesktop,版本是1.2.0
命令:rdesktop –u user –f 192.168.1.70 色默认的是8位
(2)要达到16色,就要下载新版本1.3.0
rdesktop –a 16 –u lichuanhua –g 800*600 192.168.1.70

80、不让显示器休眠

setterm –blank 0

81、显示最后一个登录到系统的用户

last

82、查看所有帐号的最后登录时间

lastlog /var/log/lastlog

83、查看系统自开通以来所有用户的登录时间和地点

cat /var/log/secure

84、显示当前用户所属信息

id

85、如何知道Apache的连接数目

ps -ef|grep httpd|wc -l #其它服务可以类推
netstat -nat|grep -i “80″|wc -l # 以上结果再减1吧

86、删除用户帐号的同时,把用户的主目录也一起删除

userdel -r 用户名

87、修改已有用户的信息

usermod [参数] 用户名
参数: -c, -d, -m, -g, -G, -s, -u以及-o与adduser参数意义相同
新参数: -l 新用户名(指定一个新的账号,即将原来的用户名改为新的用户名)

88、改变redhat的系统语言/字符集

改 /etc/sysconfig/i18n 文件,如
LANG=”en_US”,xwindow会显示英文界面,
LANG=”zh_CN.GB18030″,xwindow会显示中文界面。
还有一种方法
cp /etc/sysconfig/i18n $HOME/.i18n
修改 $HOME/.i18n 文件,如
LANG=”en_US”,xwindow会显示英文界面,
LANG=”zh_CN.GB18030″,xwindow会显示中文界面。
这样就可以改变个人的界面语言,而不影响别的用户
vi .bashrc
export LANG=zh_CN.GB2312
export LC_ALL=zh_CN.GB2312

89、cd光盘做成iso文件

cp /dev/cdrom xxxx.iso

90、快速观看开机的硬件检测

dmesg | more

91、查看硬盘的使用情况

df -k 以K为单位显示
df -h 以人性化单位显示,可以是b,k,m,g,t..

92、查看目录的大小

du -sh dirname
-s 仅显示总计
-h 以K、M、G为单位,提高信息的可读性。KB、MB、GB是以1024为换算单 位, -H以1000为换算单位。

93、查找或删除正在使用某文件的进程

fuser filename
fuser -k filename

94、linux中让用户的密码必须有一定的长度,并且符合复杂度

vi /etc/login.defs,改PASS_MIN_LEN

95、以不同的用户身份运行程序

su – username -c “/path/to/command”
有时候需要运行特殊身份的程序, 就可以让su来做

mysql 大数据查询性能加速研究

对于50万级别的数据, 应该有必要研究一下如何增加mysql的性能了, 当然了mysql处理50万数据的表就算按默认设置也是ok的, 不过肉眼能感受到速度的下降, 但在接受范围内. 还是研究下来对付百万级别的数据量:

1. 开启默认的mysql缓存

大多数情况下这个好像是开启的, 我们来确认一下:

[cce]
#mysql localhost -u root -p
#enter passwd
mysql>
mysql>use database1
[/cce]
[cce]
mysql> show VARIABLES LIKE '%cache%';

+------------------------------+----------------------+
| Variable_name                | Value                |
+------------------------------+----------------------+
| binlog_cache_size            | 32768                |
| have_query_cache             | YES                  |
| key_cache_age_threshold      | 300                  |
| key_cache_block_size         | 1024                 |
| key_cache_division_limit     | 100                  |
| max_binlog_cache_size        | 18446744073709547520 |
| query_cache_limit            | 1048576              |
| query_cache_min_res_unit     | 4096                 |
| query_cache_size             | 16777216             |
| query_cache_type             | ON                   |
| query_cache_wlock_invalidate | OFF                  |
| table_definition_cache       | 256                  |
| table_open_cache             | 64                   |
| thread_cache_size            | 8                    |
+------------------------------+----------------------+
[/cce]
[cce]
mysql> show status LIKE '%Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 32759656 |
| Qcache_hits             | 48678002 |
| Qcache_inserts          | 11871037 |
| Qcache_lowmem_prunes    | 7482829  |
| Qcache_not_cached       | 178368   |
| Qcache_queries_in_cache | 0        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+

mysql> SET GLOBAL query_cache_size = 32777216
[/cce]
query_cache_size: 是缓存占用内存的大小
query_cache_type:ON 表示启用了缓存

 

2. 由”备份-删除-恢复”机制带来的性能加速

注: 本方法对于”不活跃”的”海量数据”具有高效的访问性能

用户案例: 100万数据, 两周内活跃的仅为10万数据, 而且其余90%两周后再次活跃的概率也极低.

显而易见, 该方法适用于 海量数据存储, 但轻量级访问频率的数据库

 

  • 编写shell脚本 自动备份数据,然后删除已备份数据:
[cce]
#!/bin/bash
# author: Wang
# date: 2013-02-12
# find all the user needed to be backup

# define the Constants
Filename="/pathTo/BackupList.txt"
#Filename="/pathTo/BackupList.txt"

MediaRoot="/pathTo/media"
User="user"
Passwd="passwd"
Host="localhost"
DB="DBname"

# the current time
ctime="$(date +"%Y-%m-%d %H:%M:%S")"

# the backup time for Linux
#btime="$(date -d '-1 weeks' +%Y-%m-%d\ %H:%M:%S)"
# the backup time for Mac
# btime="$(date -v-1w +%Y-%m-%d\ %H:%M:%S)"
btime="$(date -v-2m +%Y-%m-%d\ %H:%M:%S)"
#btime="$(date -d '-2 weeks' +%Y-%m-%d\ %H:%M:%S)"

# the last backup time
# ltime="$(date -v-4m +%Y-%m-%d\ %H:%M:%S)"
# ltime="$(date -d '-4 months' +%Y-%m-%d\ %H:%M:%S)"

# get the list of backup ids
mysql -u$User -p$Passwd -h$Host -D $DB -e "select id from users where active=True and last_login < \"$btime\"" > $Filename

# for every id, output all the sql and delete it.
for id in `cat $Filename | grep -v id`
	do
	#echo $id
	if [ -e "$MediaRoot/$id" ];  then
		echo "dump for profile: "$id
		else
		echo "mkdir for profile: "$id
		mkdir "$MediaRoot/$id"
	fi

	if [ -e "$MediaRoot/$id/$id.sql" ];  then
		echo "$id.sql already exists"
	else
		mysqldump -u$User -p$Passwd -h$Host $DB paws --where "source_id in (select id from datasources where owner_id=$id)" --no-create-info --lock-all-tables > $MediaRoot/$id/$id.sql
		mysql -u$User -p$Passwd -h$Host -D $DB -e "DELETE from paws where source_id in (select id from datasources where owner_id=$id)" >> /tmp/log.txt
		mysql -u$User -p$Passwd -h$Host -D $DB -e "UPDATE users SET active=False where id=$id" >> /tmp/log.txt
	fi
done
[/cce]

 

  • 当用户2周后再次登录时, 使用python在其登录时调用恢复脚本
[cce]
if p.active == False:
        file_time = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
        restore_cmd = shlex.split("mysql -uuser -ppasswd -hlocalhost -D DBname -e 'SOURCE "+MediaRoot+"/"+str(p.id)+"/"+str(p.id)+".sql'")
        backup_cmd = shlex.split("mv "+MediaRoot+"/"+str(p.id)+"/"+str(p.id)+".sql "+MediaRoot+"/"+str(p.id)+"/"+str(p.id)+"_"+file_time+".sql")
        subprocess.Popen(restore_cmd).wait()
        subprocess.Popen(backup_cmd).wait()
        p.active = True
    p.last_login = now_time
    p.save()
[/cce]