Editing
Linux commands
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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 === * {{kbd | key= date}} ({{Linux}}) output the system date: Thu Oct 25 15:05:10 CST 2012 <ref>[http://linux.vbird.org/linux_server/0440ntp.php 鳥哥的 Linux 私房菜 -- NTP 時間伺服器]</ref><ref>[https://en.wikipedia.org/wiki/DATE_(command) DATE (command) - Wikipedia, the free encyclopedia]</ref> * {{kbd | key= TZ=Asia/Taipei date}} ({{Linux}}) output the time from the Taipei/CST timezone <ref>[http://unix.stackexchange.com/questions/48101/how-can-i-have-date-output-the-time-from-a-different-timezone How can I have `date` output the time from a different timezone? - Unix & Linux Stack Exchange]</ref> * {{kbd | key= date/t}} ({{Win}}) (parameter: /t will not ask you to change the current date) More on the page [[Show current time]] === specify the location where Linux or MS-DOS looks when using a command === * {{kbd | key= echo $PATH}} ({{Linux}})<ref>[http://linux.vbird.org/linux_basic/0220filemanager.php#dir_path 鳥哥的 Linux 私房菜 -- 檔案與目錄管理 -- 關於執行檔路徑的變數: $PATH]</ref> * {{kbd | key= echo %PATH%}} ({{Win}}) more on [[How to setup my system path]] === reboot the system/server === * {{kbd | key=reboot}} ({{Linux}})<ref>[http://linux.about.com/od/commands/l/blcmdl8_reboot.htm reboot - Linux Command - Unix Command]</ref> * {{kbd | key=shutdown -r}} ({{Win}})<ref>[http://blog.soft.idv.tw/?p=216 Windows原來也有內建好用的關機軟體(Shutdown.exe) | ㊣軟體玩家]</ref> Shutdown system at a specific date time. * {{Linux}}: (1) Enter {{kbd | key=date}} to verify the server timezone. (2) Enter {{kbd | key=sudo shutdown -h 20:02}} <ref>[https://unix.stackexchange.com/questions/120506/how-to-shutdown-linux-at-a-specific-datetime-from-terminal How to shutdown Linux at a specific datetime from terminal? - Unix & Linux Stack Exchange]</ref> * {{Win}}: [http://www.thewindowsclub.com/schedule-shutdown-restarts-windows-7-task-schedular Schedule Shutdown or Restarts in Windows using the Task Scheduler] === {{OS}} version === * {{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=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 & 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 for {{Linux}} & {{Mac}} #* {{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}} # keyin {{kbd | key=q}} to leave the process list === Kill the process === 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]] === Search and extract string from command output === * {{kbd | key=''linux command'' <nowiki>|</nowiki> awk '/''string''/'}} ({{Linux}})<ref>[https://unix.stackexchange.com/questions/393948/how-to-search-and-extract-string-from-command-output shell script - How to search and extract string from command output? - Unix & Linux Stack Exchange]</ref> ex: ** keyin {{kbd | key=<nowiki>dpkg --get-selections | awk '/tar/'</nowiki>}} to search the installed package naming ''tar'' for Ubuntu.<ref>[http://www.linuxquestions.org/questions/linux-newbie-8/logical-operators-in-grep-775595/ logical operators in grep]</ref> ** keyin {{kbd | key=<nowiki>dpkg --get-selections | awk '/zip|unzip/'</nowiki>}} to search the installed package naming ''zip'' or ''unzip'' for Ubuntu. * {{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> === 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 === ==== 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> 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 === ==== 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> total used free shared buffers cached Mem: 8060416 7922496 137920 120 183608 526180 -/+ buffers/cache: 7212708 847708 Swap: 4194300 1219364 2974936 </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 === hard disk usage * [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}} ** {{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 --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: <pre> 0M /sys 1M /tmp 9M /run 27M /etc 36M /root 165M /boot 1647M /usr 49287M /var 51379M /home 102547M / </pre> [https://www.cyberciti.biz/faq/how-do-i-find-the-largest-filesdirectories-on-a-linuxunixbsd-filesystem/ How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD - nixCraft] {{exclaim}} It may costs time to return the result if there are too many file under the specified path. Output example of {{kbd | key = <nowiki>du -a -BM /path/to/folder/ | sort -n -r | head -n 10</nowiki>}}: <pre> # du -a -BM /var/ | sort -n -r | head -n 10 11199M /var/ 10384M /var/lib 6336M /var/lib/mysql 3714M /var/lib/strong-pm 3661M /var/lib/strong-pm/svc 2042M /var/lib/strong-pm/svc/1/work 2042M /var/lib/strong-pm/svc/1 1619M /var/lib/strong-pm/svc/2/work </pre> Options cited from the content of Linux man page<ref>[http://linuxcommand.org/lc3_man_pages/du1.html du man page]</ref> * {{kbd | key=<nowiki>-a, --all</nowiki>}} "write counts for all files, not just directories" * {{kbd | key=<nowiki>-B, --block-size=SIZE</nowiki>}} "scale sizes by SIZE before printing them" === Scan the disk === * GUI: [http://support.microsoft.com/kb/156571/en-us How to Perform Scandisk in Windows] {{Win}} * Command: {{kbd | key=shutdown -rF now}} Force fsck when rebooting a Linux server ({{Linux}})<ref>[http://www.limestonenetworks.com/support/kc/7/67/how_do_i_schedule_fsck_to_run_automatically.html Cloud Provider and Dedicated Server Hosting in Dallas, TX]</ref> === Switch the user account === switch the current user * {{kbd | key=<nowiki>su</nowiki>}} on {{Linux}} switch the current user to super user <ref>[https://www.computerhope.com/unix/usu.htm Linux su command help and examples]</ref> * {{kbd | key=<nowiki>su - <user name></nowiki>}} on {{Linux}} switch the current user to another user prints the current user * {{kbd | key=<nowiki>whoami</nowiki>}} on {{Linux}} & {{Win}} "prints the effective user ID"<ref>[https://www.computerhope.com/unix/whoami.htm Linux whoami command help and examples]</ref><ref>[https://technet.microsoft.com/zh-tw/library/cc771299(v=ws.11).aspx Whoami]</ref> === Clean the screen/console === * {{Win}}: {{kbd | key= cls}} <ref>[https://technet.microsoft.com/en-us/library/bb490879.aspx Cls]</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>
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information