my linux FAQ

來源:互聯網
上載者:User
  1. 用命令查詢系統是32位還是64位
    getconf LONG_BIT or getconf WORD_BIT
    例如:
    [root@sy02 /]# getconf LONG_BIT
    64

    file command
    例如:
    [root@sy02 /]# file /bin/ls
    /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), stripped

  2. 以Mb方式查看檔案
    ls -lh
  3. 過濾顯示檔案
    如只顯示xml結尾檔案: ls *.xml
  4. 查看磁碟剩餘空間: 
    df -hl
  5. ssh中下載檔案到本地 : 
    session sftp tab option設定本地目的路徑
    sz filename.filetype
  6. 兩個伺服器間拷貝檔案 
    scp -P 2188 user@ip:/path/file .
  7. 清空檔案: 
    true > des.txt
  8. svn提交檔案: 
    svn -m "message" filename 不寫"-m" 資訊出現無法提交現象,加上message是個好習慣!
  9. tar命令解壓檔案: 
    tar xzvf this.tar.gz "tar xzvf this.tar.gz ./" 這個命令網上資料說是解壓到本目錄 經實驗不可用
  10. 建立檔案命令 
    touch filename
  11. Windows和linux分行符號不一致問題 
    在 Linux 下的換行是 \n ,而在 Windows 下的換行是 \r\n 。不經過處理的話,兩者的檔案在顯示的時候會出現問題,比如 一個 Linux 的文字檔用 Windows 記事本開啟的時候不會換行格式很亂,一個 Windows 檔案在 Linux 下用 VI 開啟的話會 出現 ^M 字元等。
    解決方案:
    在 Linux 下使用 CMD : dos2unix and unix2dos
    dos2unix/unix2dos 包含在 Tofrodos 安裝包
    DOS text files traditionally have CR/LF (carriage return/line feed) pairs as their new line delimiters while
    Unix text files traditionally have LFs (line feeds) to terminate each line.
    Tofrodos comprises one program, "fromdos" alias "todos", which converts text files to and from these formats.
    Use "fromdos" to convert DOS text files to the Unix format, and "todos" to convert Unix text files to the DOS format.
    This functionality is also available via the dos2unix/unix2dos symlinks.
    UBUNTU 下安裝 dos2unix/unix2dos :
     $ sudo apt-get install tofrodos
  12. alias 目錄起別名,方便訪問
    vi ~/.bashrc
    alias scserver="cd /usr/local/netqin/boss/netqin/BOSS_SC_SERVICE/trunk/src"
    僅對某一使用者生效 重新進入ssh時生效
  13. 查看目前的目錄下檔案總數: 
    ls -l |grep "^-"|wc -l
  14. vi跳轉 
    出現了"home" "end"鍵不管用的情況
    到某行尾部 : "$" 命令
    到某行開頭 : "0" 命令
    正向滾動一整屏的命令是 CTRL-F (減去兩行)。反向的命令是 CTRL-B。
  15. vi刪除多行 : ndd 刪除以當前行開始的n行
  16. 在linux中刪除大量檔案時,直接用rm會出現:-bash: /bin/rm: 參數列表過長,的錯誤。 
    這時可以用find命令來結合使用。 例: 1、rm * -rf 改為: find . -name "*" | xargs rm -rf '*' 就行了。 2、rm test* -rf 改為: find . -name "test*" | xargs rm -rf "test*"
  17. 遠程登入其他伺服器 ssh -p 2188 username@ip
  18. 殭屍進程清除方法
    kill -18 PPID (PPID是其父進程)
    這個訊號是告訴父進程,該子進程已經死亡了,請收回分配給他的資源。
  19. 尋找檔案中的某字串
    grep -n "str" -r ./ 這是尋找目前的目錄下以及下轄子目錄下所有包含str字串的檔案
     grep -R abc * 在本目錄及其所有的子目錄下的檔案裡面尋找字串abc
  20. Linux系統手動安裝rz sz 軟體包
    wget http://freeware.sgi.com/source/rzsz/rzsz-3.48.tar.gz
    tar zxvf rzsz-3.48.tar.gz
    安裝 cd rzsz-3.48
    修改Makefile第四行OFLAG= -O 為 OFLAG= -O -DREGISTERED
    make posix (make 跟幾種配置選項 一般使用posix即可)
    cp rz sz /usr/bin/.
  21. linux 檔案切割
    split -b 10m filename.txt m必須是小寫 切割為10m 每個檔案
  22. 查看 程式開啟檔案
    伺服器tmp目錄磁碟滿了,直接清空或刪除檔案無效,空間仍然不能釋放,需要以root使用者方式 lsof命令查看程式佔用的檔案情況,找到該程式,kill掉重啟才可以。
    lsof(list open files)是一個列出當前系統開啟檔案的工具。在linux環境下,任何事物都以檔案的形式存在,通過檔案不僅僅可以訪問常規資料,還可以訪問網路連接和硬體。
     在終端下輸入lsof即可顯示系統開啟的檔案,因為 lsof 需要訪問核心記憶體和各種檔案,所以必須以 root 使用者的身份運行它才能夠充分地發揮其功能。
  23. secure CRT rz上傳檔案錯誤問題
    rz上傳的檔案大小和本地不對 ,檔案10M上傳後只剩了343bytes , 後來發現是 rz對話方塊裡不小心勾上了
    upload file as ascii,去掉後正常
  24. 安裝 Net::SNMP
    1) By CPAN (best)
    on command line, as root :
    [your_host]# perl -MCPAN -e shell
    cpan shell -- CPAN exploration and modules installation (v1.76)
    ReadLine support enabled
     cpan> install Net::SNMP
  25. ulimit修改最大開啟檔案數

    linux對使用者有預設的ulimit限制,而這個檔案可以配置使用者的硬配置和軟配置,硬配置是個上限。
    超出上限的修改就會出“不允許的操作”這樣的錯誤。
    在 /etc/security/limits.conf加上
    * soft noproc 10240
    * hard noproc 10240
    * soft nofile 10240
    * hard nofile 10240
    如果使用了nginx 最好也調整下裡邊的配置

  26. 負載分析
    使用 ps faux 查看當前進程狀態
    R – Running
    S – Sleeping
    D – Waiting for something
     如果某進程長時間為D ,說明該進程可能存在問題 ,導致其他進程請求處於等待狀態,系統負載將會急劇升高 ( http://www.andymillar.co.uk/blog/2006/12/24/linux-load-average-explained/ )
  27. 變更檔的所有者
    chown username somefile
    chown -R username somedir (目錄及子檔案)
    chown username:usergroup somefile
  28. linux 時間戳記
    http://tool.chinaz.com/Tools/unixtime.aspx
  29. linux中批量解壓檔案的方法-
    find -maxdepth 1 -name "*.bz2"|xargs -i tar xvjf {} 
  30. 一行代碼統計目錄下程式碼數 find . -name "*.java" | xargs wc -l
  31. 添加路由/sbin/route add -net 10.74.0.109 netmask 255.255.255.255 gw 172.16.54.1(需要添加的機器)                                                                (原生網關)
    /sbin/route 可以查看路由表
  32. crontab 位置
    CentOS為例,其真實的位置在:/var/spool/cron
  33. curl顯示連線時間  
    curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com
  34. 將MySQL中sql運行結果儲存到檔案
    方法一:在mysql>提示符中使用tee
    mysql> tee output.txt
    Logging to file 'output.txt'
    mysql> notee
    Outfile disabled.
    方法二:使用mysql命令列工具的--tee參數
    $mysql --tee=ot.txt
    Logging to file 'ot.txt'
    mysql>
    這回將所有的輸入和輸出內容都記錄到指定的檔案中(直到exit為止)。如果指定已經存在的檔案,則結果會附加到檔案中。
  35. 刪除超過三天的日誌
    find . -name "*.log.*" -mtime +3|xargs rm -rf
  36. ssh無密碼串連
    從A無密碼訪問B,則在A上產生ssh-keygen -t rsa -P '' -f /root/.ssh/id_dsa  追加到B的  /root/.ssh/iauthorized_keys
    需要開啟/etc/ssh/sshd_config :

    PubkeyAuthentication yes
    AllowUsers root
    AuthorizedKeysFile .ssh/authorized_keys

  37. linux java 安裝
    http://lelong.iteye.com/blog/349549  設定.bashrc不管用,需要設定 /etc/profile

    export JAVA_HOME=/root/jre1.6.0_31/
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    並source  /etc/profile

  38. nc使用
    http://www.huanxiangwu.com/477/linux-nc-netcat 詳解
    1. chat功能  主機 nc -l 1234  用戶端 nc $ip 1234   注意版本,有的要加參數 -lp
    2. telnet redis並加入命令  
       echo -e "get test \r\n" | nc localhost 6379   redis必須加 "\r\n"  echo需要加參數 -e,識別反斜線
    3. telnet memcache並加入命令
       printf "set zhangyan 0 0 5\r\n12345\r\n" | nc 127.0.0.1 11211
       printf "get zhangyan\r\n" | nc 127.0.0.1 11211
       incr   decr delete類似 
       來自: http://blog.s135.com/post/384/
  39. mac vim for python 設定
    需要安裝ctags gcc,gcc最好使用pkg版(我的是lion os),用tar封裝的各種問題  並且必須是with python編譯的才有自動補全功能  mvim
    ctags安裝後需要設定到vimrc中,不然無法產生成員列表 各種問題(ctags: illegal option )
    let Tlist_Ctags_Cmd = '/usr/local/bin/ctags'

    左右視窗切換Ctrl+ww 更多捷徑 http://www.clovery.org/tree-explorer-plugin-nerdtree-for-vim.html

    ctrl+x 緊接著 ctrl+o 顯示代碼補齊功能
    vim python  http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/   

  40. mysql int bigint範圍

    INT[(M)] [UNSIGNED] [ZEROFILL] 

    A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295

    BIGINT[(M)] [UNSIGNED] [ZEROFILL] 

    A large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615

     項目產生id範圍設定小了,並且使用parseInt方式解析,導致超過2147483647即出錯,解析方式改為parseLong,但目前還有隱患,資料庫的類型設定的是INT而不是BIGINT,超出的話就無法產生。

  41.  linux 安裝python  mysqldb出現錯誤  error: command 'gcc' failed with exit status 1 

    yum install gcc python-devel

  42. mac /home 目錄建立檔案夾 
    sudo vim /etc/auto_master  注釋掉home那一行,並重新啟動,不重啟僅執行sudo automount  經本機測試無效。
  43.  iptables連接埠編輯

    在Linux中設定防火牆,以CentOS為例,開啟iptables的設定檔:

     

    1. vi /etc/sysconfig/iptables  
    2.  

     

    通過/etc/init.d/iptables status命令查詢是否有開啟80連接埠,如果沒有可通過兩種方式處理:

    1.修改vi /etc/sysconfig/iptables命令添加使防火牆開放80連接埠

     

    1. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  
    2.  

     

    2.關閉/開啟/重啟防火牆

     

    1. /etc/init.d/iptables stop  
    2.  
    3. #start 開啟  
    4.  
    5. #restart 重啟  

     

     

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.