14,954
edits
m (Text replacement - "== references ==" to "== References ==") |
|||
| (23 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
(1) | (1) Different Linux commands that achieve the same outcome, | ||
(2) | (2) Commands that are either equivalent or alike across Windows and Linux systems. | ||
: [ | |||
== Help for command == | |||
{{Tips}} Help for command: | |||
* {{Gd}} [https://explainshell.com/ explainshell.com - match command-line arguments to their help text] | |||
* {{Linux}} or {{Mac}}: Type {{kbd | key= ''command_name'' --help}} or {{kbd | key= man ''command_name''}} ; | |||
* {{Win}}: Type {{kbd | key= ''command_name'' <nowiki>/?</nowiki>}} "Open Source tools [http://www.cygwin.com/ Cygwin] which provide functionality similar to a Linux distribution on Windows" | |||
* Official manpage [https://manpages.ubuntu.com/ Ubuntu Manpage] | |||
== Text file manipulation == | == Text file manipulation == | ||
| Line 77: | Line 82: | ||
=== Copy & overwrite file === | === Copy & overwrite file === | ||
overwrite file without prompt (confirmation) | Copy and overwrite file without prompt (confirmation) | ||
* {{kbd | key=cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} | * On {{Linux}} {{kbd | key=cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} <ref>[http://www.ewdna.com/2012/05/linux-cp-force-overwrite.html Linux:檔案複製強制覆寫, cp force overwrite @ 符碼記憶]</ref> | ||
* {{kbd | key=/bin/cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} | * On {{Linux}} {{kbd | key=/bin/cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} | ||
* On {{Win}} {{kbd | key=<nowiki>copy /Y C:\source\some.file C:\target\</nowiki>}}<ref>[https://home.csulb.edu/~murdock/copy.html DOS Command: COPY]</ref> | |||
==== Copy old directory to new directory ==== | ==== Copy old directory to new directory ==== | ||
| Line 156: | Line 162: | ||
* {{Gd}} create ls command in {{Win}} command prompt: {{kbd | key = echo dir %1 > %systemroot%\system32\ls.bat}} Thanks, hmjd!<ref>[http://stackoverflow.com/questions/9362692/how-to-create-ls-in-windows-command-prompt How to create ls in windows command prompt? - Stack Overflow]</ref> | * {{Gd}} create ls command in {{Win}} command prompt: {{kbd | key = echo dir %1 > %systemroot%\system32\ls.bat}} Thanks, hmjd!<ref>[http://stackoverflow.com/questions/9362692/how-to-create-ls-in-windows-command-prompt How to create ls in windows command prompt? - Stack Overflow]</ref> | ||
* List all files and directories including hidden ones in the current directory. On {{Win}} is {{kbd | key=<nowiki>dir /a</nowiki>}}, and on {{Mac}} or {{Linux}} is {{kbd | key=<nowiki>ls -a</nowiki>}}<ref>[http://www.computerhope.com/issues/ch001039.htm How to see hidden files in MS-DOS and the Command Prompt]</ref><ref>[http://www.cyberciti.biz/faq/bash-shell-display-only-hidden-dot-files/ Bash Shell: Display All Hidden Dot Files In a Directory]</ref>. | * List all files and directories including hidden ones in the current directory. On {{Win}} is {{kbd | key=<nowiki>dir /a</nowiki>}}, and on {{Mac}} or {{Linux}} is {{kbd | key=<nowiki>ls -a</nowiki>}}<ref>[http://www.computerhope.com/issues/ch001039.htm How to see hidden files in MS-DOS and the Command Prompt]</ref><ref>[http://www.cyberciti.biz/faq/bash-shell-display-only-hidden-dot-files/ Bash Shell: Display All Hidden Dot Files In a Directory]</ref>. | ||
List only filenames and one file per line | |||
* {{Win}} {{kbd | key=dir/b}}<ref>[https://smallbusiness.chron.com/copy-list-files-windows-folder-excel-list-40032.html How to Copy a List of Files in a Windows Folder Into an Excel List]</ref> | |||
* {{Mac}} {{kbd | key=ls -1}}<ref>[https://www.baeldung.com/linux/list-one-filename-per-line List One Filename Per Line in Linux | Baeldung on Linux]</ref> | |||
Verify if a file or a directory exists | Verify if a file or a directory exists | ||
| Line 193: | Line 204: | ||
* {{kbd | key= cd ~/}} or {{kbd | key= cd $HOME}} ({{Linux}}) will switch to my home directory ex: /Users/ACCOUNT<ref>[http://superuser.com/questions/271925/where-is-the-home-environment-variable-set linux - Where is the $HOME environment variable set? - Super User]</ref> | * {{kbd | key= cd ~/}} or {{kbd | key= cd $HOME}} ({{Linux}}) will switch to my home directory ex: /Users/ACCOUNT<ref>[http://superuser.com/questions/271925/where-is-the-home-environment-variable-set linux - Where is the $HOME environment variable set? - Super User]</ref> | ||
* {{kbd | key= cd %UserProfile%}} ({{Win}}) will switch to my home directory ex: C:\Users\ACCOUNT more on [[MS_Windows_Explorer]]<ref>[http://weka.wikispaces.com/Where+is+my+home+directory+located%3F weka - Where is my home directory located?]</ref> | * {{kbd | key= cd %UserProfile%}} ({{Win}}) will switch to my home directory ex: C:\Users\ACCOUNT more on [[MS_Windows_Explorer]]<ref>[http://weka.wikispaces.com/Where+is+my+home+directory+located%3F weka - Where is my home directory located?]</ref> | ||
=== How to list the ten most recent files in the same directory === | |||
[[How to list the ten most recent files in the same directory]] | |||
== network == | == network == | ||
| Line 236: | Line 250: | ||
== | == System operation == | ||
=== How to find the location of an executable === | |||
[[Find the location of an executable]] e.g. Using {{kbd | key=where}} command on unix-like {{OS}} | |||
=== show current time === | === show current time === | ||
| Line 258: | Line 274: | ||
=== {{OS}} version === | === {{OS}} version === | ||
* {{kbd | key=lsb_release -a}} | * {{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}} | * {{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 274: | 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 286: | Line 316: | ||
* {{kbd | key=''linux command'' <nowiki>|</nowiki> grep ''string''}} ({{Linux}}) ex: keyin {{kbd | key=<nowiki>yum list installed | grep tar</nowiki>}} to search the installed package naming tar for CentOS.<ref>[http://alvinalexander.com/unix/edu/examples/grep.shtml Unix/Linux grep command examples | grep command in Unix and Linux | grep examples | alvinalexander.com]</ref> | * {{kbd | key=''linux command'' <nowiki>|</nowiki> grep ''string''}} ({{Linux}}) ex: keyin {{kbd | key=<nowiki>yum list installed | grep tar</nowiki>}} to search the installed package naming tar for CentOS.<ref>[http://alvinalexander.com/unix/edu/examples/grep.shtml Unix/Linux grep command examples | grep command in Unix and Linux | grep examples | alvinalexander.com]</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> | * {{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 load average === | |||
==== Using WQL query to obtain Load capacity of each processor ==== | |||
on {{Win}}: | |||
* namespace: ROOT\CIMV2 | |||
* WQL query: {{kbd | key= SELECT LoadPercentage FROM Win32_Processor}}; No need to divide by CPU cores number for Win8 | |||
* output example: 24 ([https://msdn.microsoft.com/zh-tw/library/system.uint16(v=vs.110).aspx UInt16, 16 bit Unsigned Integer]) unit: % | |||
* CPU usage = LoadPercentage; unit: % | |||
==== Using PHP function to obtain system load average ==== | |||
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 | |||
=== System CPU usage === | === System CPU usage === | ||
CPU usage / | ==== Using mpstat command to obtain CPU usage ==== | ||
Example result of {{kbd | key=<nowiki>mpstat 2 2 | grep -E '^Average'</nowiki>}} on {{Linux}} | |||
* | <pre> | ||
** | Average: all 1.31 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 98.43 | ||
** | </pre> | ||
** CPU usage = | |||
The output of the mpstat command includes an "Average" line, which provides detailed information about CPU usage, similar to the %Cpu(s) line in the top command but with a slightly different format. Here's an explanation of the columns in the "Average" line: | |||
* all: This represents the average values for all CPU cores. | |||
The subsequent columns represent percentages of CPU usage: | |||
* The first column (1.31) is the CPU usage by user-space processes, indicating the percentage of time the CPU is occupied by user programs and processes. | |||
* The second column (0.00) is the CPU usage by system kernel processes, indicating the percentage of time the CPU is occupied by the operating system kernel. | |||
* The third column (0.25) is the CPU usage by processes with a "nice" priority, representing the percentage of CPU time used by processes adjusted with the nice command. | |||
* The fourth column (0.00) is the CPU's idle time, indicating the percentage of time the CPU is not executing any tasks. | |||
* The fifth column (0.00) is the CPU's time spent waiting for I/O operations to complete, indicating the percentage of time the CPU is waiting for disk or other I/O device responses. | |||
* Columns six through eleven (0.00 to 0.00) represent CPU usage in other states, such as the time spent handling hardware or software interrupts and time spent in virtualized environments with CPU steal time. | |||
* The last column (98.43) indicates the percentage of time the CPU is idle, representing the time when the CPU is not executing any tasks. | |||
In the above example, the CPU usage is as follows: | |||
* User processes have used 1.31% of CPU time. | |||
* System kernel processes have used 0.00% of CPU time. | |||
* There are no "nice" priority processes using CPU time. | |||
* The CPU is idle for 98.43% of the time. | |||
* There is no time spent waiting for I/O operations to complete. | |||
* There is no time spent handling hardware or software interrupts. | |||
* There is no CPU steal time in a virtualized environment. | |||
==== 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 -l 2 | grep -E '^CPU'</nowiki>}} on {{Mac}} | |||
# Command: Using top command e.g. {{kbd | key=<nowiki>top -n 1 | grep '%Cpu(s)'</nowiki>}} on {{Linux}} | |||
Example result of {{kbd | key=<nowiki>top -n 1 | grep '%Cpu(s)'</nowiki>}} | |||
<pre> | |||
%Cpu(s): 1.5 us, 1.5 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st | |||
</pre> | |||
The result provided is the information about CPU usage obtained through the `top` command. It shows the percentages of different CPU states, and each column is explained as follows: | |||
* `%Cpu(s)`: This is the header indicating that the following numbers are percentages related to CPU usage. | |||
* `us` (User): It represents the CPU usage by user-space processes, which includes the percentage of time the CPU is occupied by user programs and processes. In this case, it's 1.5%, indicating that 1.5% of CPU time is used for running user programs. | |||
* `sy` (System): It represents the CPU usage by system kernel processes, indicating the percentage of time the CPU is occupied by the operating system kernel. In this case, it's 1.5%, indicating that 1.5% of CPU time is used for handling operating system kernel tasks. | |||
* `ni` (Nice): It represents the CPU usage by processes with a "nice" priority, which includes the percentage of CPU time used by processes adjusted with the `nice` command. In this case, it's 0%, indicating that there are no "nice" processes running. | |||
* `id` (Idle): It represents the percentage of time the CPU is in an idle state, indicating the percentage of time the CPU is not executing any tasks. In this case, it's 96.9%, meaning that 96.9% of CPU time is idle. | |||
* `wa` (IO Wait): It represents the CPU usage while waiting for I/O operations to complete, indicating the percentage of time the CPU is waiting for disk or other I/O device responses. In this case, it's 0%, indicating that the CPU is not waiting for I/O to complete. | |||
* `hi` (Hardware IRQ): It represents the CPU usage caused by hardware interrupts, indicating the percentage of time spent handling hardware device interrupts. In this case, it's 0%, indicating that the CPU is not handling hardware interrupts. | |||
* `si` (Software IRQ): It represents the CPU usage caused by software interrupts, indicating the percentage of time spent handling software interrupts. In this case, it's 0%, indicating that the CPU is not handling software interrupts. | |||
* `st` (Steal Time): It represents the "steal time" in a virtualized environment, indicating the percentage of time a virtual machine's CPU is taken away when sharing host CPU resources with other virtual machines. In this case, it's 0%, meaning there is no virtualization steal time. | |||
In summary, this output indicates the following CPU usage: | |||
* User programs are using 1.5% of CPU time. | |||
* System kernel tasks are using 1.5% of CPU time. | |||
* There are no "nice" processes running. | |||
* 96.9% of CPU time is idle. | |||
* There is no time spent waiting for I/O operations to complete. | |||
* There is no time spent handling hardware or software interrupts. | |||
* There is no virtualization steal time. | |||
=== System memory usage === | === System memory usage === | ||
Memory | ==== Obtaining Memory Usage through a WQL Query ==== | ||
on {{Win}}: | |||
* namespace: ROOT\CIMV2 | |||
* WQL query: {{kbd | key= SELECT FreePhysicalMemory, TotalVisibleMemorySize FROM Win32_OperatingSystem}}<ref>[http://www.perlmonks.org/?node_id=882932 Collect Memory Data from WMI (Win32)]</ref> | |||
* output example: 21683460 ([https://msdn.microsoft.com/zh-tw/library/system.uint64(v=vs.110).aspx UInt64, 64 bit Unsigned Integer]) unit: KB | |||
* memory usage = (FreePhysicalMemory * 100) / TotalVisibleMemorySize; unit: % | |||
==== Obtaining Memory Usage through free command ==== | |||
on freebsd & {{Linux}} | |||
* command: {{kbd | key = free}} | |||
* output example: | |||
<pre> | <pre> | ||
total used free shared buffers cached | total used free shared buffers cached | ||
| Line 314: | Line 416: | ||
Swap: 4194300 1219364 2974936 | Swap: 4194300 1219364 2974936 | ||
</pre> | </pre> | ||
* memory usage = 7922496 / 8060416; unit: % | |||
==== Obtaining Memory Usage through top command ==== | |||
{{Mac}} (known as [https://zh.wikipedia.org/zh-tw/Darwin_(%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F) Darwin]): | |||
* command: {{kbd | key = top -l 1 | grep PhysMem:}} | |||
* output example: PhysMem: 8017M used (1486M wired), 172M unused. | |||
* 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 373: | 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 | * {{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 401: | 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]] | |||