/bash.md

https://github.com/eremite/notes · Markdown · 184 lines · 132 code · 52 blank · 0 comment · 0 complexity · bb33af38f0b793f9c13bfa9c9b8849a8 MD5 · raw file

  1. # Notes on bash (and linux)
  2. ## If you forget where you are:
  3. ```bash
  4. hostname
  5. ```
  6. ## For disk usage analysis:
  7. ```bash
  8. sudo du -sh ./*
  9. df -h
  10. ```
  11. ## Count the number of files in a directory.
  12. ```bash
  13. find . -type f | wc -l
  14. ```
  15. ## Recursively find all files with a given extension
  16. (I can never remember `find` syntax.)
  17. ```bash
  18. find . -name *.rabl
  19. ```
  20. ## Sum the numbers in the first | delimited column in list.txt
  21. ```bash
  22. cut list.txt -d'|' -f1 | awk '{s+=$1} END {print s}'
  23. ```
  24. ## Rename extensions
  25. ```bash
  26. for i in *.php; do mv "$i" "${i/.php}".html; done
  27. ```
  28. ## Ubuntu stores its mail
  29. ```bash
  30. var/mail/username
  31. ```
  32. ## Show what distro you're using:
  33. ```bash
  34. lsb_release -a
  35. ```
  36. ## Show the latest exit code. 1 is true, 0 is false
  37. ```bash
  38. echo $?
  39. ```
  40. ## Bash shells and config files.
  41. ```
  42. A login shell is when you get the little introductory text like when using ssh. Opening up a console via gnome-terminal locally is not a login shell
  43. Pretty much everything is treated as an interactive shell, even scp.
  44. "When bash is invoked as an interactive login shell ... it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable."
  45. "When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist."
  46. ```
  47. ## To set up ssh bash_completion
  48. ```bash
  49. /etc/bash_completion.d/ssh
  50. ```
  51. ## Run something so that logging out won't kill it
  52. ```bash
  53. nohup bundle exec rake db:set_unit_names >> log/unit_names.log 2>&1 &
  54. ```
  55. ## Remove Old Kernels
  56. ```bash
  57. ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r`
  58. sudo aptitude remove $old_kernel
  59. ```
  60. ## Redirect a single page to ssl in a vhosts file
  61. ```bash
  62. Redirect /payments https://www.mokisystems.com/payments
  63. ```
  64. ## tar and gzip
  65. ```bash
  66. tar -czf filename.tar.gz directory # Create
  67. tar -xzf filename.tar.gz # Extract
  68. ```
  69. ## Renew dhcp lease
  70. ```bash
  71. sudo dhclient -r && sudo dhclient
  72. ```
  73. ## Set up a new server
  74. ```bash
  75. vim ~/.ssh/config
  76. SERVER=new_server
  77. ssh $SERVER
  78. adduser daniel
  79. vim /etc/group # add to moki group
  80. visudo # add to sudoers file ^K^U^U ^X
  81. exit
  82. ssh-copy-id $SERVER #make sure /home/daniel is not group writable.
  83. sync_server_dotfiles $SERVER
  84. ```
  85. ## Edit iptables
  86. ```bash
  87. sudo vim /etc/iptables.rules
  88. sudo iptables-restore < /etc/iptables.rules
  89. ```
  90. ## Grep installed packages
  91. ```bash
  92. dpkg --get-selections | grep pattern
  93. ```
  94. ## Password protect a file
  95. ```bash
  96. gpg -c db.sql.gz # encrypt
  97. gpg -d db.sql.gz.gpg > db.sql.gz # decrypt
  98. ```
  99. ## Restart delayed jobs based on current directory
  100. ```sh
  101. function rsdj {
  102. if [ $(basename `pwd`) == 'devstorage' ]; then
  103. sudo monit delayed_job_staging restart
  104. elif [ $(basename `pwd`) == 'storageunitsoftware' ]; then
  105. sudo monit -g delayed_job_production restart
  106. else
  107. echo 'delayed_jobs restart: Production or staging?'
  108. fi
  109. }
  110. ```
  111. ## Start `less` without control characters and line numbers
  112. ```sh
  113. less --RAW-CONTROL-CHARS --line-numbers
  114. ```
  115. ## Debug running a program
  116. ```sh
  117. export LD_DEBUG=files # or bindings, libs, versions
  118. program_to_debug
  119. ```
  120. ## Get Public Key from Private
  121. ```sh
  122. ssh-keygen -yf private_key_file > public_key_file
  123. ```
  124. ## Show fingerprint of public key file
  125. ```sh
  126. ssh-keygen -lf public_key_file
  127. ```
  128. ## Connect to Redis
  129. ```sh
  130. nc -v redis.endpoint.com 6379
  131. ping
  132. ```