a cheat sheet for common (and some uncommon) command line tasks required when administering web applications, Drupal in particular. References include:
- Diff/Merge
- Find (on file system and in file)
- RegEx
- Text Editing
- Drupal/Drush/Composer
- General Admin
- Backup via rsync
- TROUBLESHOOTING
- Random Things
- git Tricks
- SVN
Reference: http://the.taoofmac.com/space/HOWTO/Merge%20Folders These commands preserve files/directories unique to destination target.
The standard UNIX way:
cp -R -v source/. destination
The geeky UNIX way (restartable):
rsync -vaEW source/ destination
create a list of files from both directories:
cd <path to local directory>
find . | sort > /tmp/filenames-local
cd <path to remote directory>
find . | sort > /tmp/filenames-remote
List files that only exist in the remote directory:
comm -1 -3 /tmp/filenames-local /tmp/filenames-remote
List files that only exist in the local directory:
comm -2 -3 /tmp/filenames-local /tmp/filenames-remote
reference: http://www.no-ack.org/2010/08/merge-directories-on-unix-like-systems.html
export IFS=$'\n'
Copy missing files from the remote to the local directory.
tar -C <path to remote directory> -c $(comm -1 -3 /tmp/filenames-local /tmp/filenames-remote) | tar -C <path to local directory> -x
Copy missing files from the local to the remote directory.
tar -C <path to local directory> -c $(comm -2 -3 /tmp/filenames-local /tmp/filenames-remote) | tar -C <path to remote directory> -x
References:
- http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples
- https://stackoverflow.com/questions/4529134/delete-files-with-string-found-in-file-linux-cli
- https://stackoverflow.com/a/11283391
find . -type f -size +50000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
or
du -ks . | sort -n -r | head -n 10
sudo find . -maxdepth 2 -type d -name '.svn'
reference: http://www.ahinc.com/linux101/textfiles.htm
grep -c -v .svn ~/svn-delete-it.brownsites.log
grep -nrol “given content” .
- Find files containing
email@example.com
.- Create
doit.sh
script that removes each of the found files.
- Create
- Review and edit the batch remove script for any outliers.
- Run the script.
find . | xargs grep -l email@example.com | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh
grep -L "given content" .
e.g.
grep -L "shib_authmap" ./*/settings.php
find . -type f -print0 | xargs --null grep -Z -L 'my string' | xargs --null rm
grep -rl <string-to-match> | xargs grep -L <string-not-to-match>
find . -name "*some-filename-pattern*" -print | xargs grep "some text pattern"
find . -iname '*php' | xargs grep 'string' -sl
(finds and lists php files containing the string “string”)
find . *.* -user $USERNAME -print
reference: http://www.cyberciti.biz/faq/how-do-i-find-all-the-files-owned-by-a-particular-user-or-group/
find directory-location -group {group-name} -name {file-name}
e.g.
find . -group webserv -name *.php
reference: http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/
sed -i 's/old-word/new-word/g' *.txt
Find files in specific files, exclude certain paths, and show first occurrence of specific string in content
sudo find . \( ! -path '*/sites/*' ! -path '*/profiles/*' ! -path '*/themes/*' ! -path '*/modules/*' \) -iname 'CHANGELOG.txt' | xargs grep -m 1 'Drupal' -s1
reference: http://stackoverflow.com/questions/848293/shell-script-get-all-files-modified-after-date
find ./*/files/. -mtime -10
(X = 10 days ago)
Search for tags and subsearch for tag name (e.g. “a” of or “b” of , etc)
Enter:
%s/old-string/new-string/
e.g. User needs to go through a settings file and replace the path to physical directory “drupal_core/drupal6/drupal-6.22” with the simpler path to a symlink “drupal-cms”:
%s/drupal_core\/drupal6\/drupal-6.22/drupal-cms
N.B. “\” used to escape “/” characters in search and/or replace strings.
set number
- Visual Studio Code (VS Code) Debugging Setup (w/DDEV)
- Debug PHP/Drupal with DDEV, XDebug, and VSCode
- Setting up XDebug + VSCode for use w/Drush
- XDebug Step Debugging Guide
- XDebug Over Command Line w/DDev
- Setup XDebug on DDEV
- Run
ddev xdebug
.
- Run
- Setup Browser's XDebug Listener
- Setup XDebug in VSCode
- Setup XDebug on DDEV
- Run
ddev xdebug
.
- Run
drush sql-cli < /path/to/exported-db-file.sql
More about drush sql-cli usage.
drush php-eval 'node_access_rebuild()';
drush php-eval 'drupal_rebuild_theme_registry()';
drush php-eval 'menu_rebuild()';
References:
composer require vendor/package_name:version_id --with-all-dependencies
composer outdated "drupal/*"
composer update "drupal/core-*" --with-all-dependencies
composer require --dev drupal/devel
composer install --no-dev
printenv #shows all environment variables
echo $variable_name #shows the value for the given variable
e.g. for PATH:
echo $PATH
shows all information about the distribution currently installed
lsb_release -a
gets amount of space used on partition where ‘sites’ folder located
df sites
human readable folder sizes and total folder size for ‘sites’ folder
du -ch sites
human readable total size for ‘sites’ folder
du -ch sites | grep total
reference: http://www.techrepublic.com/article/master-the-linux-bash-command-line-with-these-10-shortcuts/5827311 Sometimes you want to see how you might have done something before on the command line.
Ctrl+r
then type in the keyword or bash command to see previous uses of the command
Show current date as UNIX timestamp:
date -u +%s
What ports are in use or assigned? reference: http://www.cyberciti.biz/tips/linux-display-open-ports-owner.html
sudo lsof -i
sudo netstat -lptu
sudo netstat -tulpn
sync directory from prod source to QA destination (assuming logged into QA server)
rsync -r -a -v -e ssh username@server.example.com:/var/www/cms/sites/example.com.base /var/www/cms/sites/example.com.base
example: "-bash: /bin/rm: Argument list too long"
Occurs when list of arguments for given command exceed system limits. The example above occurred when rm * was run on a directory with an excessive (100s) number of files on a restrictive system.
workaround:
find . -name ‘*.jpg’ | xargs rm
reference: http://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/
date | md5sum
reference: http://www.zoharbabin.com/git-pull-inside-multiple-folders
find basefolder -name .git -execdir git pull origin master \;
this finds the subdirectories under current directory with git repositories (contains .git folder), echoes the path and pulls updates (from origin to local master)
find . -name ".git" -exec echo {} \; -execdir git pull \;
this checks out revision 1234 of somepath to ./working-directory
svn checkout svn://somepath@1234 working-directory
find . -type d -name '.svn' -print0 | xargs -0 rm -rdf
reference: http://www.thingy-ma-jig.co.uk/blog/11-03-2009/lazy-linux-piping when running svn ci, kept getting error message: " svn: Commit failed (details follow): svn: Directory '/www/data/httpd/htdocs/cms/sites/all/modules/xxxxx' is missing used the following to re-add missing directories in bulk"
svn st all/modules | grep ^! | gawk '{ print $2 }' | xargs svn up
references:
- http://subversion.apache.org/faq.html#in-place-import
- http://stackoverflow.com/questions/678437/svn-in-place-import-and-checkout
- http://news.e-scribe.com/350
svn mkdir file:///root/svn-repository/etc \
-m "Make a directory in the repository to correspond to /etc"
cd /etc
svn checkout file:///root/svn-repository/etc .
svn add apache samba alsa X11
svn commit -m "Initial version of my config files"
reference: http://sdesmedt.wordpress.com/2006/12/10/how-to-make-subversion-ignore-files-and-folders/ inside target directory:
svn propset svn:ignore -F ignore.txt .
svn delete --keep-local the_file
In response to: "Can't remove directory 'example.com/mysite/themes/.svn/tmp/props': Permission denied"
entering the following command:
find example.com.mysite* -type d -exec sudo chown -R myuser:mygroup .svn '{}' \;; svn cleanup .
find -maxdepth 1 -type d -newer example.com.mysite -exec svn info {} \;