Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, January 19, 2013

grep + find example 範例

find ./ -name \*.py -exec grep -wl nltk {} \;

Find all the files which contain 'nltk' in their content in current directory.

找出當前目錄下所有內容含有'nltk'這個字的python檔案


如何紀錄linux終端的操作日誌


在linux終端下,為方便檢查操作中可能出現的錯誤,以及避免螢幕滾屏的限制,我們可以把操作日誌記錄下來。常用的工具有 screen,script,以及tee等。
  1. screen — screen manager with VT100/ANSI terminal emulatio
  > screen -L
  這裡是我們的操作
  > exit
  結束後會生成一個類似這樣的檔案名: screenlog.0
  > more screenlog.0
  這樣可以查看你剛才進行的操作,發現可能存在的問題
  2. script  — make typescript of terminal session
  > script
  我們的操作
  > exit
  生成一個這樣的檔:typescript
  > more typescript
  這裡查看我們進行的操作
  3. tee –  read from standard input and write to standard output and files
  這個命令可以讀取終端輸入輸出到終端或者檔中,有時候可以用來記錄make等命令可能產生的輸出到終端的大量內容輸出到檔中去。這樣可以方便記 錄這些命令的日誌。
  > make 2>&1 | tee make.log
  當然,我們也可以直接重定向到一個檔中
  > make > make.log
  PS: 2>&1是為了記錄錯誤日誌
  if you want to filter the control symbols, try to use the "col" command like this:
  $ cat screenlog.0 | col -b > screenlog
  or
  $ cat typescript | col -b > scriptlog
  還有一個比較好用的命令tail -f
  比如把輸出定向到text
  ./run.sh>text
  tail -f  text 這個命令會follow輸出定向檔的內容


ImportError: cannot import name urandom


I use Ubuntu11.10 and python2.7.2+ .
File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

After I checked os.py , I found that there is no module named "urandom" in it:
$ python
Python 2.7.2+ (default, Aug 16 2011, 07:24:41)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.urandom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'urandom'

So I reinstall python2.7.2 :
$ ./configure
$ make
$ sudo make install

then it fixed the error!


vim 縮排


在 VIM 中撰寫程式,可以利用「gg」指令將游標移到視窗最上方、利用「v」切換到選取模式、再用「G」將游標移到檔案尾端 (即達到全選的功能),最後按「=」,VIM 便會幫你的程式做自動對齊

另外也可在選取後按
Shift+< : 將選取的程式碼往左縮排一次
Shift+> : 將選取的程式碼往右縮排一次
或是按一個數字後再按Shift+< , 就可做指定次數的縮排

Unable to find a medium containing a live file system


When I was trying to install linux(ubuntu 11.10) from live CD, it showed "Unable to find a medium containing a live file system"
 
solution:
1.Pull out the ATA which linked to CD-ROM and hard-disks, then exchange the ATA head which was connected to your CD-ROM with the ATA head which was connected to hard-disks. In other words, use different heads to connect them to CD-ROM and hard-disks.

2.Restart the computer, done!




用linux(ubuntu)光碟開機想重灌時出現 Unable to find a medium containing a live file system
 
我的解法:
1.把連接光碟機和硬碟的ATA拔掉, 把ATA原本插光碟機的接頭(長的那端)插在硬碟上, 原本插光碟機的ATA接頭(較短那端)插在硬碟上, 總之把ATA接頭在光碟機和硬碟間多換幾次試試
2.重新開機, 就解決了!

修改Ubuntu 11.10 ssh登入畫面


在一般linux(例:CentOS)要修改ssh登入畫面,只要修改 /etc/motd 這個檔的內容就行。
但Ubuntu(11.10)較不同,
修改/etc/motd後下次登入還是會顯示同樣畫面。(因/etc/motd只是指向/var/run/motd的symbolic link)

最簡單修改Ubuntu11.10登入畫面的方法是:
1.在/etc/新增一個檔案motd.static
   sudo vi /etc/motd.static
   接著自己增添motd.static的內容,增添的內容就是新的登入畫面(但無法加入\d等顯示系統資訊的指令)

2.讓/etc/motd改指向/etc/motd.static
 sudo ln -f -s /etc/motd.static /etc/motd
  
3.重新登入,登入顯示訊息就會變成 /etc/motd.static 的內容 




若想修改顯示的系統訊息(如kernel版本, 時間),
則可修改/etc/update-motd.d/下的檔案,
例如在沒有更動/etc/motd的情況下,把/etc/update-motd.d/00-header的執行權限取消,
則登入後就不會顯示"Welcome to Ubuntu 11.10 (GNU/Linux 3.x.x-xx-generic-pae...)"

Ubuntu linux cat 指令合併檔案


用法:

cat file.01 file.02 file.03 > file.merge
(將 file.01  file.02  file.03 合併到 file.merge)

另外,如果要合併的檔名有空白字元的話,那就要檔名前後加上 " "

cat "arthur today.001"  "arthur today.001"  "arthur today.004" > arthurtoday.avi



Reference:
http://www.arthurtoday.com/2010/10/ubuntu-linux.html

Ubuntu上的Apache設定檔 httpd.conf


我的Ubuntu11.10是用Apache2,
但找不到httpd.conf這個檔,後來發現Ubuntu的Apache2設定檔不是httpd.conf。
Apache2的設定檔是/etc/apache2/apache2.conf
但若要調整網站設定(ServerName, DocumentRoot等) ,則應修改/etc/apache2/sites-available/default

Reference : http://www.arthurtoday.com/2009/11/ubuntu-httpdcon.html