Advertisement
urosevic

Handy Bash Commands

Feb 13th, 2015
627
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### MySQL ###
  2. ## Handy gist - https://gist.github.com/hofmannsven/9164408 ##
  3.  
  4. # dump DB
  5. $ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
  6.  
  7. # dump DB and gzip
  8. $ mysqldump --opt -u [uname] -p[pass] [dbname] | gzip > [dbname]-$(date +%Y%m%d-%H%M%S).sql.gz
  9.  
  10. # import DB dump
  11. $ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
  12.  
  13. # import gzipped DB dump
  14. $ gunzip < [dump].sql.gz | mysql -u [uname] -p[pass]
  15.  
  16. # import DB dumped to signel WP files per table
  17. $ for sql in *.sql; do mysql -u [unape] -p[pass] [db_to_restore] <$sql; done
  18.  
  19. # import multiple SQL dumps in single run
  20. $ cat *.sql | mysql -u [user] -p [db_to_restore]
  21.  
  22. # access mysql shell
  23. $ mysql -u [user] -p
  24.  
  25. # show all databases
  26. > show databases;
  27.  
  28. # create new database
  29. > create database [database];
  30.  
  31. # select database
  32. > use [database];
  33.  
  34. # Determine what database is in use
  35. > select database();
  36.  
  37. # show all tables
  38. > show tables;
  39.  
  40. # show table structure
  41. > describe [table];
  42.  
  43. # inserting a record
  44. > INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');
  45.  
  46. # grant ALL access to user for * tables
  47. > GRANT ALL ON database.* TO 'user'@'localhost';
  48.  
  49. # updating records
  50. > UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];
  51.  
  52. ### Search ###
  53.  
  54. # List and delete .svn directories
  55. find . -name .svn -exec echo '{}' \;
  56. find . -name .svn -exec rm -rf '{}' \;
  57.  
  58. # Find recursively in current directory
  59. # 1) .JS and .CSS files
  60. # 2) modified in less than 1 minute
  61. # 3) not in paths that contains /cache/, /backupbuddy_, /plugins/ and /headway/
  62.  
  63. find . -type f -mmin -1 \( -name "*.js" -or -name "*.css" \) -not \( -path "*/cache/*" -or -path "*/backupbuddy_*/*" -or -path "*/plugins/*" -or -path "*/headway/*" \)
  64.  
  65. # Find all default WP themes
  66. $ find /home -type d -wholename '*/wp-content/themes/*' | grep -v 'cache' | egrep '\/wp-\content\/themes\/(default|twenty)' | egrep '(ten|teen|elewen|twelve|default)$'
  67.  
  68. # Find all W3TotalCache exception errors
  69. $ grep 'BrowserCache.php on line 191' /usr/local/apache/logs/error_log | grep '/home/' | sed 's/^\[.*\ in\ //' | sed 's/on\ line\ 191.*//' | sort | uniq | less
  70.  
  71. ### Archiving ###
  72.  
  73. # Pack to TAR from file list
  74. $ tar -cvf archive.tar -T filelist.txt
  75.  
  76. # List content of TAR.GZ w/o extracting archive
  77. $ tar -tf file.tar.gz
  78.  
  79. # Extract content of TAR.GZ archive
  80. $ tar -zxvf file.tar.gz
  81.  
  82. ### Permissions ###
  83.  
  84. # Chown recursively
  85. $ chown -R username. *
  86.  
  87. # Calculate size of currend directory
  88. $ du -chs
  89.  
  90. # Get working folder name
  91. $ echo ${PWD##*/}
  92.  
  93. # Set proper permissions in public_html
  94. $ find $PWD -type d -exec chmod 755 {} \;
  95. $ find $PWD -type f -exec chmod 644 {} \;
  96.  
  97. # Get username of any directory in /home on regular web server
  98. if [[ "$PWD" == "/home/"* ]]; then
  99.  USERNAME=$( pwd | cut -d "/" -f3 )
  100. fi
  101.  
  102. ### Scripting ###
  103.  
  104. # Prepend "some text" as 1st line to file - sed insert
  105. sed -i '1s/^/some text\n/' FILE_NAME.ext
  106.  
  107. # Get current BASH script directory path
  108. SCRIPT_DIR_PATH=$( dirname $0 )
  109.  
  110. # Check does file contain "some text" exact string in whole line
  111. if ! grep -Fxq 'some text' FILE_NAME.ext
  112. then
  113.  echo "file does not contain string"
  114. else
  115.  echo "file contains string"
  116. fi
  117.  
  118. ### Debugging ###
  119.  
  120. # Compare two folders but exclude specific path
  121. $ diff --exclude=directory/to/exclude -r dir1 dir2
  122.  
  123. # Compare files in two directories and print only file paths that differ
  124. diff -qr dir1 dir2
  125.  
  126. # Compare files in two directories and print only file path that is same
  127. diff -rs dir1 dir2 | egrep '^Files .+ and .+ are identical$'
Advertisement
RAW Paste Data Copied
Advertisement