Bash oneliners

Estimated reading time of this article: 2 minutes

What useful bash commands I've used?

Copy files of git commits to a folder (keeping the directory structure)

Instead of creating patches and applying them, an alternative is to copy the complete files over the existing files. The following command gets all the modified files in commits and copies them with the full path into a given directory.

cp --parents $(git diff --name-only start_commit..end_commit) path/for/extracted/files

Example

cp --parents $(git diff --name-only dcf8c646..HEAD) ../drupal/core/php72_fixed_files

Compare directories recursively with file contents

Compare recursively two directory with file contents ignoring some files like .bak.

diff -u -r -w -x"*.bak" dirA/ dirB/ | less
Argument Description
-u Unified diff
-r Recursive
-w Ignore whitespace
-x"*.bak" Files to ignore

Ignore certain lines in git diff

Sometimes some lines disturb in a git diff such as AUTO_INCREMENT values. The following command filters these lines from diff while comparing dump.sql from git HEAD and dump.sql in the workspace.

diff -u <(git show HEAD:dump.sql | perl -p -e's/AUTO_INCREMENT=\d+//ig') <(cat dump.sql | perl -p -e's/AUTO_INCREMENT=\d+//ig') | less

More MySQL dump filtering:

diff -u <(git show HEAD:dump.sql | perl -p -e's/DEFINER=.*? SQL SECURITY DEFINER//ig' | perl -p -e's/DEFINER=`.*?`@`localhost` //ig' | perl -p -e's/ALGORITHM=UNDEFINED//ig' | perl -p -e's/AUTO_INCREMENT=\d+//ig') <(cat dump.sql | perl -p -e's/AUTO_INCREMENT=\d+//ig') | less

Instead of less a visual diff tool such as kompare can be used, replace with kompare -.