Linux commands: Difference between revisions

Jump to navigation Jump to search
9,185 bytes added ,  18 December 2025
m
 
(38 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.


: [[Image:Owl icon.jpg]] Help for command:
 
: {{Linux}} or {{Mac}}: Type {{kbd | key= ''command_name'' --help}} or {{kbd | key= man ''command_name''}}  ;  
== Help for command ==
: {{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"
{{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 14: Line 19:
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}})
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}})
* [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}}); Open file and press {{kbd | key=gg}} for [http://www.vim.org/ gVim] ({{Win}})
* [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}}); Open file and press {{kbd | key=gg}} for [http://www.vim.org/ gVim] ({{Win}})
==== Display the first xxx bytes of text file ====
* On {{Linux}}, To display the first bytes characters of text file<ref>[https://unix.stackexchange.com/questions/167814/get-first-x-characters-from-the-cat-command cut - get first X characters from the cat command? - Unix & Linux Stack Exchange]</ref>
<pre>
head -c 100 file_name
</pre>


==== Display the last few lines of text file ====
==== Display the last few lines of text file ====
Line 59: Line 71:


=== Merge multiple plain text files ===
=== Merge multiple plain text files ===
Step 1: check the last line of text file is [[Return symbol | newline]]<ref>[https://stackoverflow.com/questions/34943632/linux-check-if-there-is-a-newline-at-the-end-of-a-file eof - Linux - check if there is a newline at the end of a file - Stack Overflow]</ref>
[[Merge multiple text files into one file]]
* {{kbd | key=<nowiki>tail -c 1 file.txt</nowiki>}} on {{Linux}}. Parameter "-c <span style="text-decoration: underline;">number</span>: The location is <span style="text-decoration: underline;">number</span> bytes." quoted from the [http://man7.org/linux/man-pages/man1/tail.1.html commands manual]. If the last line is newline, returned result will be empty. {{exclaim}} How to check multiple files?
* (optional) If the last line is not newline, you may add the new line manually. See details on [https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file bash - How to add a newline to the end of a file? - Unix & Linux Stack Exchange]
 
Step 2: Merge the content
* {{kbd | key=<nowiki>copy *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>copy file1.txt file2.txt > bundle.txt </nowiki>}} on {{Win}}
* {{kbd | key=<nowiki>cat *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>cat file1.txt file2.txt > bundle.txt </nowiki>}} on {{Linux}}<ref>[http://www.maclife.com/article/columns/terminal_101_join_multiple_files_together_cat Terminal 101: Join Multiple Files Together with Cat]</ref><ref>[https://unix.stackexchange.com/questions/3770/how-to-merge-all-text-files-in-a-directory-into-one How to merge all (text) files in a directory into one? - Unix & Linux Stack Exchange]</ref>
 
Step 3: (optional) Remove the duplicated lines
* {{kbd | key=sort -us -o bundle_unique.txt bundle.txt}}<ref>[http://unix.stackexchange.com/questions/19641/how-to-remove-duplicate-lines-in-a-large-multi-gb-textfile linux - How to remove duplicate lines in a large multi-GB textfile? - Unix & Linux Stack Exchange]</ref> OS: {{Linux}}, cygwin of {{Win}} "-u means Unique keys; -s means stable sort; -o means output" quoted from [https://www.computerhope.com/unix/usort.htm sort] manual.


== File operation ==
== File operation ==
Line 79: Line 82:


=== Copy & overwrite file ===
=== Copy & overwrite file ===
overwrite file without prompt (confirmation): [http://www.computerhope.com/unix/ucp.htm cp] command
Copy and overwrite file without prompt (confirmation)
* {{kbd | key=cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} ({{Linux}})<ref>[http://www.ewdna.com/2012/05/linux-cp-force-overwrite.html Linux:檔案複製強制覆寫, cp force overwrite @ 符碼記憶]</ref>
* 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''}} ({{Linux}})
* 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 104: Line 108:


==== Rename (move) files or directory ====
==== Rename (move) files or directory ====
* {{kbd | key=mv ''old_folder_name new_folder_name''}} ({{Linux}})<ref>[http://www.liamdelahunty.com/tips/linux_rename_directory.php Linux Rename Files, Folders or Directories]</ref>
* {{kbd | key=mv ''/path/to/old_folder_name /path/to/new_folder_name''}} ({{Linux}})<ref>[http://www.liamdelahunty.com/tips/linux_rename_directory.php Linux Rename Files, Folders or Directories]</ref>
* {{kbd | key=mv ''/path/to/old.file /path/to/new.file''}} ({{Linux}})
* {{kbd | key=mv ''/path/to/file /path/to/new_folder_name''}} ({{Linux}})
* {{kbd | key=mv ''/path/to/file /path/to/new_folder_name''}} ({{Linux}})
* {{kbd | key=rename ''old_folder_name new_folder_name''}} ({{Win}})<ref>[http://www.computerhope.com/renamehl.htm MS-DOS ren and rename command help]</ref>
* {{kbd | key=rename ''old_folder_name new_folder_name''}} ({{Win}})<ref>[http://www.computerhope.com/renamehl.htm MS-DOS ren and rename command help]</ref>
Line 157: 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 194: 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 ==
show ip
show ip
* {{kbd | key=<nowiki>ifconfig</nowiki>}} ({{Linux}})<ref>[http://linux.vbird.org/linux_server/0140networkcommand.php#ifconfig 鳥哥的 Linux 私房菜 -- Linux 常用網路指令介紹]</ref><ref>bash: ifconfig: command not found [http://phorum.study-area.org/index.php?topic=47045.0 無法使用ifconfig指令?] 先加上 {{kbd | key = su -l}}指令</ref>
* {{kbd | key=<nowiki>ifconfig</nowiki>}} for {{Linux}}<ref>[http://linux.vbird.org/linux_server/0140networkcommand.php#ifconfig 鳥哥的 Linux 私房菜 -- Linux 常用網路指令介紹]</ref><ref>bash: ifconfig: command not found [http://phorum.study-area.org/index.php?topic=47045.0 無法使用ifconfig指令?] 先加上 {{kbd | key = su -l}}指令</ref> & {{Mac}}<ref>[https://www.mobile01.com/topicdetail.php?f=177&t=251814 mac 要怎麼看自己的ip呢 - Mobile01]</ref>
* {{kbd | key=<nowiki>ipconfig/all</nowiki>}} ({{Win}})
* {{kbd | key=<nowiki>ipconfig/all</nowiki>}} ({{Win}})


Line 237: Line 250:




== system operation ==
== 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 259: 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 & kill the process ===
=== Show the process list ===
Show the process list & kill the high-resource-consumption process<ref>[http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html How do I Find Out Linux CPU Utilization? - nixCraft]</ref><ref>[http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/ Show All Running Processes in Linux]</ref>
Show the process list & kill the high-resource-consumption process<ref>[http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html How do I Find Out Linux CPU Utilization? - nixCraft]</ref><ref>[http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/ Show All Running Processes in Linux]</ref>
# display the process list
# Display the process list for {{Linux}} & {{Mac}}
#* {{kbd | key=top }} "display Linux tasks" for {{Linux}} or  
#* {{kbd | key=top }} "display Linux tasks" for {{Linux}} or  
#* {{kbd | key=<nowiki>top -a</nowiki>}} or {{kbd | key=<nowiki>ps aux --sort -rss | more</nowiki>}}<ref>[http://linux.vbird.org/linux_basic/0440processcontrol.php#ps_aux 鳥哥的 Linux 私房菜 -- 第十六章、程序管理與 SELinux 初探]</ref> "Sort by memory usage" for {{Linux}}
#* {{kbd | key=<nowiki>top -a</nowiki>}} or {{kbd | key=<nowiki>ps aux --sort -rss | more</nowiki>}}<ref>[http://linux.vbird.org/linux_basic/0440processcontrol.php#ps_aux 鳥哥的 Linux 私房菜 -- 第十六章、程序管理與 SELinux 初探]</ref> "Sort by memory usage" for {{Linux}}
# keyin {{kbd | key=q}} to leave the process list
# keyin {{kbd | key=q}} to leave the process list
# find which process to be killed. And then keyin:  
 
#* {{kbd | key=kill <PID>}} or {{kbd | key=kill -<PID> PID}}. To kill the process with PID number: 101, enter {{kbd | key=kill 101}}. <ref>[http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/ Kill Process in Linux or Terminate a Process in UNIX / Linux Systems], [https://linux.die.net/man/1/kill kill(1): terminate process - Linux man page]</ref>
=== Kill the process ===
#* {{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>
Find which process to be killed. And then keyin the process ID (PID):  
* {{kbd | key=kill <PID>}} or {{kbd | key=kill -<PID> PID}}. To kill the process with PID number: 101, enter {{kbd | key=kill 101}}. <ref>[http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/ Kill Process in Linux or Terminate a Process in UNIX / Linux Systems], [https://linux.die.net/man/1/kill kill(1): terminate process - Linux man page]</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>
* {{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 282: 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 / system load average
==== Using mpstat command to obtain CPU usage ====
* {{Win}}:  
Example result of {{kbd | key=<nowiki>mpstat 2 2 | grep -E '^Average'</nowiki>}} on {{Linux}}
** namespace: ROOT\CIMV2
<pre>
** WQL query: {{kbd | key= SELECT LoadPercentage FROM Win32_Processor}}; No need to divid by CPU cores number for Win8
Average:     all    1.31    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00  98.43
** output example: 24 ([https://msdn.microsoft.com/zh-tw/library/system.uint16(v=vs.110).aspx UInt16, 16 bit Unsigned Integer]) unit: %
</pre>
** CPU usage = LoadPercentage; unit: %
 
* freebsd, {{Linux}} & {{Mac}}:  
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:
** PHP function [http://php.net/manual/en/function.sys-getloadavg.php sys_getloadavg()] unit: %; '''Need''' to divid by CPU cores number
 
* 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 usage
==== Obtaining Memory Usage through a WQL Query ====
* {{Win}}:
on {{Win}}:
** namespace: ROOT\CIMV2
* 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>
* 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
* 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: %
* memory usage = (FreePhysicalMemory * 100) / TotalVisibleMemorySize; unit: %


* freebsd & {{Linux}}:
==== Obtaining Memory Usage through free command ====
** command: {{kbd | key = free}}
on freebsd & {{Linux}}
** output example:  
* command: {{kbd | key = free}}
* output example:  
<pre>
<pre>
                               total      used      free    shared    buffers    cached
                               total      used      free    shared    buffers    cached
Line 310: Line 416:
                     Swap:      4194300    1219364    2974936
                     Swap:      4194300    1219364    2974936
</pre>
</pre>
** memory usage = 7922496 / 8060416; unit: %
* 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>
 


* {{Mac}} (known as [https://zh.wikipedia.org/zh-tw/Darwin_(%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F) Darwin]):
* PID: Process ID, 13086 in this case, which is the unique identifier for the process.
** command: {{kbd | key = top -l 1 | grep PhysMem:}}
* USER: The user that started the process, account here.
** output example: PhysMem: 8017M used (1486M wired), 172M unused.
* PR: Priority of the process in the kernel's scheduler. The value 20 indicates a regular priority.
** memory usage: 172 / (8017+172); unit: %
* 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 321: Line 451:
* [http://linux.about.com/od/commands/l/blcmdl1_df.htm df command] "disk space usage" for {{Linux}} {{kbd | key = <nowiki>df -h</nowiki>}}
* [http://linux.about.com/od/commands/l/blcmdl1_df.htm df command] "disk space usage" for {{Linux}} {{kbd | key = <nowiki>df -h</nowiki>}}
* [http://linux.about.com/library/cmd/blcmdl1_du.htm du command] "estimate file space usage" of specific folder for {{Linux}}  
* [http://linux.about.com/library/cmd/blcmdl1_du.htm du command] "estimate file space usage" of specific folder for {{Linux}}  
** {{kbd | key = <nowiki>du -h /path/to/folder/</nowiki>}} Detailed output of hard disk usage of specific folder and child folders.
** {{kbd | key = <nowiki>du -h /path/to/folder/</nowiki>}} Detailed output of hard disk usage of specific folder and child folders. e.g. {{kbd | key = <nowiki>du -h /home | sort -rn >
~/disk.txt</nowiki>}}
** {{kbd | key = <nowiki>du -hcs /path/to/folder/</nowiki>}} Brief output of total hard disk usage.
** {{kbd | key = <nowiki>du -hcs /path/to/folder/</nowiki>}} Brief output of total hard disk usage.
** {{kbd | key = <nowiki>du --max-depth=1 -B M /path/to/folder/ | sort -g</nowiki>}}<ref>[http://blog.xuite.net/chingwei/blog/32566618-%E3%80%90%E7%B3%BB%E7%B5%B1%E3%80%91%E4%BD%BF%E7%94%A8+du+%E4%BE%86%E7%9C%8B%E7%A3%81%E7%A2%9F%E7%9A%84%E4%BD%BF%E7%94%A8%E7%A9%BA%E9%96%93 【系統】使用 du 來看磁碟的使用空間 @ My Life :: 隨意窩 Xuite日誌]</ref>Output example:
** {{kbd | key = <nowiki>du --max-depth=1 -B M /path/to/folder/ | sort -g</nowiki>}}<ref>[http://blog.xuite.net/chingwei/blog/32566618-%E3%80%90%E7%B3%BB%E7%B5%B1%E3%80%91%E4%BD%BF%E7%94%A8+du+%E4%BE%86%E7%9C%8B%E7%A3%81%E7%A2%9F%E7%9A%84%E4%BD%BF%E7%94%A8%E7%A9%BA%E9%96%93 【系統】使用 du 來看磁碟的使用空間 @ My Life :: 隨意窩 Xuite日誌]</ref>Output example:
Line 368: 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 377: Line 511:
=== find the path of an executable ===
=== find the path of an executable ===
* {{kbd | key= which ''command''}} for {{Linux}} ex: {{kbd | key= which wc}} will return the path '/usr/bin/wc'
* {{kbd | key= which ''command''}} for {{Linux}} ex: {{kbd | key= which wc}} will return the path '/usr/bin/wc'
* {{kbd | key= which ''command''}} or {{kbd | key= where ''command''}} for {{Mac}} ex: {{kbd | key= which wc}} will return the path '/usr/bin/wc'
* {{kbd | key= which ''command''}} for {{Mac}} ex: {{kbd | key= which wc}} will return the path '/usr/bin/wc' (Input {{kbd | key= where ''command''}} to find alternative commands with the same file name.)
* {{kbd | key= where ''command''}} for {{Win}} version 2003 or 8. ex: {{kbd | key= where nslookup}} will return the path 'C:\Windows\System32\nslookup.EXE'<ref>[http://superuser.com/questions/207707/what-is-windows-equivalent-of-the-which-command-in-unix-is-there-an-equivale What is Windows' equivalent of the "which" command in Unix? Is there an equivalent PowerShell command? - Super User]</ref> {{exclaim}} not all DOS command will return the information ex: where cd, where dir. Related page: [[How to setup my system path]]
* {{kbd | key= where ''command''}} for {{Win}} version 2003 or 8. ex: {{kbd | key= where nslookup}} will return the path 'C:\Windows\System32\nslookup.EXE'<ref>[http://superuser.com/questions/207707/what-is-windows-equivalent-of-the-which-command-in-unix-is-there-an-equivale What is Windows' equivalent of the "which" command in Unix? Is there an equivalent PowerShell command? - Super User]</ref> {{exclaim}} not all DOS command will return the information ex: where cd, where dir. Related page: [[How to setup my system path]]


Line 384: Line 518:
* {{kbd | key= Pause/Break}} ({{Linux}}) ex: I pressed the key to leave from the warning message "Vim: Warning: Output is not to a terminal".
* {{kbd | key= Pause/Break}} ({{Linux}}) ex: I pressed the key to leave from the warning message "Vim: Warning: Output is not to a terminal".


== 相關新聞聯播 ==
== References ==
{{News feed | title =Linux OR unix 相關新聞聯播 | feed = <rss>https://news.google.com/rss/search?q=linux+OR+unix&hl=zh-TW&gl=TW&ceid=TW:zh-Hant</rss> }}
 
== references ==
<references/>
<references/>


Line 399: 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