├── .editorconfig ├── LICENSE └── README.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{package.json,*.yml}] 12 | indent_style = space 13 | indent_size = 2 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Siddharth Kannan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # one-liners 2 | 3 | > Bash one-liners that are always useful :heart: :heart: 4 | 5 | ### TOC 6 | 7 | - [Why](#why) 8 | - [List](#init) 9 | - [Concepts](#concepts) 10 | - [Commands](#commands) 11 | - [Docker](#docker) 12 | 13 | ### Why? 14 | 15 | There are [many](https://github.com/jlevy/the-art-of-command-line#basics) 16 | [oneliners](https://github.com/stephenturner/oneliners) 17 | [repositories](https://github.com/congto/oneliners) that more or less have a 18 | ton of oneliners. This is a list of the commands that I use the most. 19 | 20 | ### `init` 21 | 22 | #### Concepts 23 | 24 | > These will help in building other commands 25 | 26 | - Generate a sequence 27 | ```sh 28 | seq 1 100 29 | ``` 30 | 31 | - Run a command N times, with a sleep of T seconds in between 32 | ```sh 33 | for i in `seq 1 N`; do 34 | command; 35 | sleep T; 36 | done; 37 | ``` 38 | 39 | 40 | #### Commands 41 | 42 | > This is the real deal! :fire: 43 | 44 | - Run `hub ci-status` for a 100 times with a sleep of 2 seconds, right after `git push`. 45 | ```sh 46 | git push origin master && for i in `seq 1 100`; do hub ci-status; sleep 2; done; 47 | ``` 48 | 49 | - Git clone a really huge repository, using a shallow clone which is deepened in stages 50 | ```sh 51 | git clone --depth 1 REMOTE_URL folder; 52 | cd folder; 53 | for i in `seq 1 100`; do git fetch --depth=$i; done; 54 | ``` 55 | 56 | - SFTP `put` a file into a remote server, in one single command (SFTP shell doesn't have TAB autocompletion) 57 | ```sh 58 | sftp -P $PORT$ $USERNAME$@$SERVER$ <<< 'put $FILENAME$' 59 | ``` 60 | 61 | - Check what certificate an APK file has been signed with 62 | ```sh 63 | jarsigner -verify -verbose -certs apk-name.apk | less 64 | ``` 65 | 66 | - List all the keys in a keystore file (Android) 67 | ```sh 68 | keytool -list -v -keystore $FILENAME$.jks 69 | ``` 70 | 71 | - Replace target regular expression with replacement (using captured groups) with GNU `sed` (or 72 | `gsed`) 73 | 74 | ```sh 75 | gsed -ie "s/some \(text\)/\U\1/g" *.xml 76 | ``` 77 | 78 | `sed` uses basic regular expressions by default, hence the group capturing parentheses 79 | [MUST](http://stackoverflow.com/a/24717687/2080089) be escaped. `sed`'s options to use extended 80 | regexp can be turned on using the `-E` flag. The above command would become: 81 | 82 | ```sh 83 | $ echo "some text here" | gsed -E "s/some (text)/\U\1/g" 84 | TEXT here 85 | ``` 86 | 87 | `sed` can be used with characters other than `/` as the separating character for the 88 | commands. This is clearer in some contexts. Especially, when the text to be replaced has a forward 89 | slash itself. 90 | 91 | ```sh 92 | $ echo "https://example.com" | gsed 's#/#-#g' 93 | https:--example.com 94 | ``` 95 | 96 | `sed` prints the pattern space (i.e. input stream) by default. This can be annoying if you want to 97 | print the output of a transform _only_ when the pattern that we are searching for exists in the 98 | input. 99 | 100 | ```sh 101 | $ echo "this is not a URL" | gsed 's#http://#https://#g' 102 | this is not a URL 103 | 104 | $ echo "http://example.com" | gsed 's#http://#https://#g' 105 | https://example.com 106 | ``` 107 | 108 | Compare this confusing output which transforms the input if the input is found and prints it 109 | without applying the transform when the pattern is not found, to the following output which prints 110 | something to the console **only** when the transform is applied. **Note** that here the expression 111 | _must_ have the `p` command, which tells `sed` to explicitly print the output of the previous 112 | `s///g` command. 113 | 114 | ```sh 115 | $ echo "this is not a URL" | gsed -n 's#http://#https://#gp' 116 | # Empty output 117 | 118 | $ echo "http://example.com" | gsed -n 's#http://#https://#gp' 119 | https://example.com 120 | ``` 121 | 122 | - Curl command to run commands through a SOCKS proxy (setup either through TOR 123 | or ssh tunneling) 124 | ```sh 125 | curl --socks5 localhost:9050 http://icanhazip.com 126 | ``` 127 | 128 | ```sh 129 | curl --socks5 localhost:9050 https://check.torproject.org > index.html 130 | $EDITOR index.html 131 | ``` 132 | 133 | **NOTE:** This is especially useful when trying to figure out whether the 134 | socks5 proxy is working properly. 135 | 136 | - Iterating over files inside `bash` 137 | ```sh 138 | for i in *.caf; do 139 | ffmpeg -i "$i" "$(echo "$i" | cut -d . -f 1)".wav 140 | done 141 | ``` 142 | 143 | **Note:** Check [this](http://unix.stackexchange.com/a/131767/36994) Unix 144 | StackExchange answer for more details about whitespaces, quoting and bash. 145 | 146 | 147 | - List the packages that are installed on your system with `dpkg` 148 | ```sh 149 | dpkg --get-selections | less 150 | ``` 151 | 152 | - List all the folders, in ascending order of size 153 | ```sh 154 | du -h --max-depth=1 | sort -h 155 | ``` 156 | 157 | - Removes a kernel module and then re-adds it. Doing this for `usbhid`, fixes 158 | problems in Ubuntu 16.04 LTS related to the mouse or other USB peripherals 159 | ```sh 160 | sudo modprobe -r usbhid && sleep 1 && sudo modprobe usbhid 161 | ``` 162 | 163 | - Convert between AV formats using `ffmpeg` 164 | ```sh 165 | ffmpeg -i input_file.mp4 output_file.mp3 166 | ``` 167 | 168 | Common formats are accepted, such as: avi, wav, mp3, mp4, mkv etc. (Audio, 169 | Video streams etc are inferred on the basis of the output file name) 170 | 171 | - Watch a file get appended using `tail` 172 | ```sh 173 | tail -f ~/test.log 174 | ``` 175 | 176 | - Use `vlc` to play a random file from a recursive list of _all_ files 177 | ```sh 178 | vlc "`find . -not -type d | shuf | head -n1`" 179 | ``` 180 | 181 | **Note:** It is inherently assumed that the subtree of the folder from which 182 | this command is being run doesn't contain any files that are not playable by 183 | VLC, such as txt, doc, etc. This oneliner _can_ be enhanced to ensure that 184 | only video files are found and played. If you find an elegant way to do 185 | that, please open a PR! 186 | 187 | - List all the files in a folder except the most recently modified one 188 | ```sh 189 | ls -tr | head -n $((`ls -tr | wc -l`-1)) 190 | ``` 191 | 192 | - Move current window to the top in `tmux` 193 | ```sh 194 | move-window -t 0 195 | ``` 196 | 197 | - Swap windows N1 and N2 inside `tmux` 198 | ```sh 199 | swap-window -t 0 200 | swap-window -s N1 -t N2 201 | ``` 202 | 203 | - Show progress while creating a gzipped archive using tar and gzip - using [pv][1] 204 | ```sh 205 | tar cf - FOLDER | pv -cN compression -s `du -sb FOLDER | cut -f1` | gzip -9 > 1A.tar.gz 206 | ``` 207 | 208 | - Show progress while encrypting a file using GPG 209 | ```sh 210 | pv FILE | gpg --symmetric --passphrase "test" > FILE.gpg 211 | ``` 212 | 213 | - Re-attach the top session of `screen` in the output of `screen -ls` 214 | ```sh 215 | screen -r `screen -ls | head -n-1 | tail -n-1 | awk '{ print $1 }'` 216 | ``` 217 | 218 | - Encrypt a file using the ChaCha20 stream cipher (using `openssl`) 219 | ```sh 220 | KEY=$(uuidgen); echo $KEY > /tmp/1.key 221 | echo "test" > /tmp/1.in 222 | openssl enc -chacha20 -kfile /tmp/1.key -pbkdf2 -base64 -out /tmp/1.out < /tmp/1.in; 223 | ``` 224 | 225 | - Using `pdftk` to concatenate multiple files ("Stapler, hole-punch, binder for PDF files") 226 | ```sh 227 | # Append multiple PDF files together 228 | pdftk 1.pdf 2.pdf cat output output.pdf 229 | ``` 230 | 231 | - Using `pdftk` to concatenate only a few pages from one file or to get pages from various files in 232 | the desired order 233 | 234 | ```sh 235 | # Get only pages 1 through 4 (both inclusive) of a 7 page PDF 236 | pdftk 1.pdf cat 1-4 output out.pdf 237 | 238 | # Get the pages of 2 2-page PDF files interleaved 239 | pdftk A=1.pdf B=2.pdf cat A1 B1 A2 B2 output out.pdf 240 | ``` 241 | 242 | - Using `pdftk` to rotate pages in PDF files 243 | ```sh 244 | # Rotate a range of pages in PDF files. Other files will be left unchanged and passed through 245 | # as-is. 246 | # 247 | # This will create an output PDF with the second and third pages rotated 90 degrees 248 | # anti-clockwise. All other pages in in.pdf will retain their original orientation. 249 | $ pdftk in.pdf rotate 2-3left output out.pdf 250 | 251 | # Rotate pages and concatenate multiple PDFs simultaneously 252 | # 253 | # This will create output PDF with 2 pages: the first page of the file in1.pdf, rotated left (90 degrees 254 | # CCW) and the first page of in2.pdf, rotated right (90 degrees CW) 255 | $ pdftk A=in1.pdf B=in2.pdf cat A1left B1right output out.pdf 256 | ``` 257 | 258 | 259 | - Using `pdftk` to markup PDF files 260 | ```sh 261 | # Markup PDF files 262 | ## Burst open PDF file into its composite pages 263 | pdftk input.pdf burst 264 | 265 | ## Markup each PDF file using Gimp 266 | gimp pg*.pdf 267 | 268 | ## Put the PDF files back together using pdftk 269 | pdftk pg*.pdf cat output output.pdf 270 | ``` 271 | 272 | - Using `qpdf` to decrypt files which have [a password](https://askubuntu.com/a/828727) 273 | ```sh 274 | qpdf -password= -decrypt /path/to/secured.pdf out.pdf 275 | ``` 276 | 277 | - Using `ghostscript` to [reduce the size of PDF 278 | files](https://linuxaria.com/pills/resize-pdfs-from-the-command-line-in-linux) 279 | 280 | ```sh 281 | $ ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf 282 | ``` 283 | 284 | There are multiple levels of compression. From highest to lowest: 285 | 286 | 1. `/screen` or `/default` (72 DPI) (Smallest file size) 287 | 2. `/ebook` (150 DPI) 288 | 3. `/printer` (300 DPI) 289 | 4. `/prepress` (color preserving, 300 DPI) 290 | 291 | `/ebook` works well in most cases. It was useful in compressing the size of PDFs which contained a 292 | single image, which was quite big. (**Example:** 8 MB => using `/ebook` => 0.8 MB.) 293 | 294 | #### Docker 295 | 296 | - Serving the current directory on the local network using `nginx` 297 | ```sh 298 | $ cat test.conf 299 | server { 300 | listen 9091; 301 | server_name localhost; 302 | 303 | location / { 304 | root /var/www/html; 305 | index index.html index.htm; 306 | 307 | # ngx_http_access_module 308 | allow 10.0.0.0/24; 309 | deny all; 310 | } 311 | } 312 | 313 | $ docker run -p 9090:9091 \ 314 | -v "`pwd`:/var/www/html" \ 315 | -v "`pwd`/test.conf:/etc/nginx/conf.d/localserver.conf" \ 316 | -d --name localserver nginx:latest 317 | ``` 318 | 319 | - Converting a markdown file to an HTML file 320 | ```sh 321 | docker run \ 322 | -v `pwd`:/source jagregory/pandoc \ 323 | -f markdown -t html5 \ 324 | scratch/scratch-2019-09-17-22-33-55-z-review.md -o z-review.html 325 | ``` 326 | 327 | - Run `htop` on a host 328 | ```sh 329 | docker run -it --pid=host jonbaldie/htop 330 | ``` 331 | 332 | - Run a squid proxy server on port 3128 333 | ```sh 334 | # Prepare the configuration file 335 | docker run --rm sameersbn/squid \ 336 | cat /etc/squid/squid.conf > squid.conf 337 | # Edit the configuration file 338 | # Start the proxy server using this configuration 339 | docker run --name squid -d --restart=always \ 340 | --publish 3128:3128 \ 341 | -v "$PWD/squid.conf":/etc/squid/squid.conf \ 342 | sameersbn/squid 343 | ``` 344 | 345 | - Get the TLS (HTTPS) certificate for a domain 346 | ```sh 347 | # Print the certificate as human readable text 348 | openssl s_client -tls1_2 -connect duckduckgo.com:443 < /dev/null 2> /dev/null | openssl x509 -text 349 | 350 | # Print the certificate for a given host name if the domain has multiple TLS 351 | # hosts 352 | openssl s_client -tls1_2 -connect duckduckgo.com:443 -servername duckduckgo.com < /dev/null 2> /dev/null | openssl x509 -text 353 | 354 | # Check if the certificate is going to expire in the next 30 seconds 355 | openssl s_client -tls1_2 -connect duckduckgo.com:443 < /dev/null 2> /dev/null | openssl x509 -checkend 30 356 | ``` 357 | 358 | - Find a regular expression and print the first captured group based on some condition (using Perl) 359 | 360 | ```sh 361 | $ cat < 10 and print $1' 362 | count: 50 363 | count: 9 364 | test: 10 365 | EOF 366 | 50 367 | ``` 368 | 369 | We are using 4 Perl options. 370 | 371 | 1. `-l`: Enable line-by-line processing 372 | 2. `-a`: Enable autosplit mode (the input line is split at whitespace characters and the result is 373 | stored in the array `@F` 374 | 3. `-n`: The given expression is put inside a loop in which each line is processed 375 | sequentially. Using this option, the final program is equivalent to: `while(<>) { ... given 376 | expression ... }` 377 | 4. `-e`: Provide a single line of the Perl script 378 | 379 | [1]: https://www.ivarch.com/programs/quickref/pv.shtml 380 | --------------------------------------------------------------------------------