{"id":13467036,"url":"https://github.com/arturoherrero/command-line-one-liners","last_synced_at":"2025-03-26T00:31:51.169Z","repository":{"id":12194396,"uuid":"14799121","full_name":"arturoherrero/command-line-one-liners","owner":"arturoherrero","description":"Command line one-liners.","archived":false,"fork":false,"pushed_at":"2019-10-08T10:59:39.000Z","size":24,"stargazers_count":396,"open_issues_count":0,"forks_count":50,"subscribers_count":30,"default_branch":"master","last_synced_at":"2024-10-29T20:34:21.061Z","etag":null,"topics":["bash","command-line","one-liners"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arturoherrero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-29T10:57:33.000Z","updated_at":"2024-10-21T12:55:35.000Z","dependencies_parsed_at":"2022-09-06T17:41:09.064Z","dependency_job_id":null,"html_url":"https://github.com/arturoherrero/command-line-one-liners","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoherrero%2Fcommand-line-one-liners","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoherrero%2Fcommand-line-one-liners/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoherrero%2Fcommand-line-one-liners/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoherrero%2Fcommand-line-one-liners/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arturoherrero","download_url":"https://codeload.github.com/arturoherrero/command-line-one-liners/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245566099,"owners_count":20636391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","command-line","one-liners"],"created_at":"2024-07-31T15:00:52.467Z","updated_at":"2025-03-26T00:31:50.464Z","avatar_url":"https://github.com/arturoherrero.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# Command line one-liners\n\nAfter my article about [command line one-liners][1], many people want to\ncontribute with their own commands. This is the place to do it, pull requests\nare welcome!\n\n## Table of Contents\n\n- [History](#history)\n- [Directories](#directories)\n- [Files](#files)\n- [Searching](#searching)\n- [Networking](#networking)\n- [User environment](#user-environment)\n- [File system](#file-system)\n- [Date \u0026 Time](#date--time)\n- [Processes](#processes)\n- [Miscellaneous](#miscellaneous)\n\n- - -\n\n### History\n\n- Run the last command\n\n  ```\n  $ !!\n  ```\n\n- Run the last command as root\n\n  ```\n  $ sudo !!\n  ```\n\n- Create a script of the last executed command\n\n  ```\n  $ echo \"!!\" \u003e script.sh\n  ```\n\n- Reuse all parameter of the previous command line\n\n  ```\n  $ echo cd .\n  $ !*\n  ```\n\n- Run the last command with some argument\n\n  ```\n  $ echo a b c d e\n  $ echo !!:2\n  $ echo !!:3-$\n  ```\n\n- Insert the last argument of the previous command\n\n  ```\n  $ cp script.sh /usr/bin/\n  $ cd !$\n  $ cd \u003cESC\u003e .\n  $ cd \u003cALT\u003e .\n  ```\n\n- Runs previous command but replacing\n\n  ```\n  $ echo no typos\n  $ ^typos^errors\n  ```\n\n- Escape any command aliases\n\n  ```\n  $ alias ls=\"ls -a\"\n  $ \\ls\n  ```\n\n- Run a command from the history\n\n  ```\n  $ history\n     ...\n     1225  ls -l\n     1226  git status\n     1227  history\n  $ !-3\n  $ !1225\n  ```\n\n- Search the history for the most recent command beginning with `text`\n\n  ```\n  $ !text\n  ```\n\n- Search the history for the most recent command containing `text`\n\n  ```\n  $ \u003cctrl-r\u003etext\n  ```\n\n- List of commands you use most often\n\n  ```\n  $ history | awk '{print $2}' | sort | uniq -c | sort -rn | head\n  ```\n\n- Execute a command without saving it in the history\n\n  ```\n  $ \u003cspace\u003ecommand\n  ```\n\n\n### Directories\n\n- Make a directory creating intermediate directories\n\n  ```\n  $ mkdir -p a/long/directory/path\n  ```\n\n- Create a directory and change into it\n\n  ```\n  $ mkdir dir \u0026\u0026 cd $_\n  ```\n\n- Change to the previous working directory\n\n  ```\n  $ cd -\n  ```\n\n- Jump to a directory. Execute a command in a subshell. Jump back to current directory\n\n  ```\n  $ (cd /tmp \u0026\u0026 ls)\n  ```\n\n\n### Files\n\n- Quickly rename a file\n\n  ```\n  $ mv filename.{old,new}\n  $ mv filename.{png,jpg}\n  ```\n\n- Create a quick back-up copy of a file\n\n  ```\n  $ cp file.txt{,.bak}\n  ```\n\n- Create a simple text file from command line\n\n  ```\n  $ cat \u003e file.txt\n  {your text here}\n  {your text here}\n  \u003cctrl-d\u003e\n  ```\n\n- Create a simple text file from command line or script (`EOF` is just a token, can be any word)\n\n  ```\n  $ cat \u003e file.txt \u003c\u003c EOF\n  {your text here}\n  {your text here}\n  EOF\n  ```\n\n- Empty a file from command line (usefull to truncate log file from running processes)\n\n  ```\n  $ \u003e file.txt\n  ```\n\n- Empty a file from command line or script\n\n  ```\n  $ cat /dev/null \u003e file.txt\n  ```\n\n- Make `less` behave like `tail -f`\n\n  ```\n  $ less +F somelogfile\n  ```\n\n- Display line numbers in a file\n\n  ```\n  $ cat -n file\n  $ less -N file\n  ```\n\n- Redirect standard input to a file. Print it to standard output\n\n  ```\n  $ command | tee file.txt | less\n\n    ┌─────────┐  ┌─────────┐  ┌─────────┐\n    │ command │─▸│   tee   │─▸│ stdout  │\n    └─────────┘  └────┬────┘  └─────────┘\n                      │\n                      ▾\n                ┌───────────┐\n                │   file    │\n                └───────────┘\n  ```\n\n\n### Searching\n\n- Search for a \u003cpattern\u003e string inside all files in the current directory\n\n  ```\n  $ grep -RnsI --color=auto \u003cpattern\u003e *\n  ```\n\n- Beyond grep\n\n  ```\n    _   /|\n    \\'o.O'\n    =(___)=\n      U    ack!\n\n  $ ack \u003cpattern\u003e\n  ```\n\n- Recursively remove all empty directories\n\n  ```\n  $ find . -type d -empty -delete\n  ```\n\n\n### Networking\n\n- Serve current directory tree at `http://$HOSTNAME:8000/`\n\n  ```\n  $ python -m SimpleHTTPServer\n  $ ruby -run -e httpd . -p 8000\n  ```\n\n- Share a file between two computers\n\n  ```\n  receiver $ nc -l 5566 \u003e data-dump.sql\n  sender   $ nc \u003creceiver-ip-address\u003e 5566 \u003c data-dump.sql\n  ```\n\n- Share a BIG file between two computers and show progress bar\n\n  ```\n  receiver $ nc -l 5566 \u003e big-file.iso\n  sender   $ pv big-file.iso | nc \u003creceiver-ip-address\u003e 5566\n  ```\n\n- Transfer a folder between two computers\n\n  ```\n  receiver $ nc -l 5566 | tar -zxv\n  sender   $ tar -zcv \u003cfolder\u003e | nc -w1 \u003creceiver-ip-address\u003e 5566\n  ```\n\n- Download an entire website\n\n  ```\n  $ wget -m -k http://website.com\n  ```\n\n### User environment\n\n- Show `PATH` in a human-readable way\n\n  ```\n  $ echo $PATH | tr ':' '\\n'\n  $ tr ':' '\\n' \u003c\u003c\u003c $PATH\n  ```\n\n- Clear the terminal screen\n\n  ```\n  $ \u003cctrl-l\u003e\n  $ clear\n  ```\n\n- Salvage a borked terminal\n\n  ```\n  $ reset\n  ```\n\n- Close shell keeping all subprocess running\n\n  ```\n  $ disown -a \u0026\u0026 exit\n  ```\n\n- Run a command immune to hangups\n\n  ```\n  $ nohup command \u0026\n  ```\n\n\n### Networking\n\n- Attach screen over ssh\n\n  ```\n  $ ssh user@host -t screen -r\n  ```\n\n- Compare a remote file with a local file\n\n  ```\n  $ ssh user@host cat /path/to/remotefile | diff /path/to/localfile -\n  ```\n\n- Get your public IP address\n\n  ```\n  $ curl ifconfig.me\n  ```\n\n- Set audible alarm when an IP address comes online\n\n  ```\n  $ ping -a IP_address\n  ```\n\n- List programs with open ports and connections\n\n  ```\n  $ lsof -i\n  ```\n\n- Check which process is listening on a specific port\n\n  ```\n  $ netstat -nlp | grep 8080\n  $ netstat -nlp tcp | grep 8080 (BSD)\n  ```\n\n\n### File system\n\n- Currently mounted filesystems in nice layout\n\n  ```\n  $ mount | column -t\n  ```\n\n- Display free disk space\n\n  ```\n  $ df -h\n  ```\n\n- Display disk usage statistics for the current directory\n\n  ```\n  $ du -sh *\n  ```\n\n- Display 10 biggest files/folders for the current directory\n\n  ```\n  $ du -s * | sort -nr | head\n  ```\n\n- Create a zip archive of a directory\n\n  ```\n  $ zip -r archive.zip directory\n  ```\n\n- Extract compressed archive\n\n  ```\n  $ unzip archive.zip\n  ```\n\n- Show File System Hierarchy\n\n  ```\n  $ man hier\n  ```\n\n\n### Date \u0026 Time\n\n- Shutdown the system at a given time\n\n  ```\n  $ shutdown -h now\n  $ shutdown -h 22:49\n  ```\n\n- Execute a command at a given time\n\n  ```\n  $ echo \"ls -l\" | at midnight\n  ```\n\n- Simple stopwatch\n\n  ```\n  $ time read\n  \u003cctrl-d\u003e\n  ```\n\n- Put a console clock in top right corner\n\n  ```\n  $ while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done \u0026\n  ```\n\n- Displays a calendar\n\n  ```\n  $ cal 12 1984\n  ```\n\n- What day was yesterday or will it be tomorrow, etc...\n\n  ```\n  $ date -d yesterday\n  $ date -d tomorrow +%Y-%m-%d\n  $ date -d \"7 days ago\" +%Y-%m-%d\n  $ date -j -v-1d (BSD)\n  ```\n\n\n### Processes\n\n- Display the top ten running processes. (Sorted by memory usage)\n\n  ```\n  $ ps aux | sort -nk +4 | tail\n  ```\n\n- Kill all Ruby processes\n\n  ```\n  $ ps aux | grep ruby | awk '{ print $2 }' | xargs kill -9\n  $ ps aux | awk '/ruby/ \u0026\u0026 ! /awk/ { system(\"kill -9 \"$2) }'\n  $ pkill -f ruby\n  $ killall -9 ruby\n  ```\n\n- Check which process is modifying a certain directory or file\n\n  ```\n  $ auditctl -w /path/to/directory -p war\n  ```\n\n  See results\n\n  ```\n  $ ausearch -f /path/to/directory\n  ```\n\n\n### Miscellaneous\n\n- 32 bits or 64 bits?\n\n  ```\n  $ getconf LONG_BIT\n  ```\n\n- Quick access to the ascii table\n\n  ```\n  $ man ascii\n  ```\n\n- Count your commits\n\n  ```\n  $ git shortlog -sn\n  ```\n\n- Russian Roulette in Bash (remember kids don't try this at home)\n\n  ```\n  $ [ $[ $RANDOM % 6 ] == 0 ] \u0026\u0026 rm -rf / || echo \"You live\"\n  ```\n\n- Watch Star Wars via telnet\n\n  ```\n  $ telnet towel.blinkenlights.nl\n  ```\n\n\n## Who made this?\n\nThis was made by Arturo Herrero under the MIT License. Find me on Twitter\n[@ArturoHerrero][2].\n\n\n[1]: http://arturoherrero.com/command-line-one-liners/\n[2]: https://twitter.com/ArturoHerrero\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturoherrero%2Fcommand-line-one-liners","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farturoherrero%2Fcommand-line-one-liners","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturoherrero%2Fcommand-line-one-liners/lists"}