urosevic icon

Handy Bash Commands

urosevic | PRO | 02/13/15 10:31:17 AM UTC | 0 ⭐ | 960 👁️ | Never ⏰ | []
Bash |

3.53 KB

|

None

|

0 👍

/

0 👎

### MySQL ###
## Handy gist - https://gist.github.com/hofmannsven/9164408 ##
 
# dump DB
$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
 
# dump DB and gzip
$ mysqldump --opt -u [uname] -p[pass] [dbname] | gzip > [dbname]-$(date +%Y%m%d-%H%M%S).sql.gz
 
# import DB dump
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
 
# import gzipped DB dump
$ gunzip < [dump].sql.gz | mysql -u [uname] -p[pass]
 
# import DB dumped to signel WP files per table
$ for sql in *.sql; do mysql -u [unape] -p[pass] [db_to_restore] <$sql; done
 
# import multiple SQL dumps in single run
$ cat *.sql | mysql -u [user] -p [db_to_restore]
 
# access mysql shell
$ mysql -u [user] -p
 
# show all databases
> show databases;
 
# create new database
> create database [database];
 
# select database
> use [database];
 
# Determine what database is in use
> select database();
 
# show all tables
> show tables;
 
# show table structure
> describe [table];
 
# inserting a record
> INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');
 
# grant ALL access to user for * tables
> GRANT ALL ON database.* TO 'user'@'localhost';
 
# updating records
> UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];
 
### Search ###
 
# List and delete .svn directories
find . -name .svn -exec echo '{}' \;
find . -name .svn -exec rm -rf '{}' \;
 
# Find recursively in current directory
# 1) .JS and .CSS files
# 2) modified in less than 1 minute
# 3) not in paths that contains /cache/, /backupbuddy_, /plugins/ and /headway/
 
find . -type f -mmin -1 \( -name "*.js" -or -name "*.css" \) -not \( -path "*/cache/*" -or -path "*/backupbuddy_*/*" -or -path "*/plugins/*" -or -path "*/headway/*" \)
 
# Find all default WP themes
$ find /home -type d -wholename '*/wp-content/themes/*' | grep -v 'cache' | egrep '\/wp-\content\/themes\/(default|twenty)' | egrep '(ten|teen|elewen|twelve|default)$'
 
# Find all W3TotalCache exception errors
$ 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
 
### Archiving ###
 
# Pack to TAR from file list
$ tar -cvf archive.tar -T filelist.txt
 
# List content of TAR.GZ w/o extracting archive
$ tar -tf file.tar.gz
 
# Extract content of TAR.GZ archive
$ tar -zxvf file.tar.gz
 
### Permissions ###
 
# Chown recursively
$ chown -R username. *
 
# Calculate size of currend directory
$ du -chs
 
# Get working folder name
$ echo ${PWD##*/}
 
# Set proper permissions in public_html
$ find $PWD -type d -exec chmod 755 {} \;
$ find $PWD -type f -exec chmod 644 {} \;
 
# Get username of any directory in /home on regular web server
if [[ "$PWD" == "/home/"* ]]; then
  USERNAME=$( pwd | cut -d "/" -f3 )
fi
 
### Scripting ###
 
# Prepend "some text" as 1st line to file - sed insert
sed -i '1s/^/some text\n/' FILE_NAME.ext
 
# Get current BASH script directory path
SCRIPT_DIR_PATH=$( dirname $0 )
 
# Check does file contain "some text" exact string in whole line
if ! grep -Fxq 'some text' FILE_NAME.ext
then
  echo "file does not contain string"
else
  echo "file contains string"
fi
 
### Debugging ###
 
# Compare two folders but exclude specific path
$ diff --exclude=directory/to/exclude -r dir1 dir2
 
# Compare files in two directories and print only file paths that differ
diff -qr dir1 dir2
 
# Compare files in two directories and print only file path that is same
diff -rs dir1 dir2 | egrep '^Files .+ and .+ are identical$'

Comments