顯示具有 [Linux 命令] 標籤的文章。 顯示所有文章
顯示具有 [Linux 命令] 標籤的文章。 顯示所有文章

2020年8月22日 星期六

[Linux 文章收集] Diff 和 Patch 的簡易操作使用筆記

 Source From Here

Preface
diff + patch 很方便, 在此紀錄幾個基本用法:
1. diff 產生 patch 檔
2. 將 patch 寫入
3. 移除此次 patch 的內容

Diff 和 Patch 的簡易操作使用筆記

產生檔案比較 patch 檔案
// Diff 產生 patch, 檔案, 資料夾都可以
// 或 diff -Naur dir1 dir2 > project.patch # Diff 產生 patch

# diff -Naur file1 file2 > project.patch

// 將 patch 寫入
# patch -p0 < project.patch

// 再做一次會詢問是否自動加上 -R 來移除之前 patch 寫入的(恢復回上一步的資料)
# patch -p0 < project.patch

// 移除回復修改 (同上一步驟, 此次就是確定要移除上次的 project.patch)
# patch -R -p0 < project.patch

兩個資料夾(檔案也可以使用此步驟) diff, patch 案例
先建立 dir1, dir2 (一樣內容), 修改 dir2, 然後產生 diff 後, patch 寫回 dir1.
# mkdir dir1
# touch dir1/a.txt
# touch dir1/b.txt
# mkdir dir2
# cp dir1/* dir2/

// 隨便寫
# vim dir2/a.txt
# vim dir2/b.txt

// 產生 diff patch
# diff -Naur dir1 dir2 > dir.patch

// dir1 就會寫入此 patch, 此時與 dir2 會是相同的內容
# patch -p0 < dir.patch

// 會詢問是否要移除之前 patch 寫入的, 可以先 'n' 都否決, 用下個指令恢復
# patch -p0 < dir.patch

// 移除回復修改 (同上一步驟, 此次就是確定要移除 dir.patch 的內容)
# patch -R -p0 < dir.patch

兩個檔案 diff, patch 案例
# echo "Hello John" > file1
# echo "Hello Peter.\nSee you" > file2

// -u, -U NUM, --unified[=NUM]: output NUM (default 3) lines of unified context
// -r, --recursive: recursively compare any subdirectories found
// -N, --new-file: treat absent files as empty
# diff -urN file1 file2 > f.patch
# cat f.patch
--- file1 2020-08-22 12:37:18.417081806 +0000
+++ file2 2020-08-22 12:37:28.580920038 +0000
@@ -1 +1 @@
-Hello John
+Hello Peter.\nSee you


# patch < f.patch // 輸入要 patch 的檔案位置 (假設輸入 file1, 此時 file1 = file2)
patching file file1

// Check content of file1
# cat file1
Hello Peter.\nSee you

# patch < f.patch // 輸入要 patch 的檔案位置 (假設輸入 file1, 此時會詢問是否要移除 f.patch 的內容)
patching file file1
Reversed (or previously applied) patch detected! Assume -R? [n]
 y

# cat file1
Hello John

Supplement
* 吉米花生醬: 原始碼 diff and patch
* patch 與 diff 的簡單應用
* patch 跟 diff 用法 @ 工作小錦囊 :: 隨意窩 Xuite日誌
* Jserv's blog: quilt - 強大的 patch 管理工具

2017年10月31日 星期二

[Linux 文章收集] iotop - 監看每支程式讀寫硬碟的情形

Source From Here 
Preface 
阿舍之前有介紹過用 NetHogs 來檢查每支在跑的程式各使用了多少的頻寬,這是 Server 端比較會用到的工具,而這次阿舍要介紹的,是個人電腦比較會用到的東西,就是用來查看看,是誰一直、一直在讀寫硬碟哩 ! 阿舍會用到這種工具的情況,就是當電腦沒在做什麼事的時候,硬碟的燈還一直的給它亮著或是閃不停的時候哩 ! 
 

Install iotop 
阿舍在網路上找到有幾支程式可用,阿舍大概的比較了一下,最後選了這個 iotop 來用,阿舍選 iotop 是因為它很簡單易用和易看,用法和 top 指令很像,而且,在 Ubuntu 內建的 PPA 就有放 iotop,所以,只要用下面這一行的 apt-get 指令來安裝就可以了。 
# sudo apt-get install iotop


iotop Usage 
在安裝好之後,只要用下面這一行來啟動就可以了,要注意的是,一定要用 sudo 來執行,否則,會因為權限問題而出現錯誤訊息的,iotop 啟動後的顯示畫面和 top 指令幾乎是一模一樣的,最大的差別是在畫面中間的那二行是顯示該程式每秒讀 (DISK READ) 和寫 (DISK WRITE) 硬碟的量,而不是顯示記憶體的使用資訊。 
# sudo iotop

如果想要顯示成累計讀寫硬碟的資料量的話,就需要像下面這行一樣,在 iotop 指令後面加上 「-a 」選項。 
# sudo iotop -a

除了「-a」選項之外,iotop 還有幾個阿舍覺得比較實用的選項,所以,就來說明一下,下面就是這幾個選項的用法及用途,而要說明的是,這下面這幾個選項和前面提到的「-a」選項都是可以合併使用的,不需要一次只能使用一個選項哩 ! 如果加了「-o」選項,那麼 iotop就只會顯示正在讀寫硬碟的程式,如果沒有程式在讀寫的話,就不會出現任何的程式哩 ! 
# sudo iotop -o

「-d」 選項是用來設定 iotop 更新的頻率,預設是 1 秒鐘,如果要改成 10 秒更新一次的話,就要用下面這一行指令來調整。 
# sudo iotop -d 10

如果想要只針對某一支正在執行中的程式來監看的話,那麼就會需要用「-p」選項加上要監看的程式的 PID,這樣 iotop 就只會出現這個 PID 的硬碟讀取情形哩; 用「-p」選項可以監看特定程式,而使用「-u」選項的話,則是可以監看特定使用者所啟動的程式對硬碟的讀取情形,但是,和「-p」選項不同的是,「-u」選項後面要帶入的曞人使用者的帳號名稱而不是代碼: 
# sudo iotop -u arthuryu

最後,阿舍要說明的是 iotop 的批次模式 (batch),iotop 的批次模式會將程式讀寫硬碟的資料依序送到指定的輸出裝置,所以,批次模式適合用來將 iotop 所產出的結果寫到檔案來供程式或處理時使用,而要啟用批次模式的話,需要使用「-b」選項,然後,可以搭配「-t」選項來要求在產出的資料上,加註日期和時間,同時,也能使用「-n」選項來要求產出幾次資料後停止 

舉例來說,如果要讓 iotop 以批次方式產出三次資料到 io.log 檔案,然後,只產出正在讀取硬碟中的程式,並且,在產出的資料上加時間的話,那麼 iotop 就要用下一面這一行的方式來執行: 
# sudo iotop -bot -n 3 > io.log


Supplement 
* Monitor Disk I/O activity using IOTOP & IOSTAT commands

2017年5月18日 星期四

[Linux 常見問題] Show only filename, date and size

Source From Here
Question
How can I in linux to get a listing of filenames date and size only. I don't need to see the other info such as owner or permission. Is this possible?

How-To
Command stat will help:
# ls -hl test.sh
-rwxr-xr-x 1 root root 120 May 13 10:45 test.sh

// -c, --format=FORMAT: use the specified FORMAT instead of the default
// Format %y: Time of last modification
// Format %s: Total size, in bytes
// Format %n: File name

# stat -c "%n %s %y" test.sh
test.sh 120 2017-05-13 10:45:35.484211409 +0800


2016年3月14日 星期一

[Linux 文章收集] 15 Linux lsof Command Examples (Identify Open Files)

Source From Here
Introduction
lsof stands for List Open Files. It is easy to remember lsof command if you think of it as “ls + of”, where ls stands for list, and of stands for open files. It is a command line utility which is used to list the information about the files that are opened by various processes. In unix, everything is a file, ( pipes, sockets, directories, devices, etc.). So by using lsof, you can get the information about any opened files.

Simply typing lsof will provide a list of all open files belonging to all active processes.
# lsof | less 



By default One file per line is displayed. Most of the columns are self explanatory. We will explain the details about couple of cryptic columns (FD and TYPE). FD – Represents the file descriptor. Some of the values of FDs are:
* cwd – Current Working Directory
* txt – Text file
* mem – Memory mapped file
* mmap – Memory mapped device
* NUMBER – Represent the actual file descriptor. The character after the number i.e ‘1u’, represents the mode in which the file is opened. r for read, w for write, u for read and write.

TYPE – Specifies the type of the file. Some of the values of TYPEs are,
* REG – Regular File
* DIR – Directory
* FIFO – First In First Out
* CHR – Character special file

For a complete list of FD & TYPE, refer man lsof.

Usage Examples

List processes which opened a specific file
You can list only the processes which opened a specific file, by providing the filename as arguments.
# lsof /var/log/syslog
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd 488 syslog 1w REG 8,1 1151 268940 /var/log/syslog


List opened files under a directory
You can list the processes which opened files under a specified directory using ‘+D’ option. +D will recurse the sub directories also. If you don’t want lsof to recurse, then use ‘+d’ option.
# lsof +D /var/log/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd 488 syslog 1w REG 8,1 1151 268940 /var/log/syslog
rsyslogd 488 syslog 2w REG 8,1 2405 269616 /var/log/auth.log
console-k 144 root 9w REG 8,1 10871 269369 /var/log/ConsoleKit/history


List opened files based on process names starting with
You can list the files opened by process names starting with a string, using ‘-c’ option. -c followed by the process name will list the files opened by the process starting with that processes name. Multiple -c is accepted.
# lsof -c ssh -c init
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root txt REG 8,1 124704 917562 /sbin/init
init 1 root mem REG 8,1 1434180 1442625 /lib/i386-linux-gnu/libc-2.13.so
init 1 root mem REG 8,1 30684 1442694 /lib/i386-linux-gnu/librt-2.13.so
...
ssh-agent 1528 lakshmanan 1u CHR 1,3 0t0 4369 /dev/null
ssh-agent 1528 lakshmanan 2u CHR 1,3 0t0 4369 /dev/null
ssh-agent 1528 lakshmanan 3u unix 0xdf70e240 0t0 10464 /tmp/ssh-sUymKXxw1495/agent.1495


List processes using a mount point
Sometime when we try to umount a directory, the system will say “Device or Resource Busy” error. So we need to find out what are all the processes using the mount point and kill those processes to umount the directory. By using lsofwe can find those processes.
# lsof /home
Or
# lsof +D /home/


List files opened by a specific user
In order to find the list of files opened by a specific users, use ‘-u’ option.
# lsof -u lakshmanan
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
update-no 1892 lakshmanan 20r FIFO 0,8 0t0 14536 pipe
update-no 1892 lakshmanan 21w FIFO 0,8 0t0 14536 pipe
bash 1995 lakshmanan cwd DIR 8,1 4096 393218 /home/lakshmanan

Sometimes you may want to list files opened by all users, expect some 1 or 2. In that case you can use the ‘^’ to exclude only the particular user as follows:
# lsof -u ^lakshmanan
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rtkit-dae 1380 rtkit 7u 0000 0,9 0 4360 anon_inode
udisks-da 1584 root cwd DIR 8,1 4096 2 /

The above command listed all the files opened by all users, expect user ‘lakshmanan’.

List all open files by a specific process
You can list all the files opened by a specific process using ‘-p’ option. It will be helpful sometimes to get more information about a specific process.
# lsof -p 1753
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 1753 lakshmanan cwd DIR 8,1 4096 393571 /home/lakshmanan/test.txt
bash 1753 lakshmanan rtd DIR 8,1 4096 2 /
bash 1753 lakshmanan 255u CHR 136,0 0t0 3 /dev/pts/0
...


Kill all process that belongs to a particular user
When you want to kill all the processes which has files opened by a specific user, you can use ‘-t’ option to list output only the process id of the process, and pass it to kill as follows:
# kill -9 `lsof -t -u lakshmanan`

The above command will kill all process belonging to user ‘lakshmanan’, which has files opened. Similarly you can also use ‘-t’ in many ways. For example, to list process id of a process which opened /var/log/syslog can be done by:
# lsof -t /var/log/syslog
489

Talking about kill, did you know that there are 4 Ways to Kill a Process?

Combine more list options using OR/AND
By default when you use more than one list option in lsof, they will be ORed. For example:
# lsof -u lakshmanan -c init
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 8,1 4096 2 /
init 1 root txt REG 8,1 124704 917562 /sbin/init
bash 1995 lakshmanan 2u CHR 136,2 0t0 5 /dev/pts/2
bash 1995 lakshmanan 255u CHR 136,2 0t0 5 /dev/pts/2
...

The above command uses two list options, ‘-u’ and ‘-c’. So the command will list process belongs to user ‘lakshmanan’ as well as process name starts with ‘init’. But when you want to list a process belongs to user ‘lakshmanan’ and the process name starts with ‘init’, you can use ‘-a’ option.
# lsof -u lakshmanan -c init -a

The above command will not output anything, because there is no such process named ‘init’ belonging to user ‘lakshmanan’.

Execute lsof in repeat mode
lsof also support Repeat mode. It will first list files based on the given parameters, and delay for specified seconds and again list files based on the given parameters. It can be interrupted by a signal. Repeat mode can be enabled by using ‘-r’ or ‘+r’. If ‘+r’ is used then, the repeat mode will end when no open files are found. ‘-r’ will continue to list,delay,list until a interrupt is given irrespective of files are opened or not.
# lsof -u lakshmanan -c init -a -r5
=======
...
=======
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
inita.sh 2971 lakshmanan cwd DIR 8,1 4096 393218 /home/lakshmanan
inita.sh 2971 lakshmanan rtd DIR 8,1 4096 2 /
inita.sh 2971 lakshmanan txt REG 8,1 83848 524315 /bin/dash
inita.sh 2971 lakshmanan mem REG 8,1 1434180 1442625 /lib/i386-linux-gnu/libc-2.13.so
inita.sh 2971 lakshmanan mem REG 8,1 117960 1442612 /lib/i386-linux-gnu/ld-2.13.so
inita.sh 2971 lakshmanan 0u CHR 136,4 0t0 7 /dev/pts/4
inita.sh 2971 lakshmanan 1u CHR 136,4 0t0 7 /dev/pts/4
inita.sh 2971 lakshmanan 2u CHR 136,4 0t0 7 /dev/pts/4
inita.sh 2971 lakshmanan 10r REG 8,1 20 393578 /home/lakshmanan/inita.sh
=======

In the above output, for the first 5 seconds, there is no output. After that a script named “inita.sh” is started, and it list the output every 5 seconds.


Network connections are also files. So we can find information about them by using lsof.
List processes which are listening on a particular port
You can list the processes which are listening on a particular port by using ‘-i’ with ‘:’ as follows:
# lsof -i :25
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
exim4 2541 Debian-exim 3u IPv4 8677 TCP localhost:smtp (LISTEN)


List all TCP or UDP connections
You can list all the TCP or UDP connections by specifying the protocol using ‘-i’.
# lsof -i tcp; lsof -i udp;


List all Network File System ( NFS ) files
You can list all the NFS files by using ‘-N’ option. The following lsof command will list all NFS files used by user ‘lakshmanan’.
# lsof -N -u lakshmanan -a

This message was edited 32 times. Last update was at 15/03/2016 14:26:04

2016年1月18日 星期一

[Linux 常見問題] Linux ls Command: Sort Files By Size

Source From Here
Question
How do I sort all *.avi or *.py files in $HOME/Download/ directory by file size using Linux ls command line utility?

How-To
The ls command is used to list directory contents under Linux and Unix like operating systems. If no options or operands are given, the contents of the current directory are displayed on the screen. By default entries are sorted alphabetically if none of the -cftuvSUX nor --sort option passed to the ls command.

The default output (sort by alphabetically)
Type the following command:
$ ls

Sample outputs:


Force sort by size option
You need to pass the -S or --sort=size option as follows:
// --sort=WORD: sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v
$ ls -S
$ ls -S -l
$ ls --sort=size -l

Sample outputs:


You will see largest file first before sorting the operands in lexicographical order. The following command will sort file size in reverse order:
$ ls -l -S | sort -k 5 -n

OR try (see comments below, thanks!):
// -r, --reverse: reverse order while sorting
$ ls -lSr

Sample outputs:


Sort output and print sizes in human readable format (e.g., 1K 234M 2G)
Pass the -h option to the ls command as follows:
// -h, --human-readable: with -l, print sizes in human readable format (e.g., 1K 234M 2G)
$ ls -lSh


2015年11月28日 星期六

[Linux 命令] tr 的詳細用法

1、關於tr 
通過使用 tr,您可以非常容易地實現 sed 的許多最基本功能。您可以將 tr 看作為 sed 的(極其)簡化的變體:它可以用一個字符來替換另一個字符,或者可以完全除去一些字符。您也可以用它來除去重復字符。這就是所有 tr 所能夠做的。 
tr用來從標准輸入中通過替換或刪除操作進行字符轉換。tr主要用於刪除文件中控制字符或進行字符轉換。使用tr時要轉換兩個字符串:字符串1用於查詢,字符串2用於處理各種轉換。tr剛執行時,字符串1中的字符被映射到字符串2中的字符,然後轉換操作開始。 
帶有最常用選項的tr命令格式為: 
tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] < input-file

這裡: 
-c 用字符串1中字符集的補集替換此字符集,要求字符集為ASCII。
-d 刪除字符串1中所有輸入字符。
-s 刪除所有重復出現字符序列,只保留第一個;即將重復出現字符串壓縮為一個字符串。
input-file是轉換文件名。雖然可以使用其他格式輸入,但這種格式最常用。


2、字符范圍 
指定字符串1或字符串2的內容時,只能使用單字符或字符串范圍或列表。 
[a-z] a-z內的字符組成的字符串。
[A-Z] A-Z內的字符組成的字符串。
[0-9] 數字串。
\octal 一個三位的八進制數,對應有效的ASCII字符。
[O*n] 表示字符O重復出現指定次數n。因此[O*2]匹配OO的字符串。

tr中特定控制字符的不同表達方式 
速記符含義八進制方式 
\a Ctrl-G 鈴聲\007
\b Ctrl-H 退格符\010
\f Ctrl-L 走行換頁\014
\n Ctrl-J 新行\012
\r Ctrl-M 回車\015
\t Ctrl-I tab鍵\011
\v Ctrl-X \030


3、應用例子 
(1)去除oops.txt裡面的重復的小寫字符 
# tr -s "[a-z]"result.txt

(2)刪除空行 
# tr -s "[\012]" < plan.txt 或 tr -s ["\n"] < plan.txt

(3)有時需要刪除文件中的^M,並代之以換行 
# tr -s "[\015]" "[\n]" < file 或 tr -s "[\r]" "[\n]" < file

(4)大寫到小寫 
# cat a.txt |tr "[a-z]" "[A-Z]" >b.txt

(5)刪除指定字符 
一個星期的日程表。任務是從其中刪除所有數字,只保留日期。日期有大寫,也有小寫格式。因此需指定兩個字符范圍[a-z]和[A-Z],命令tr -cs "[a-z][A-Z]" "[\012*]" 將文件每行所有不包含在[a-z]或[A-Z](所有希臘字母)的字符串放在字符串1中並轉換為一新行。-s選項表明壓縮所有新行, -c表明保留所有字母不動。原文件如下,後跟tr命令: 
# tr -cs "[a-z][A-Z]" "[\012*]"

(6)轉換控制字符 
tr的第一個功能就是轉換控制字符,特別是從dos向UNIX下載文件時,忘記設置ftp關於回車換行轉換的選項時更是如此。cat -v filename 顯示控制字符。 
cat -v stat.txt
box aa^^^^^12^M
apple bbas^^^^23^M
^Z

猜想‘^ ^ ^ ^ ^ ^’是tab鍵。每一行以Ctrl-M結尾,文件結尾Ctrl-Z,以下是改動方法。 
使用-s選項,查看ASCII表。^的八進制代碼是136,^M是015,tab鍵是011,^Z是032 ,下面將按步驟完成最終功能。 
用tab鍵替換^ ^ ^ ^ ^ ^,命令為"\136" "[\011*]"。將結果重定向到臨時工作文件stat.tmp 
# tr -s "[\136]" "[\011*]" stat.tmp

用新行替換每行末尾的^M,並用\n去除^Z,輸入要來自於臨時工作文件stat.tmp。 
# tr -s "[\015][\032]" "\n"

要刪除所有的tab鍵,代之以空格,使用命令 
tr -s "[\011]" "[\040*]"  
(7)替換passwd文件中所有冒號,代之以tab鍵,可以增加可讀性 

# tr -s "[:]" "[\011]" < /etc/passwd 或 tr -s "[:]" "[\t]" < /etc/passwd

(8)使路徑具有可讀性 
如果用 echo $PATH 或者 echo $LD_LIBRARY_PATH 等類似的命令來顯示路徑信息的話,我們看到的將會是一大堆用冒號連接在一起的路徑, tr命令可以把這些冒號轉換為回車,這樣,這些路徑就具有很好的可讀性了 
#
echo $PATH | tr ":" "\n"

(9)可以在vi內使用所有這些命令!只要記住:在tr命令前要加上您希望處理的行范圍和感歎號 (!),如 1,$!tr -d '\t'(美元符號表示最後一行)。 
(10)另外,當有人給您發送了一個在 Mac OS 或 DOS/Windows 機器上創建的文本文件時,您會發現tr非常有用。 
如果沒有將文件保存為使用 UNIX 換行符來表示行結束這種格式,則需要將這樣的文件轉換成本機 UNIX 格式,否則一些命令實用程序不會正確地處理這些文件。Mac OS 的行尾以回車字符(\r)結束,許多文本處理工具將這樣的文件作為一行來處理。為了糾正這個問題,可以用下列技巧: 
# Mac -> UNIX:tr "\r" "\n" unixfile
# UNIX -> Mac:tr "\n" "\r" macfile

Microsoft DOS/Windows 約定,文本的每行以回車字符(\r)並後跟換行符(\n)結束。為了糾正這個問題,可以使用下列命令: 
# DOS -> UNIX:tr -d "\r" unixfile

UNIX -> DOS:在這種情況下,需要用awk,因為tr不能插入兩個字符來替換一個字符。要使用的 awk 命令為 awk '{ print $0"\r" }' dosfile 
注:都可以用sed 來完成。

[Linux 命令] tr : 更換或變更檔案中的字元

屬性 : 系統相關 - 文書處理 
語法 : tr [參數] 字串一 [字串二] 
參數 | 用法 
-c | 取代或刪除不包含字元一的字元
-d | 刪除字串一所包含的字元, 此參數不需要字串二
-s | 當字串一連續出現時, 取代為一個, 此參數不需要字串二
-t | 若字串一與字串二長度不同, 指定字串一的長度給字串二


執行範例 : 
* 將 testfile 的檔案內容透過 tr 將is 字元分別換成 mn : 
client:~/test2 # cat testfile
this is line 1
this is line 2
this is line 2
this is line 3
this is line 3

client:~/test2 # cat testfile | tr is mn
thmn mn lmne 1
thmn mn lmne 2
thmn mn lmne 2
thmn mn lmne 3
thmn mn lmne 3
 

* 將testfile的檔案內容小寫全部換成大寫 : 
client:~/test2 # cat testfile | tr a-z A-Z
THIS IS LINE 1
THIS IS LINE 2
THIS IS LINE 2
THIS IS LINE 3
THIS IS LINE 3

補充說明 : 
@. tr 是常用的字元處理工具, 我們可以透過此指令將檔案的字元做處理. 
@ [Linux 命令] tr的詳細用法

2015年9月24日 星期四

[Linux 文章收集] 10 lsof Command Examples in Linux

Source From Here 
Preface 
This is our on-going series of Linux commands and in this article we are going to review lsof command with practical examples. lsof meaning ‘LiSt Open Files’ is used to find out which files are open by which process. As we all know Linux/Unix considers everything as a files (pipes, sockets, directories, devices etc).One of the reason to use lsof command is when a disk cannot be unmounted as it says the files are being used. With the help of this command we can easily identify the files which are in use. 

1. List all Open Files with lsof Command 
In the below example, it will show long listing of open files some of them are extracted for better understanding which displays the columns like Command, PID, USER, FD, TYPE etc. 






















Sections and it’s values are self-explanatory. However, we’ll review FD & TYPE columns more precisely. FD – stands for File descriptor and may seen some of the values as: 

* cwd current working directory
* rtd root directory
* txt program text (code and data)
* mem memory-mapped file

Also in FD column numbers like 1u is actual file descriptor and followed by u,r,w of it’s mode as: 
* r for read access.
* w for write access.
* u for read and write access.

TYPE – of files and it’s identification. 
* DIR – Directory
* REG – Regular file
* CHR – Character special file.
* FIFO – First In First Out

2. List User Specific Opened Files 
The below command will display the list of all opened files of user tecmint. 

















3. Find Processes running on Specific Port 
To find out all the running process of specific port, just use the following command with option -i. The below example will list all running process of port 22. 










4. List Only IPv4 & IPv6 Open Files 
In below example shows only IPv4 and IPv6 network files open with separate commands. 

























5. List Open Files of TCP Port ranges 1-1024 
To list all the running process of open files of TCP Port ranges from 1-1024. 
 

6. Exclude User with ‘^’ Character 
Here, we have excluded root user. You can exclude a particular user using ‘^’ with command as shown above. 
 

7. Find Out who’s Looking What Files and Commands? 
Below example shows user tecmint is using command like ping and /etc directory . 
 

8. List all Network Connections 
The following command with option ‘-i’ shows the list of all network connections ‘LISTENING & ESTABLISHED’. 
 

9. Search by PID 
The below example only shows whose PID is 1. 
 

10. Kill all Activity of Particular User 
Sometimes you may have to kill all the processes for a specific user. Below command will kills all the processes of tecmint user. 

// -t: specifies that lsof should produce terse output with process identifiers only and no header - e.g., so that the output may be
// piped to kill(1).

# kill -9 `lsof -t -u tecmint`


Supplement 
* 15 Linux lsof Command Examples (Identify Open Files)

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...