{"id":361,"date":"2017-03-13T02:19:43","date_gmt":"2017-03-12T18:19:43","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=361"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"linux-commands-cookbook","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/linux-commands-cookbook.html","title":{"rendered":"Linux commands cookbook"},"content":{"rendered":"<p>50 Most Frequently Used UNIX \/ Linux Commands<br \/>\n<a href=\"http:\/\/www.thegeekstuff.com\/2010\/11\/50-linux-commands\/\">http:\/\/www.thegeekstuff.com\/2010\/11\/50-linux-commands\/<\/a><\/p>\n<h2>Write a Simple Script in Shell<\/h2>\n<p>Write a <code>for<\/code> loop.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ for pod_name in $(kubectl get pods -l app=swag-worker-test -o jsonpath={..metadata.name}); do; kubectl delete pod $pod_name; done\npod \"swag-worker-test-67fffcdd5-5hgf3\" deleted\npod \"swag-worker-test-67fffcdd5-h8jgg\" deleted\n\n# you could also write multiple lines\nfor pod_name in $(kubectl get pods -l app=swag-worker-test -o jsonpath={..metadata.name}); do\n    kubectl delete pod $pod_name\ndone<\/code><\/pre>\n<p>Write a <code>while true<\/code>.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-console\"># using trap and wait will make your container react immediately to a stop request\n$ bash -c \"trap : TERM INT; sleep infinity &amp; wait\"\n# or\n$ bash -c \"while true; do echo 'I am alive!'; sleep 3600; done\"<\/code><\/pre>\n<p>or<\/p>\n<pre class=\"line-numbers\"><code class=\"language-bash\">#!\/bin\/bash\nwhile true; do echo 'I am alive!'; sleep 3600; done<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/31870222\/how-can-i-keep-container-running-on-kubernetes\">https:\/\/stackoverflow.com\/questions\/31870222\/how-can-i-keep-container-running-on-kubernetes<\/a><\/p>\n<h2>Set Environment Variables from a File<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ export $(cat .env | grep -v ^# | xargs)<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/19331497\/set-environment-variables-from-file\">https:\/\/stackoverflow.com\/questions\/19331497\/set-environment-variables-from-file<\/a><\/p>\n<h2>Switch to Another User<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\"># the latter with \"-\" gets an environment as if another user just logged in\n$ sudo su - ubuntu<\/code><\/pre>\n<h2>Clear the Content of a File<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ echo -n &gt; \/var\/log\/nginx\/error.log<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/unix.stackexchange.com\/questions\/88808\/empty-the-contents-of-a-file\">https:\/\/unix.stackexchange.com\/questions\/88808\/empty-the-contents-of-a-file<\/a><\/p>\n<h2>Pipeline stdout with xargs<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ find . -type f -name \"*.yaml\" | xargs echo\n.\/configmap.yaml .\/pvc.yaml .\/service.yaml .\/statefulset.yaml\n\n$ find . -type f -name \"*.yaml\" | xargs -n 1 echo\n.\/configmap.yaml\n.\/pvc.yaml\n.\/service.yaml\n.\/statefulset.yaml\n\n$ find . -type f -name \"*.yaml\" | xargs -n 2 echo\n.\/configmap.yaml .\/pvc.yaml\n.\/service.yaml .\/statefulset.yaml\n\n$ redis-cli KEYS \"*-*-*-*.reply.celery.pidbox\" | xargs -n 100 redis-cli DEL<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/shapeshed.com\/unix-xargs\/\">https:\/\/shapeshed.com\/unix-xargs\/<\/a><\/p>\n<h2>Set a Timeout for any Command<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ timeout -t 15 celery inspect ping -A app:celery -d celery@$(hostname)<\/code><\/pre>\n<h2>Run a One-time Command at a Specific Time<\/h2>\n<p><code>at<\/code> executes commands at a specified time. You may need to install the &quot;at&quot; package manually.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-console\"># install\n$ sudo apt-get install at\n\n# start\n$ sudo atd\n\n# list jobs\n$ atq\n\n$ at 00:05\nat&gt; echo \"123\" &gt; \/tmp\/test.txt\n\n$ at 00:00 18.1.2017\nat&gt; DPS_ENV=production \/home\/ubuntu\/.virtualenvs\/dps\/bin\/python \/home\/ubuntu\/dps\/manage.py send_emails &gt; \/tmp\/send_emails.log<\/code><\/pre>\n<p>Press <code>Control + D<\/code> to exit at shell.<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/www.lifewire.com\/linux-command-at-4091646\">https:\/\/www.lifewire.com\/linux-command-at-4091646<\/a><br \/>\n<a href=\"https:\/\/tecadmin.net\/one-time-task-scheduling-using-at-commad-in-linux\/\">https:\/\/tecadmin.net\/one-time-task-scheduling-using-at-commad-in-linux\/<\/a><\/p>\n<h2>Pass Arguments to bash when Executing a script Fetched by curl<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ curl -L http:\/\/bit.ly\/open-the-pod-bay-doors | bash -s -- --tags docker <\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/a\/25563308\/885524\">https:\/\/stackoverflow.com\/a\/25563308\/885524<\/a><br \/>\n<a href=\"https:\/\/github.com\/vinta\/HAL-9000\">https:\/\/github.com\/vinta\/HAL-9000<\/a><\/p>\n<h2>Change a File's Modify Time<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ touch -m -d '1 Jan 2006 12:34' tmp\n$ touch -m -d '1 Jan 2006 12:34' tmp\/old_file.txt<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/www.unixtutorial.org\/2008\/11\/how-to-update-atime-and-mtime-for-a-file-in-unix\/\">https:\/\/www.unixtutorial.org\/2008\/11\/how-to-update-atime-and-mtime-for-a-file-in-unix\/<\/a><\/p>\n<h2>Delete Old Files under a Directory<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ find \/data\/storage\/tmp\/* -mtime +2 | xargs rm -Rf\n$ find \/data\/storage\/tmp\/* -mtime +2 -exec rm {} ;<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/14731133\/how-to-delete-all-files-older-than-3-days-when-argument-list-too-long\">http:\/\/stackoverflow.com\/questions\/14731133\/how-to-delete-all-files-older-than-3-days-when-argument-list-too-long<\/a><\/p>\n<h2>Append String to a File<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\"># append\n$ echo \"the last line\" &gt;&gt; README.md\n\n# replace\n$ echo \"replace all\" &gt; README.md<\/code><\/pre>\n<h2>Rename Sub-folders<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ for f in *\/migrations\/; do mv -v \"$f\" \"${f%???????????}south_migrations\"; done<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/unix.stackexchange.com\/questions\/220176\/rename-specific-level-of-sub-folders\">http:\/\/unix.stackexchange.com\/questions\/220176\/rename-specific-level-of-sub-folders<\/a><\/p>\n<h2>List History Commands<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ export HISTTIMEFORMAT=\"%Y%m%d %T  \"\n$ history<\/code><\/pre>\n<h2>Make Permission For A File Same As Another File<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ chmod --reference=file1 file2<\/code><\/pre>\n<h2>Find Computer's Public IP<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ wget -qO- http:\/\/ipecho.net\/plain ; echo<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/askubuntu.com\/questions\/95910\/command-for-determining-my-public-ip\">http:\/\/askubuntu.com\/questions\/95910\/command-for-determining-my-public-ip<\/a><\/p>\n<h2>Compress and Uncompress Files<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ tar czf media-20151010.tar.gz media\/\n$ s3cmd put media-20151010.tar.gz s3:\/\/goeasytaxi\/\n\n# decompress\n$ tar -xzf media.tar.gz\n\n$ sudo apt-get install zip unzip\n$ zip -j -r deps.zip spark_app\/src\/deps\/\n$ zip -r hourmasters.zip hourmasters\/\n$ scp -r -i ~\/hourmasters.pem ssh ubuntu@52.196.120.217:\/home\/ubuntu\/hourmasters.zip ~\/Desktop\/\n\n# decompress\n$ unzip stork.1.4.zip\n$ gzip -d uwsgi.log.*.gz\n\n$ gzip dps.201701171200.sql\n$ gunzip dps.201701171200.sql.gz<\/code><\/pre>\n<h2>Count File Lines<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ wc -l filename.txt\n\n$ wc -l *.py<\/code><\/pre>\n<h2>Find Files by Name or Content<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ find \/ -name virtualenvwrapper.sh\n\n# \u5728\u73fe\u5728\u7684\u8cc7\u6599\u593e\u88e1\u7684\u5168\u90e8\u6a94\u6848\u4e2d\u641c\u5c0b\u5b57\u4e32\uff0c\u6703\u81ea\u52d5\u641c\u5c0b\u5b50\u76ee\u9304\n$ find . | xargs grep 'string'\n\n$ find . -iname '*something*'\n\n$ find *.html | xargs grep 'share_server.html'\n\n# \u641c\u5c0b\u7576\u524d\u76ee\u9304\u53ca\u5b50\u76ee\u9304\u4e0b\u7684\u542b\u6709 print() \u5b57\u4e32\u7684\u6a94\u6848\n$ grep -rnw \".\" -e \"print()\"\n\n$ grep -rnw \".\" -e \"print()\" --include=*.py<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/16956810\/how-do-i-find-all-files-containing-specific-text-on-linux\">https:\/\/stackoverflow.com\/questions\/16956810\/how-do-i-find-all-files-containing-specific-text-on-linux<\/a><\/p>\n<h2>Find Directories by Name<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ find . -type d -name \"*native*\" -print<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/askubuntu.com\/questions\/153144\/how-can-i-recursively-search-for-directory-names-with-a-particular-string-where\">https:\/\/askubuntu.com\/questions\/153144\/how-can-i-recursively-search-for-directory-names-with-a-particular-string-where<\/a><\/p>\n<h2>List Files by Date<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ ls -lrt<\/code><\/pre>\n<h2>List Files Opened by a Process<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ lsof | grep uwsgi\n\n$ lsof -i | grep LISTEN\n$ lsof -i -n -P | grep LISTEN<\/code><\/pre>\n<h2>Extract Content from a File<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ cat uwsgi.log | grep error<\/code><\/pre>\n<h2>Display Contents of All Files in the Current Directory<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ grep . *\n$ grep . *.html<\/code><\/pre>\n<h2>List Used Ports<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ netstat -a\n\n# TCP\n$ netstat -ntlp | grep uwsgi\n\n# UCP\n$ netstat -nulp<\/code><\/pre>\n<h2>Ping a Port<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ curl -I \"10.148.70.84:9200\"\n$ curl -I \"192.168.100.10:80\"\n\n$ sudo apt-get install nmap\n$ nmap -p 4505 54.250.5.176\n$ nmap -p 8000 10.10.100.70\n$ nmap -p 5672 10.10.100.82\n\n$ telnet 54.250.5.176 4505<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/12792856\/what-ports-does-rabbitmq-use\">http:\/\/stackoverflow.com\/questions\/12792856\/what-ports-does-rabbitmq-use<\/a><\/p>\n<h2>Show Network Traffic and Bandwidth<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ tcpdump -i eth0\n\n$ sudo apt-get install tcptrack\n$ tcptrack -i eth0<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/askubuntu.com\/questions\/257263\/how-to-display-network-traffic-in-terminal\">http:\/\/askubuntu.com\/questions\/257263\/how-to-display-network-traffic-in-terminal<\/a><\/p>\n<h2>List Running Processes<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\"># show all processes\n$ pstree -a\n\n# also show pid\n$ pstree -ap\n\n# \u5217\u51fa\u524d 10 \u500b\u6700\u4f54\u8a18\u61b6\u9ad4\u7684 processes\n$ ps aux | sort -nk +4 | tail\n\n# \u5217\u51fa mysql \u76f8\u95dc\u7684 processes\n$ ps aux | grep 'worker process'\n$ ps aux | grep uwsgi\n\n# \u6a39\u72c0\u986f\u793a\n$ ps auxf\n\n# \u641c\u5c0b process \u4e26\u4ee5\u6a39\u72c0\u7d50\u679c\u986f\u793a parent process\n$ ps f -opid,cmd -C python<\/code><\/pre>\n<h2>Kill Processes<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\"># \u5217\u51fa\u76ee\u524d\u6240\u6709\u7684\u6b63\u5728\u8a18\u61b6\u9ad4\u7576\u4e2d\u7684\u7a0b\u5e8f\n$ ps aux\n\n# \u5339\u914d\u5b57\u4e32\n$ ps aux | grep \"mongo\"\n\n# \u5e79\u6389\u5b83\n$ kill PID\n\n# kill all processes matching a name\n$ sudo pkill -f runserver<\/code><\/pre>\n<h2>Store User's Input as a Variable<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ read YOUR_VARIABLE_NAME\n\n$ read name\n# you type: vinta\n\n$ echo $name\nvinta<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/canred.blogspot.tw\/2013\/03\/read.html\">https:\/\/canred.blogspot.tw\/2013\/03\/read.html<\/a><\/p>\n<h2>Show Terminal Size<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ stty size\n$ echo $LINES &amp;&amp; echo $COLUMNS\n59 273 <\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/263890\/how-do-i-find-the-width-height-of-a-terminal-window\">https:\/\/stackoverflow.com\/questions\/263890\/how-do-i-find-the-width-height-of-a-terminal-window<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The cookbook of useful Linux commands.<\/p>\n","protected":false},"author":1,"featured_media":362,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[101,74],"class_list":["post-361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-devops","tag-cli-tool","tag-linux"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/362"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}