在寫網路程式,常常需要處理protocol的問題,http要看header and content對不對,可是一直用Wireshark sniffer也是很煩,幸好linux可以用很簡單的方式看到~~~^^
mkfifo proxypipe
cat proxypipe | nc -l 11111 | tee -a inflow | nc localhost 11112 | tee -a outflow 1>proxypipe
可以把input放在inflow,把output放在outflow~~~~^^
好用呦~~~^^
來源出自
http://smaftoul.wordpress.com/2009/05/13/netcat-as-a-logging-tcp-proxy/
星期三, 8月 11, 2010
星期二, 6月 22, 2010
[Linux] 當打錯command的時候怎麼辦....(#rm -rf /)
我個人常常打太快,常常rm的結果就是
#rm -rf /
慘了,電腦就得從灌了
Google了一下,發現真的沒甚麼方式可以recover,但皇天不負苦心人,可以用yum reinstall~~~~^^
#rpm -qa --qf '%{name}\n' | sort -u | xargs -i yum reinstall -y --verbose {}
#rm -rf /
慘了,電腦就得從灌了
Google了一下,發現真的沒甚麼方式可以recover,但皇天不負苦心人,可以用yum reinstall~~~~^^
#rpm -qa --qf '%{name}\n' | sort -u | xargs -i yum reinstall -y --verbose {}
星期三, 4月 07, 2010
星期三, 3月 17, 2010
[Linux] time 怎麼用 GNU verison
個人常常使用time來看program跑多久,man time的說明中,有--verbose,還有format,但
# time -v ps aux
這樣都會有顯示
-bash: -v: command not found
real 0m0.001s
user 0m0.000s
sys 0m0.001s
後來才知道原來是bash這個shell中,time的指令沒有GNU版本的功能
所以怎麼跑GNU版本的time呢?
就是..
# /usr/bin/time -v ps aux
水啦~~~~^^
# time -v ps aux
這樣都會有顯示
-bash: -v: command not found
real 0m0.001s
user 0m0.000s
sys 0m0.001s
後來才知道原來是bash這個shell中,time的指令沒有GNU版本的功能
所以怎麼跑GNU版本的time呢?
就是..
# /usr/bin/time -v ps aux
水啦~~~~^^
星期五, 1月 15, 2010
[Linux] vim 怎麼用?!
http://blog.vgod.tw/2009/12/08/vim-cheat-sheet-for-programmers/
這位部落客,把Vim常用的部份給圖形化了,這真是太屌了~~~^^
vim的指令(command)不會,上鳥哥太慢~~~^^
有PDF可以下載嚕~~~^^
這位部落客,把Vim常用的部份給圖形化了,這真是太屌了~~~^^
vim的指令(command)不會,上鳥哥太慢~~~^^
有PDF可以下載嚕~~~^^
星期三, 1月 13, 2010
[Linux] About "kernel: TCP: time wait bucket table overflow"
tcp_max_tw_buckets
Maximal number of timewait sockets held by system simultaneously. If this number is exceeded TIME_WAIT socket is immediately destroyed and warning is printed. This limit exists only to prevent simple DoS attacks, you must not lower the limit artificially, but rather increase it (probably, after increasing installed memory), if network conditions require more than default value (180000).
所以當你設小的時候呢,/var/log/messages就會出現一堆"kernel: TCP: time wait bucket table overflow",所以default值千萬別亂改,如果你的程式會有狠大的traffic
怎麼改回來呢?
在/etc/sysctl.conf加這行
net.ipv4.tcp_max_tw_buckets = 180000
還要
echo 180000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
Maximal number of timewait sockets held by system simultaneously. If this number is exceeded TIME_WAIT socket is immediately destroyed and warning is printed. This limit exists only to prevent simple DoS attacks, you must not lower the limit artificially, but rather increase it (probably, after increasing installed memory), if network conditions require more than default value (180000).
所以當你設小的時候呢,/var/log/messages就會出現一堆"kernel: TCP: time wait bucket table overflow",所以default值千萬別亂改,如果你的程式會有狠大的traffic
怎麼改回來呢?
在/etc/sysctl.conf加這行
net.ipv4.tcp_max_tw_buckets = 180000
還要
echo 180000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
星期二, 12月 29, 2009
[Linux]linux記憶體被吃光
相信很多人遇到用top看明明就沒有程式用記憶體很兇,但是記憶體還是被用光了
怎麼辦呢?
http://blog.chinaunix.net/u/27173/showart_467689.html
http://blog.yesican.tw/?p=912
其實就是linux為了提高disk IO的存取效率,所以有cache pagecache, dentries and inodes
怎麼free呢
#free
#sync
#echo 3 > /proc/sys/vm/drop_caches
#echo 0 > /proc/sys/vm/drop_caches
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to
free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >
/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects
are not freeable, the user should run sync(8) first.
-----------------------------------------------------------------
Update
http://ssorc.tw/rewrite.php/read-599.html
這篇對於linux memory usage有更詳細的說明
節錄buffer and cache的說明~~~^^
Free中的buffer和cache︰(它們都是佔用內存)︰
buffer : 作為buffer cache的內存,是塊設備的讀寫緩沖區
cache: 作為page cache的內存, 文件系統的cache
如果 cache 的值很大,說明cache住的文件數很多。如果頻繁訪問到的文件都能被cache住,那麼磁盤的讀IO bi會非常小。
Read more: http://ssorc.tw/rewrite.php/read-599.html#ixzz0gbxRjPrV
------------------------------------------------------------------------
[Update]
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
但這一篇就在說明ps很不準,別看ps的memory usage
怎麼辦呢?
http://blog.chinaunix.net/u/27173/showart_467689.html
http://blog.yesican.tw/?p=912
其實就是linux為了提高disk IO的存取效率,所以有cache pagecache, dentries and inodes
怎麼free呢
#free
#sync
#echo 3 > /proc/sys/vm/drop_caches
#echo 0 > /proc/sys/vm/drop_caches
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to
free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >
/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects
are not freeable, the user should run sync(8) first.
-----------------------------------------------------------------
Update
http://ssorc.tw/rewrite.php/read-599.html
這篇對於linux memory usage有更詳細的說明
節錄buffer and cache的說明~~~^^
Free中的buffer和cache︰(它們都是佔用內存)︰
buffer : 作為buffer cache的內存,是塊設備的讀寫緩沖區
cache: 作為page cache的內存, 文件系統的cache
如果 cache 的值很大,說明cache住的文件數很多。如果頻繁訪問到的文件都能被cache住,那麼磁盤的讀IO bi會非常小。
Read more: http://ssorc.tw/rewrite.php/read-599.html#ixzz0gbxRjPrV
------------------------------------------------------------------------
[Update]
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
但這一篇就在說明ps很不準,別看ps的memory usage
星期一, 12月 21, 2009
[Linux] logrotate setup and test
如果要利用logrotate幫你rotate log,請在加入一個file在/etc/logrotate.d/下面
# vim /etc/logrotate.d/aaa
content:
/var/log/aaa {
daily
ifempty
rotate 7
}
# service syslog restart
如何知道設定對不對,請打
# logrotate /etc/logrotate.conf
至於有那些可以設定,請man logrotate嚕
# vim /etc/logrotate.d/aaa
content:
/var/log/aaa {
daily
ifempty
rotate 7
}
# service syslog restart
如何知道設定對不對,請打
# logrotate /etc/logrotate.conf
至於有那些可以設定,請man logrotate嚕
[linux] Too many open files
想知道自己某個程式的開啟檔案數,可以用以下的指令察看:
lsof -p [pid of your process] | wc -l
如果想調大檔案開啟數,可以調整fs.file-max這個核心參數。如果memory夠大,可以將此值加大到65536,或是更大:調整的方式:
在/etc/rc.local中加入echo 65536 > /proc/sys/fs/file-max
或是
在sysctl.conf中加入fs.file-max=65536
不過單修改核心參數只能加大系統核心的上限,Linux本身還有針對每個user的每個程式本身的限制,因此還需要編輯/etc/security/limits.conf,並加入
* soft nofile 4096
* hard nofile 4096
修改完畢之後,重新開機即可!
lsof -p [pid of your process] | wc -l
如果想調大檔案開啟數,可以調整fs.file-max這個核心參數。如果memory夠大,可以將此值加大到65536,或是更大:調整的方式:
在/etc/rc.local中加入echo 65536 > /proc/sys/fs/file-max
或是
在sysctl.conf中加入fs.file-max=65536
不過單修改核心參數只能加大系統核心的上限,Linux本身還有針對每個user的每個程式本身的限制,因此還需要編輯/etc/security/limits.conf,並加入
* soft nofile 4096
* hard nofile 4096
修改完畢之後,重新開機即可!
星期五, 12月 11, 2009
[Linux] How to setup reposity on Centos and RHEL.
http://doc.linuxpk.com/58088.html
我寫下簡單的command
1. mkdir /centos.source/5.3/
mkdir /RHEL.source/5.2/
2. centos
15 5 * * * rsync -aqzH --delete msync.centos.org::CentOS/5.3 /cent.source/
RHEL
cp RHEL_source /RHEL.source/5.2/
3. centos
vim /etc/httpd/conf.d/cent.mirror.conf
content:
Alias /CentOS "/cent.source/"
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
RHEL
vim /etc/httpd/conf.d/RHEL.mirror.conf
content:
Alias /RHEL "/RHEL.source/"
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
4. RHEL
mv /RHEL.source/5.2/Server/repodata /RHEL.source/5.2/Server/backup_repodata
yum install createrepo 或 rpm -ivh createrepo-0.4.4-2.fc6.noarch.rpm
mkdir /RHEL.source/5.2/Server/repodata
cd /RHEL.source/5.2/Server/repodata
createrepo -g /RHEL.source/5.2/Server/backup_repodata/comps-rhel5-server-core.xml /RHEL.source/5.2/Server
5. centos
cd /etc/yum.repos.d/
vim CentOS-Base.repo
content:
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://xxxxxxx/CentOS/5.3/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
RHEL
cd /etc/yum.repos.d/
vim rhel-5.2.repo
content:
[rhel-5.2]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=http://xxxxxxx/RHEL/5.2/Server
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
6. wget http://xxxxxxx/CentOS/5.3/os/$basearch/
wget http://xxxxxxx/RHEL/5.2/Server
Done
我寫下簡單的command
1. mkdir /centos.source/5.3/
mkdir /RHEL.source/5.2/
2. centos
15 5 * * * rsync -aqzH --delete msync.centos.org::CentOS/5.3 /cent.source/
RHEL
cp RHEL_source /RHEL.source/5.2/
3. centos
vim /etc/httpd/conf.d/cent.mirror.conf
content:
Alias /CentOS "/cent.source/"
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
RHEL
vim /etc/httpd/conf.d/RHEL.mirror.conf
content:
Alias /RHEL "/RHEL.source/"
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
4. RHEL
mv /RHEL.source/5.2/Server/repodata /RHEL.source/5.2/Server/backup_repodata
yum install createrepo 或 rpm -ivh createrepo-0.4.4-2.fc6.noarch.rpm
mkdir /RHEL.source/5.2/Server/repodata
cd /RHEL.source/5.2/Server/repodata
createrepo -g /RHEL.source/5.2/Server/backup_repodata/comps-rhel5-server-core.xml /RHEL.source/5.2/Server
5. centos
cd /etc/yum.repos.d/
vim CentOS-Base.repo
content:
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://xxxxxxx/CentOS/5.3/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
RHEL
cd /etc/yum.repos.d/
vim rhel-5.2.repo
content:
[rhel-5.2]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=http://xxxxxxx/RHEL/5.2/Server
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
6. wget http://xxxxxxx/CentOS/5.3/os/$basearch/
wget http://xxxxxxx/RHEL/5.2/Server
Done
星期四, 11月 12, 2009
[Linux] how to separate array in shell script.
通常linux based的系統,shell script中的array都是用空白,或是tab來當分隔符號,但如果要換成別的時候該怎麼辦呢?
在前面加IFS=,
就會是用逗號分隔嚕~~~~^^
例子如下
#!/bin/sh
IFS=,
string="aaa,bbb,ccc"
for str in $string
do
echo $str
done
在前面加IFS=,
就會是用逗號分隔嚕~~~~^^
例子如下
#!/bin/sh
IFS=,
string="aaa,bbb,ccc"
for str in $string
do
echo $str
done
星期二, 10月 20, 2009
[Linux] shellscript如何用用array在for loop
sources=(`cat test.log`)
destinations=(`cat test.1.log`)
for ((i=0; i<${#sources[@]}; i++))
do
echo $i
#echo ${sources[$i]} ${destinations[$i]}
done
要同時跑兩個array的時候蠻方便的,還有要把cat的資料轉成array就是加括弧嚕。
destinations=(`cat test.1.log`)
for ((i=0; i<${#sources[@]}; i++))
do
echo $i
#echo ${sources[$i]} ${destinations[$i]}
done
要同時跑兩個array的時候蠻方便的,還有要把cat的資料轉成array就是加括弧嚕。
星期一, 5月 04, 2009
[Linux]find如何找多久之前的檔案
常常要砍log的檔案,不然硬碟要爆啦,如果log並沒有時間上的一句,這時候就需要用find
#find / -mtime +30 -type f
這是找出30天之前的檔案,接下來如果要砍檔就是加xargs
#find / -mtime +30 -type f | xargs -i rm {}
那如果是-30呢?
#find / -mtime -30 -type f
就是從現在到30天以內的檔案找出來嚕
#find / -mtime +30 -type f
這是找出30天之前的檔案,接下來如果要砍檔就是加xargs
#find / -mtime +30 -type f | xargs -i rm {}
那如果是-30呢?
#find / -mtime -30 -type f
就是從現在到30天以內的檔案找出來嚕
星期一, 12月 15, 2008
[Linux] 如何監控process的CPU使用率
http://www.javaworld.com.tw/jute/post/view?bid=5&id=123936&tpg=1&ppg=1&sty=1&age=0#123936
目前我找到的有
ps u -p PID
ps aux | grep PID
top -b -pPID
這是可以觀看單一個process的CPU使用率
如果是要看電腦主機 系統本身的,就是
top -b
cat /proc/stat
vmstat 1 10 //每隔一秒顯示一行,但只會持續十秒
sar -u 1 5 //統計目前主機 CPU 狀態,每秒一次,共計五次
sar -r 1 5 //統計目前主機 MEM 狀態,每秒一次,共計五次
/proc/stat欄位說明:
user: normal processes executing in user mode
nice: niced processes executing in user mode
system: processes executing in kernel mode
idle: twiddling thumbs
iowait: waiting for I/O to complete
irq: servicing interrupts
softirq: servicing softirqs
The "intr" line gives counts of interrupts serviced since boot time, for each of the possible system interrupts. The first column is the total of all interrupts serviced; each subsequent column is the total for that particular interrupt.
The "ctxt" line gives the total number of context switches across all CPUs.
The "btime" line gives the time at which the system booted, in seconds since the Unix epoch.
The "processes" line gives the number of processes and threads created, which includes (but is not limited to) those created by calls to the fork() and clone() system calls.
The "procs_running" line gives the number of processes currently running on CPUs.
The "procs_blocked" line gives the number of processes currently blocked, waiting for I/O to complete.
copied from the kernel documentation of the /proc filesystem
vmstat欄位說明:
r 等著存取處理器的可執行程序之數量
b 處於不可執行睡眠狀態下的程序數目
與記憶體相關的欄位
swpd 已使用的虛擬記憶體大小
free 未使用的記憶體大小
buff 用來當作緩衝區的記憶體大小
cache 用來當作分頁快取的記憶體大小
與 swap 有關的欄位
si 從磁碟讀入,置換到記憶體的資料大小
so 從記憶體置換到磁碟的資料大小
與 I/O 有關的欄位
bi 送到區塊(block)裝置的區塊數目
bo 從區塊裝置讀入的區塊數目
與系統相關的欄位
in 每秒的中斷數目
cs 每秒的環境切換(context switch)數目
與 CPU 相關的欄位
us CPU 處理使用者等級程式碼的時間,以百分比表示
sy CPU 處理系統等級程式碼的時間,以百分比表示
id CPU 的閒置時間,以百分比表示
wa I/O 等待
目前我找到的有
ps u -p PID
ps aux | grep PID
top -b -pPID
這是可以觀看單一個process的CPU使用率
如果是要看電腦主機 系統本身的,就是
top -b
cat /proc/stat
vmstat 1 10 //每隔一秒顯示一行,但只會持續十秒
sar -u 1 5 //統計目前主機 CPU 狀態,每秒一次,共計五次
sar -r 1 5 //統計目前主機 MEM 狀態,每秒一次,共計五次
/proc/stat欄位說明:
user: normal processes executing in user mode
nice: niced processes executing in user mode
system: processes executing in kernel mode
idle: twiddling thumbs
iowait: waiting for I/O to complete
irq: servicing interrupts
softirq: servicing softirqs
The "intr" line gives counts of interrupts serviced since boot time, for each of the possible system interrupts. The first column is the total of all interrupts serviced; each subsequent column is the total for that particular interrupt.
The "ctxt" line gives the total number of context switches across all CPUs.
The "btime" line gives the time at which the system booted, in seconds since the Unix epoch.
The "processes" line gives the number of processes and threads created, which includes (but is not limited to) those created by calls to the fork() and clone() system calls.
The "procs_running" line gives the number of processes currently running on CPUs.
The "procs_blocked" line gives the number of processes currently blocked, waiting for I/O to complete.
copied from the kernel documentation of the /proc filesystem
vmstat欄位說明:
r 等著存取處理器的可執行程序之數量
b 處於不可執行睡眠狀態下的程序數目
與記憶體相關的欄位
swpd 已使用的虛擬記憶體大小
free 未使用的記憶體大小
buff 用來當作緩衝區的記憶體大小
cache 用來當作分頁快取的記憶體大小
與 swap 有關的欄位
si 從磁碟讀入,置換到記憶體的資料大小
so 從記憶體置換到磁碟的資料大小
與 I/O 有關的欄位
bi 送到區塊(block)裝置的區塊數目
bo 從區塊裝置讀入的區塊數目
與系統相關的欄位
in 每秒的中斷數目
cs 每秒的環境切換(context switch)數目
與 CPU 相關的欄位
us CPU 處理使用者等級程式碼的時間,以百分比表示
sy CPU 處理系統等級程式碼的時間,以百分比表示
id CPU 的閒置時間,以百分比表示
wa I/O 等待
星期一, 12月 01, 2008
[Linux] message的error code 定義
有使用linux的人都知道有error,可以去看
/var/log/message這個檔案,查看error message
但會發現最後面有一個error code,卻搞不懂什麼意思
可以去/usr/include/asm-generic/errno-base.h
這個檔案看linux error code的定義,很方便吧~~~~^^
/var/log/message這個檔案,查看error message
但會發現最後面有一個error code,卻搞不懂什麼意思
可以去/usr/include/asm-generic/errno-base.h
這個檔案看linux error code的定義,很方便吧~~~~^^
星期二, 11月 18, 2008
星期二, 10月 28, 2008
星期一, 10月 13, 2008
[Linux]查看RedHat Linux的版本
#cat /etc/redhat-release
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
#cat /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
Kernel \r on an \m
#uname -a
Linux URL-FTP 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux
記錄一下,不然總是找不到RHEL的版本
#############################################
update另一個更好的指令,真是好用阿~~~~^____^
#lsb_release -a
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.2 (Tikanga)
Release: 5.2
Codename: Tikanga
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
#cat /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
Kernel \r on an \m
#uname -a
Linux URL-FTP 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux
記錄一下,不然總是找不到RHEL的版本
#############################################
update另一個更好的指令,真是好用阿~~~~^____^
#lsb_release -a
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.2 (Tikanga)
Release: 5.2
Codename: Tikanga
星期二, 9月 30, 2008
[Linux] cat然後接grep
http://blog.snese.net/archives/20070126.html
中文版
http://www-128.ibm.com/developerworks/aix/library/au-badunixhabits.html#nine
英文版
千萬別再用cat然後加grep
請直接grep pattern files
速度差很多
"time grep pattern files"
可以得到這次grep的速度
中文版
http://www-128.ibm.com/developerworks/aix/library/au-badunixhabits.html#nine
英文版
千萬別再用cat然後加grep
請直接grep pattern files
速度差很多
"time grep pattern files"
可以得到這次grep的速度
[Linux]find與xargs的結合
http://marrtw.blogspot.com/2008/06/find-and-remove-files.html
這篇有說明如何用find找出符合條件的檔案,然後remove
其實常常需要用到,我列幾個例子
find ./ -type f -name "*.html" | xargs -i rm {}
找出*.html的檔案,xargs -i的意思是把這些檔案放在{}裡
但有時候file name含有空白(space)、換行(\r\n or \n)等符號
就得改成
find ./ -type f -name "*.html" -print0 | xargs -i -0 rm {}
----------------------------------------------------------
Update:
看到SUMMARY - find -exec vs xargs
一定要說明一下的~~~~^__^
用xargs比-exec 有效率(more efficient)
-exec: 是每find到一筆資料(data)就執行
xargs: 是收集了20~50筆資料(data)在執行
另外要提醒的是如果stdin是empty,xargs要加 -r
-----------------------------------------------------------
Update:
另外如果不想找出來的檔案有./
則要在find後面直接接目錄名稱
find dir -type f
這篇有說明如何用find找出符合條件的檔案,然後remove
其實常常需要用到,我列幾個例子
find ./ -type f -name "*.html" | xargs -i rm {}
找出*.html的檔案,xargs -i的意思是把這些檔案放在{}裡
但有時候file name含有空白(space)、換行(\r\n or \n)等符號
就得改成
find ./ -type f -name "*.html" -print0 | xargs -i -0 rm {}
----------------------------------------------------------
Update:
看到SUMMARY - find -exec vs xargs
一定要說明一下的~~~~^__^
用xargs比-exec 有效率(more efficient)
-exec: 是每find到一筆資料(data)就執行
xargs: 是收集了20~50筆資料(data)在執行
另外要提醒的是如果stdin是empty,xargs要加 -r
-----------------------------------------------------------
Update:
另外如果不想找出來的檔案有./
則要在find後面直接接目錄名稱
find dir -type f
訂閱:
文章 (Atom)