Linux commands: Difference between revisions

Jump to navigation Jump to search
3,802 bytes added ,  18 December 2025
m
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
(1) Alternative Linux commands to complete the same task,  
(1) Different Linux commands that achieve the same outcome,  
(2) The equivalent or similar commands between Windows and Linux system.
(2) Commands that are either equivalent or alike across Windows and Linux systems.
 


== Help for command ==
== Help for command ==
: [[Image:Owl icon.jpg]] Help for command:  
{{Tips}} Help for command:  


* {{Gd}} [https://explainshell.com/ explainshell.com - match command-line arguments to their help text]
* {{Gd}} [https://explainshell.com/ explainshell.com - match command-line arguments to their help text]
Line 273: Line 274:


=== {{OS}} version ===
=== {{OS}} version ===
* {{kbd | key=lsb_release -a}} ({{Linux}})<ref>[http://blog.miniasp.com/post/2009/02/25/How-to-query-Linux-distribution-and-version-information.aspx The Will Will Web | 如何查詢 Linux 的種類與版本 ( Linux Standard Base )]</ref>
* {{kbd | key=lsb_release -a}} on {{Linux}}<ref>[http://blog.miniasp.com/post/2009/02/25/How-to-query-Linux-distribution-and-version-information.aspx The Will Will Web | 如何查詢 Linux 的種類與版本 ( Linux Standard Base )]</ref>
* {{kbd | key=ver}} ({{Win}})<ref>[http://www.windows-commandline.com/2009/01/find-windows-os-version-from-command.html Find windows OS version from command line]</ref>
* {{kbd | key=cat /etc/redhat-release}} on {{Linux}} CentOS<ref>[https://www.cnblogs.com/technology178/p/16709400.html 查看操作系统版本的 N 种方式(Windows、CentOS、Ubuntu、Debian) - Rainbowhhy - 博客园]</ref>
 
* {{kbd | key=ver}} on {{Win}}<ref>[http://www.windows-commandline.com/2009/01/find-windows-os-version-from-command.html Find windows OS version from command line]</ref>


=== Show the process list ===
=== Show the process list ===
Line 289: Line 290:
* {{kbd | key=kill -9 <PID>}}. Force to kill the process with PID number: 101, enter {{kbd | key=kill -9 101}}. <ref>[http://linux.vbird.org/linux_basic/0440processcontrol.php#killjobs 鳥哥的 Linux 私房菜 -- 第十六章、程序管理與 SELinux 初探]</ref>
* {{kbd | key=kill -9 <PID>}}. Force to kill the process with PID number: 101, enter {{kbd | key=kill -9 101}}. <ref>[http://linux.vbird.org/linux_basic/0440processcontrol.php#killjobs 鳥哥的 Linux 私房菜 -- 第十六章、程序管理與 SELinux 初探]</ref>


Kill the process by process name<ref>[https://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex Find and kill a process in one line using bash and regex - Stack Overflow]</ref>
'''Kill the process by process name'''<ref>[https://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex Find and kill a process in one line using bash and regex - Stack Overflow]</ref>
* {{kbd | key=<nowiki>sudo kill $(ps aux | grep '<NAME>' | awk '{print $2}')</nowiki>}}
* {{kbd | key=<nowiki>sudo kill $(ps aux | grep '<NAME>' | awk '{print $2}')</nowiki>}}
'''Kill the process by port number'''<ref>[https://stackoverflow.com/questions/11583562/how-to-kill-a-process-running-on-particular-port-in-linux How to kill a process running on particular port in Linux? - Stack Overflow], [https://linux.die.net/man/8/lsof lsof(8) - Linux man page]</ref>
* {{kbd | key=<nowiki>kill -9 $(lsof -ti:3000) 2>/dev/null</nowiki>}}: Force kill the process using port 3000. The {{kbd | key=2>/dev/null}} suppresses error messages if no process is found. Replace 3000 with your target port number.
* Alternative (shorter command): {{kbd | key=fuser -k 3000/tcp}}: Kill the process using TCP port 3000 (if {{kbd | key=fuser}} is available on your system)
Explanation:
* {{kbd | key=<nowiki>lsof -ti:3000</nowiki>}}: Find the process ID (PID) using port 3000
** {{kbd | key=-t}}: Output only the PID
** {{kbd | key=-i:3000}}: Filter by port 3000
* {{kbd | key=2>/dev/null}}: Redirects error messages (stderr) to {{kbd | key=/dev/null}} (discards them) Useful when no process is found on the port to avoid error output
* {{kbd | key=<nowiki>$(lsof -ti:3000)</nowiki>}}: Command substitution<ref>[https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Command Substitution (Bash Reference Manual)]</ref>: executes the command inside {{kbd | key=$()}} and replaces it with the output
** First runs {{kbd | key=lsof -ti:3000}} to get the PID(s)
** Then substitutes the PID(s) into the {{kbd | key=kill -9}} command
** Example: if port 3000 is used by PID 12345, {{kbd | key=<nowiki>kill -9 $(lsof -ti:3000)</nowiki>}} becomes {{kbd | key=kill -9 12345}}


=== Find process running on port ===
=== Find process running on port ===
Line 302: Line 317:
* {{kbd | key=''Windows command'' <nowiki>|</nowiki> find ''"string"''}} ({{Win}}) ex: {{kbd | key=<nowiki>netstat -a | find "3306"</nowiki>}}  (note:  enclose string in double quotation marks)<ref>[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/find.mspx?mfr=true Microsoft Windows XP - Find]</ref>
* {{kbd | key=''Windows command'' <nowiki>|</nowiki> find ''"string"''}} ({{Win}}) ex: {{kbd | key=<nowiki>netstat -a | find "3306"</nowiki>}}  (note:  enclose string in double quotation marks)<ref>[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/find.mspx?mfr=true Microsoft Windows XP - Find]</ref>


=== System CPU usage ===
=== System load average ===
==== Using WQL query ====
==== Using WQL query to obtain Load capacity of each processor ====
on {{Win}}:  
on {{Win}}:  
* namespace: ROOT\CIMV2
* namespace: ROOT\CIMV2
Line 310: Line 325:
* CPU usage = LoadPercentage; unit: %
* CPU usage = LoadPercentage; unit: %


==== Using PHP function ====
==== Using PHP function to obtain system load average ====
on freebsd, {{Linux}} & {{Mac}}:  
on freebsd, {{Linux}} & {{Mac}}:  
* PHP function [http://php.net/manual/en/function.sys-getloadavg.php sys_getloadavg()] unit: %; '''Need''' to divide by CPU cores number
* PHP function [http://php.net/manual/en/function.sys-getloadavg.php sys_getloadavg()] unit: %; '''Need''' to divide by CPU cores number


==== Using mpstat command ====
=== System CPU usage ===
==== Using mpstat command to obtain CPU usage ====
Example result of {{kbd | key=<nowiki>mpstat 2 2 | grep -E '^Average'</nowiki>}} on {{Linux}}
Example result of {{kbd | key=<nowiki>mpstat 2 2 | grep -E '^Average'</nowiki>}} on {{Linux}}
<pre>
<pre>
Line 342: Line 358:
* There is no CPU steal time in a virtualized environment.
* There is no CPU steal time in a virtualized environment.


==== Using top command ====
==== Using top command to obtain CPU usage ====
 
# Command: Using top command e.g. {{kbd | key=<nowiki>top</nowiki>}}  on {{Linux}} or {{Mac}}  
# Command: Using top command e.g. {{kbd | key=<nowiki>top</nowiki>}}  on {{Linux}} or {{Mac}}  
# Command: Using top command e.g. {{kbd | key=<nowiki>top -l 2 | grep -E '^CPU'</nowiki>}} on {{Mac}}  
# Command: Using top command e.g. {{kbd | key=<nowiki>top -l 2 | grep -E '^CPU'</nowiki>}} on {{Mac}}  
Line 408: Line 423:
* output example: PhysMem: 8017M used (1486M wired), 172M unused.
* output example: PhysMem: 8017M used (1486M wired), 172M unused.
* memory usage: 172 / (8017+172); unit: %
* memory usage: 172 / (8017+172); unit: %
{{Linux}} or {{Mac}}
* command: {{kbd | key = top}} (and press q key to quit the top window)
* output example: The Python process with the Process ID (PID) 13086 is utilizing 22.19 GB of memory, as indicated by the value in the RES (Resident Set Size) field.
<pre>
PID    USER      PR  NI  VIRT    RES    SHR S  %CPU %MEM    TIME+  COMMAND
13086 account  20  0  132.1g  22.19g 13.0g S 42.9 17.6    30:33.61 python
</pre>
* PID: Process ID, 13086 in this case, which is the unique identifier for the process.
* USER: The user that started the process, account here.
* PR: Priority of the process in the kernel's scheduler. The value 20 indicates a regular priority.
* NI: Nice value, which influences the priority (0 by default, meaning no adjustment to the scheduling).
* VIRT: Virtual memory size, 132.1g for this process, which is the total amount of virtual memory used by the process.
* RES: Resident memory size, 22.19g here, showing how much physical memory the process is currently using.
* SHR: Shared memory size, 13.0g in this case, which is the memory this process is sharing with others.
* S: Process status, S here which usually means 'sleeping'.
* %CPU: CPU usage, 42.9 indicating the percentage of the CPU time the process is currently using.
* %MEM: Memory usage, 17.6 showing the percentage of total physical memory used by the process.
* TIME+: Total CPU time the process has been running, 30:33.61 indicates 30 minutes and 33.61 seconds.
* COMMAND: The command that initiated the process, python here.


=== System disk space usage  ===
=== System disk space usage  ===
Line 461: Line 499:
=== Clean the screen/console ===
=== Clean the screen/console ===
* {{Win}}: {{kbd | key= cls}} <ref>[https://technet.microsoft.com/en-us/library/bb490879.aspx Cls]</ref>
* {{Win}}: {{kbd | key= cls}} <ref>[https://technet.microsoft.com/en-us/library/bb490879.aspx Cls]</ref>
* {{Linux}} & {{Mac}}: {{kbd | key= clear}} <ref>[https://www.linux.org/threads/clear-clear-your-terminal-screen.78/ Clear! (clear your terminal screen) | Linux.org]</ref>
* {{Linux}}: {{kbd | key= clear}} <ref>[https://www.linux.org/threads/clear-clear-your-terminal-screen.78/ Clear! (clear your terminal screen) | Linux.org]</ref>
* {{Mac}}
** {{kbd | key= clear}}
** {{kbd | key=⌘ command}} +{{kbd | key=K}} <ref>[https://stackoverflow.com/questions/2198377/how-can-i-clear-previous-output-in-terminal-in-mac-os-x macos - How can I clear previous output in Terminal in Mac OS X? - Stack Overflow]</ref>


== help for command ==
== help for command ==
Line 489: Line 530:
* [[Open command window here]]
* [[Open command window here]]


[[Category:Linux]]
[[Category: Linux]]
[[Category:Windows]]
[[Category: Windows]]
[[Category:Database]]
[[Category: Database]]
[[Category:MySQL]]
[[Category: MySQL]]
[[Category: Revised with LLMs]]

Navigation menu