{"id":16102724,"url":"https://github.com/icyflame/oneliners","last_synced_at":"2025-03-17T17:31:29.922Z","repository":{"id":150350431,"uuid":"59853510","full_name":"icyflame/oneliners","owner":"icyflame","description":"Bash one-liners that are always useful :heart:","archived":false,"fork":false,"pushed_at":"2024-08-16T01:38:26.000Z","size":21,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T02:24:07.783Z","etag":null,"topics":["aliases","bash","oneliners","scripts","shell"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/icyflame.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-27T17:58:24.000Z","updated_at":"2024-10-02T22:22:57.000Z","dependencies_parsed_at":"2023-07-27T07:01:13.485Z","dependency_job_id":null,"html_url":"https://github.com/icyflame/oneliners","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/icyflame%2Foneliners","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyflame%2Foneliners/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyflame%2Foneliners/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyflame%2Foneliners/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icyflame","download_url":"https://codeload.github.com/icyflame/oneliners/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243872484,"owners_count":20361493,"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":["aliases","bash","oneliners","scripts","shell"],"created_at":"2024-10-09T18:54:32.910Z","updated_at":"2025-03-17T17:31:29.610Z","avatar_url":"https://github.com/icyflame.png","language":null,"readme":"# one-liners\n\n\u003e Bash one-liners that are always useful :heart: :heart:\n\n### TOC\n\n- [Why](#why)\n- [List](#init)\n  - [Concepts](#concepts)\n  - [Commands](#commands)\n  - [Docker](#docker)\n\n### Why?\n\nThere are [many](https://github.com/jlevy/the-art-of-command-line#basics)\n[oneliners](https://github.com/stephenturner/oneliners)\n[repositories](https://github.com/congto/oneliners) that more or less have a\nton of oneliners. This is a list of the commands that I use the most.\n\n### `init`\n\n#### Concepts\n\n\u003e These will help in building other commands\n\n- Generate a sequence\n  ```sh\n  seq 1 100\n  ```\n\n- Run a command N times, with a sleep of T seconds in between\n  ```sh\n  for i in `seq 1 N`; do\n    command;\n    sleep T;\n  done;\n  ```\n\n\n#### Commands\n\n\u003e This is the real deal! :fire:\n\n- Run `hub ci-status` for a 100 times with a sleep of 2 seconds, right after `git push`.\n  ```sh\n  git push origin master \u0026\u0026 for i in `seq 1 100`; do hub ci-status; sleep 2; done;\n  ```\n\n- Git clone a really huge repository, using a shallow clone which is deepened in stages\n  ```sh\n  git clone --depth 1 REMOTE_URL folder;\n  cd folder;\n  for i in `seq 1 100`; do git fetch --depth=$i; done;\n  ```\n\n- SFTP `put` a file into a remote server, in one single command (SFTP shell doesn't have TAB autocompletion)\n  ```sh\n  sftp -P $PORT$ $USERNAME$@$SERVER$ \u003c\u003c\u003c 'put $FILENAME$'\n  ```\n\n- Check what certificate an APK file has been signed with\n  ```sh\n  jarsigner -verify -verbose -certs apk-name.apk | less\n  ```\n\n- List all the keys in a keystore file (Android)\n  ```sh\n  keytool -list -v -keystore $FILENAME$.jks\n  ```\n\n- Replace target regular expression with replacement (using captured groups) with GNU `sed` (or\n  `gsed`)\n\n  ```sh\n  gsed -ie \"s/some \\(text\\)/\\U\\1/g\" *.xml\n  ```\n\n  `sed` uses basic regular expressions by default, hence the group capturing parentheses\n  [MUST](http://stackoverflow.com/a/24717687/2080089) be escaped. `sed`'s options to use extended\n  regexp can be turned on using the `-E` flag. The above command would become:\n\n  ```sh\n  $ echo \"some text here\" | gsed -E \"s/some (text)/\\U\\1/g\"\n  TEXT here\n  ```\n\n  `sed` can be used with characters other than `/` as the separating character for the\n  commands. This is clearer in some contexts. Especially, when the text to be replaced has a forward\n  slash itself.\n\n  ```sh\n  $ echo \"https://example.com\" | gsed 's#/#-#g'\n  https:--example.com\n  ```\n\n  `sed` prints the pattern space (i.e. input stream) by default. This can be annoying if you want to\n  print the output of a transform _only_ when the pattern that we are searching for exists in the\n  input.\n\n  ```sh\n  $ echo \"this is not a URL\" | gsed 's#http://#https://#g'\n  this is not a URL\n\n  $ echo \"http://example.com\" | gsed 's#http://#https://#g'\n  https://example.com\n  ```\n\n  Compare this confusing output which transforms the input if the input is found and prints it\n  without applying the transform when the pattern is not found, to the following output which prints\n  something to the console **only** when the transform is applied. **Note** that here the expression\n  _must_ have the `p` command, which tells `sed` to explicitly print the output of the previous\n  `s///g` command.\n\n  ```sh\n  $ echo \"this is not a URL\" | gsed -n 's#http://#https://#gp'\n  # Empty output\n\n  $ echo \"http://example.com\" | gsed -n 's#http://#https://#gp'\n  https://example.com\n  ```\n\n- Curl command to run commands through a SOCKS proxy (setup either through TOR\n  or ssh tunneling)\n  ```sh\n  curl --socks5 localhost:9050 http://icanhazip.com\n  ```\n\n  ```sh\n  curl --socks5 localhost:9050 https://check.torproject.org \u003e index.html\n  $EDITOR index.html\n  ```\n\n  **NOTE:** This is especially useful when trying to figure out whether the\n  socks5 proxy is working properly.\n\n- Iterating over files inside `bash`\n  ```sh\n  for i in *.caf; do\n  ffmpeg -i \"$i\" \"$(echo \"$i\" | cut -d . -f 1)\".wav\n  done\n  ```\n\n  **Note:** Check [this](http://unix.stackexchange.com/a/131767/36994) Unix\n  StackExchange answer for more details about whitespaces, quoting and bash.\n\n\n- List the packages that are installed on your system with `dpkg`\n  ```sh\n  dpkg --get-selections | less\n  ```\n\n- List all the folders, in ascending order of size\n  ```sh\n  du -h --max-depth=1 | sort -h\n  ```\n\n- Removes a kernel module and then re-adds it. Doing this for `usbhid`, fixes\nproblems in Ubuntu 16.04 LTS related to the mouse or other USB peripherals\n  ```sh\n  sudo modprobe -r usbhid \u0026\u0026 sleep 1 \u0026\u0026 sudo modprobe usbhid\n  ```\n\n- Convert between AV formats using `ffmpeg`\n  ```sh\n  ffmpeg -i input_file.mp4 output_file.mp3\n  ```\n\n  Common formats are accepted, such as: avi, wav, mp3, mp4, mkv etc. (Audio,\n  Video streams etc are inferred on the basis of the output file name)\n\n- Watch a file get appended using `tail`\n  ```sh\n  tail -f ~/test.log\n  ```\n\n- Use `vlc` to play a random file from a recursive list of _all_ files\n  ```sh\n  vlc \"`find . -not -type d | shuf | head -n1`\"\n  ```\n\n  **Note:** It is inherently assumed that the subtree of the folder from which\n  this command is being run doesn't contain any files that are not playable by\n  VLC, such as txt, doc, etc. This oneliner _can_ be enhanced to ensure that\n  only video files are found and played. If you find an elegant way to do\n  that, please open a PR!\n\n- List all the files in a folder except the most recently modified one\n  ```sh\n  ls -tr | head -n $((`ls -tr | wc -l`-1))\n  ```\n\n- Move current window to the top in `tmux`\n  ```sh\n  move-window -t 0\n  ```\n\n- Swap windows N1 and N2 inside `tmux`\n  ```sh\n  swap-window -t 0\n  swap-window -s N1 -t N2\n  ```\n\n- Show progress while creating a gzipped archive using tar and gzip - using [pv][1]\n  ```sh\n  tar cf - FOLDER | pv -cN compression -s `du -sb FOLDER | cut -f1` | gzip -9 \u003e 1A.tar.gz\n  ```\n\n- Show progress while encrypting a file using GPG\n  ```sh\n  pv FILE | gpg --symmetric --passphrase \"test\" \u003e FILE.gpg\n  ```\n\n- Re-attach the top session of `screen` in the output of `screen -ls`\n  ```sh\n  screen -r `screen -ls | head -n-1 | tail -n-1 | awk '{ print $1 }'`\n  ```\n\n- Encrypt a file using the ChaCha20 stream cipher (using `openssl`)\n  ```sh\n  KEY=$(uuidgen); echo $KEY \u003e /tmp/1.key\n  echo \"test\" \u003e /tmp/1.in\n  openssl enc -chacha20 -kfile /tmp/1.key -pbkdf2 -base64 -out /tmp/1.out \u003c /tmp/1.in;\n  ```\n\n- Using `pdftk` to concatenate multiple files (\"Stapler, hole-punch, binder for PDF files\")\n  ```sh\n  # Append multiple PDF files together\n  pdftk 1.pdf 2.pdf cat output output.pdf\n  ```\n\n- Using `pdftk` to concatenate only a few pages from one file or to get pages from various files in\n  the desired order\n\n  ```sh\n  # Get only pages 1 through 4 (both inclusive) of a 7 page PDF\n  pdftk 1.pdf cat 1-4 output out.pdf\n\n  # Get the pages of 2 2-page PDF files interleaved\n  pdftk A=1.pdf B=2.pdf cat A1 B1 A2 B2 output out.pdf\n  ```\n\n- Using `pdftk` to rotate pages in PDF files\n  ```sh\n  # Rotate a range of pages in PDF files. Other files will be left unchanged and passed through\n  # as-is.\n  #\n  # This will create an output PDF with the second and third pages rotated 90 degrees\n  # anti-clockwise. All other pages in in.pdf will retain their original orientation.\n  $ pdftk in.pdf rotate 2-3left output out.pdf\n\n  # Rotate pages and concatenate multiple PDFs simultaneously\n  #\n  # This will create output PDF with 2 pages: the first page of the file in1.pdf, rotated left (90 degrees\n  # CCW) and the first page of in2.pdf, rotated right (90 degrees CW)\n  $ pdftk A=in1.pdf B=in2.pdf cat A1left B1right output out.pdf\n  ```\n\n\n- Using `pdftk` to markup PDF files\n  ```sh\n  # Markup PDF files\n  ## Burst open PDF file into its composite pages\n  pdftk input.pdf burst\n\n  ## Markup each PDF file using Gimp\n  gimp pg*.pdf\n\n  ## Put the PDF files back together using pdftk\n  pdftk pg*.pdf cat output output.pdf\n  ```\n\n- Using `qpdf` to decrypt files which have [a password](https://askubuntu.com/a/828727)\n  ```sh\n  qpdf -password=\u003cyour-password\u003e -decrypt /path/to/secured.pdf out.pdf\n  ```\n\n- Using `ghostscript` to [reduce the size of PDF\n  files](https://linuxaria.com/pills/resize-pdfs-from-the-command-line-in-linux)\n\n  ```sh\n  $ ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf\n  ```\n\n  There are multiple levels of compression. From highest to lowest:\n\n  1. `/screen` or `/default` (72 DPI) (Smallest file size)\n  2. `/ebook` (150 DPI)\n  3. `/printer` (300 DPI)\n  4. `/prepress` (color preserving, 300 DPI)\n\n  `/ebook` works well in most cases. It was useful in compressing the size of PDFs which contained a\n  single image, which was quite big. (**Example:** 8 MB =\u003e using `/ebook` =\u003e 0.8 MB.)\n\n#### Docker\n\n- Serving the current directory on the local network using `nginx`\n  ```sh\n  $ cat test.conf\n  server {\n      listen       9091;\n      server_name  localhost;\n\n      location / {\n          root   /var/www/html;\n          index  index.html index.htm;\n\n          # ngx_http_access_module\n          allow 10.0.0.0/24;\n          deny all;\n      }\n  }\n\n  $ docker run -p 9090:9091 \\\n    -v \"`pwd`:/var/www/html\" \\\n    -v \"`pwd`/test.conf:/etc/nginx/conf.d/localserver.conf\" \\\n    -d --name localserver nginx:latest\n  ```\n\n- Converting a markdown file to an HTML file\n  ```sh\n  docker run \\\n    -v `pwd`:/source jagregory/pandoc \\\n    -f markdown -t html5 \\\n    scratch/scratch-2019-09-17-22-33-55-z-review.md -o z-review.html\n  ```\n\n- Run `htop` on a host\n  ```sh\n  docker run -it --pid=host jonbaldie/htop\n  ```\n\n- Run a squid proxy server on port 3128\n  ```sh\n  # Prepare the configuration file\n  docker run --rm sameersbn/squid \\\n    cat /etc/squid/squid.conf \u003e squid.conf\n  # Edit the configuration file\n  # Start the proxy server using this configuration\n  docker run --name squid -d --restart=always \\\n    --publish 3128:3128 \\\n    -v \"$PWD/squid.conf\":/etc/squid/squid.conf \\\n    sameersbn/squid\n  ```\n\n- Get the TLS (HTTPS) certificate for a domain\n  ```sh\n  # Print the certificate as human readable text\n  openssl s_client -tls1_2 -connect duckduckgo.com:443 \u003c /dev/null 2\u003e /dev/null | openssl x509 -text\n\n  # Print the certificate for a given host name if the domain has multiple TLS\n  # hosts\n  openssl s_client -tls1_2 -connect duckduckgo.com:443 -servername duckduckgo.com \u003c /dev/null 2\u003e /dev/null | openssl x509 -text\n\n  # Check if the certificate is going to expire in the next 30 seconds\n  openssl s_client -tls1_2 -connect duckduckgo.com:443 \u003c /dev/null 2\u003e /dev/null | openssl x509 -checkend 30\n  ```\n\n- Find a regular expression and print the first captured group based on some condition (using Perl)\n\n  ```sh\n  $ cat \u003c\u003cEOF | perl -lane 'm!count: ([0-9]+)! and $1 \u003e 10 and print $1'\n  count: 50\n  count: 9\n  test: 10\n  EOF\n  50\n  ```\n\n  We are using 4 Perl options.\n\n  1. `-l`: Enable line-by-line processing\n  2. `-a`: Enable autosplit mode (the input line is split at whitespace characters and the result is\n     stored in the array `@F`\n  3. `-n`: The given expression is put inside a loop in which each line is processed\n     sequentially. Using this option, the final program is equivalent to: `while(\u003c\u003e) { ... given\n     expression ... }`\n  4. `-e`: Provide a single line of the Perl script\n\n[1]: https://www.ivarch.com/programs/quickref/pv.shtml\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyflame%2Foneliners","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficyflame%2Foneliners","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyflame%2Foneliners/lists"}