Difference between revisions of "Comparing files"
(Initial version of comparing files script and command)
Revision as of 20:45, 18 July 2014
The script below takes two arguments on the command-line and compares them via
diff command. It also echoes generic human-readable answer saying whether two file system objects are equal. As
diff compares directories as well as files, paths to directories are valid arguments as well.
#!/bin/bash if [ $# != 2 ] ; then echo 'Expected exactly two arguments' exit 1 fi if [ -f "$1" -a -f "$2" ]; then : # OK, do nothing here elif [ -d "$1" -a -d "$2" ]; then : # OK, do nothing here else if [ -f "$1" ]; then type_of_1='file' else type_of_1='directory' fi if [ -f "$2" ]; then type_of_2='file' else type_of_2='directory' fi echo "Arguments are of different type ($type_of_1/$type_of_2)" exit 1 fi if diff "$1" "$2" > /dev/null 2>&1; then echo 'Equal' else echo 'Not equal' fi
Plugging the script into vifm
To use it in vifm:
- Save the script as
compareto avoid name conflict with existing tools) at one of directories listed in your
$PATHenvironment variable or under
$VIFM/scripts(most likely that it's
- On Unix-like operating systems make the script file executable by running
chmod +x compare-cmp
- Add following lines to your
command! cmpinternal compare-cmd %a %S command! cmp : if expand('%c') == expand('%f') \ | echo expand('Comparing %"c and %"C:t ...') \ | cmpinternal %c %C \ | else \ | echo expand('Comparing files: %"f ...') \ | cmpinternal %f \ | endif
Explanation of key parts:
cmpinternal- a trick to invoke external command. Calling
execute "!compare-cmd ... %S"reparses command-line which leads to loosing results of command execution (they are overwritten by execution of
expand('%c') == expand('%f')- check whether only one file is selected in current pane.
:cmpwithout selection or with single file selection to compare files under cursors in two panes.
:cmpafter selecting two files in the current pane to compare them.
Results are printed on the status bar (because of
%S macro in