List Files That Are in Directory1 But Not in Directory2 and Vice Versa

List files that are in directory1 but NOT in directory2 and vice versa?

a bit crude - but the easiest way I always use is (can play with the diff params, I typically use different grep

diff -rcw DIR1 DIR2| grep ^Only

then you can sort and format as you like

Revised to format (less efficient as we are running diff twice here ... easily solved)

echo files only in $dir1
LST=$(diff ${dir1} ${dir2}| grep "^Only in ${dir1}"| sed 's@^.*: @@')
(cd ${dir1}; ls -l ${LST})

echo files only in $dir2
LST=$(diff ${dir1} ${dir2}| grep "^Only in ${dir2}"| sed 's@^.*: @@')
(cd ${dir2}; ls -l ${LST})

Expanding on the sed expression above:

s=search and replace

the three '@' are separating the expressions (this is TRADITIONALLY done with '/')

^ matches the beginning of a line (forces the rest not to match elsewhere)
. means any character

* means the previous expression (.==match any char) 0-N times
": " is what I matched on from the diff output "Only in X: "

Look Mommy, no hands - now without 'sed' its beginning to be less and less crude

XIFS="${IFS}"
IFS=$'\n\r'
for DIFFLINE in $(diff ${dir1} ${dir2}|grep ^Only); do
case "${DIFFLINE}" in
"Only in ${dir1}"*)
LST1="${LST1} ${DIFFLINE#*:}"
;;
"Only in ${dir2}"*)
LST2+="${DIFFLINE#*:}"
;;
esac
done
IFS="${XIFS}"

echo files only in $dir1
(cd ${dir1}; ls -l ${LST1})

echo files only in $dir2
(cd ${dir2}; ls -l ${LST2})

You will probably want to know about IFS ... it needs some reading in the bash manual, but its basically the field separator characters ... by default they include spaces and I don't want the loop to be fed with fractions of lines, just complete lines - so for the duration of the loop I override the default IFS to just newlines and carriage returns.

BTW maybe your professor is reading stackoverflow, maybe next you wont be allowed to use semicolons ;-) ... (back to 'man bash' ... BTW if you do 'man bash' do it in emacs, makes much easier to read IMO)

Unix Get only Difference between 2 files

I believe you cannot do that using diff.

However, you can by alternative tools in Linux.

For example, vimdiff which gives you seperated page that you prefer and there are other tools like kompare and diffMerge. Please refer to this link. HERE

Read lines from a file into a Bash array

Latest revision based on comment from BinaryZebra's comment
and tested here. The addition of command eval allows for the expression to be kept in the present execution environment while the expressions before are only held for the duration of the eval.

Use $IFS that has no spaces\tabs, just newlines/CR

$ IFS=$'\r\n' GLOBIGNORE='*' command eval  'XYZ=($(cat /etc/passwd))'
$ echo "${XYZ[5]}"
sync:x:5:0:sync:/sbin:/bin/sync

Also note that you may be setting the array just fine but reading it wrong - be sure to use both double-quotes "" and braces {} as in the example above


Edit:

Please note the many warnings about my answer in comments about possible glob expansion, specifically gniourf-gniourf's comments about my prior attempts to work around

With all those warnings in mind I'm still leaving this answer here (yes, bash 4 has been out for many years but I recall that some macs only 2/3 years old have pre-4 as default shell)

Other notes:

Can also follow drizzt's suggestion below and replace a forked subshell+cat with

$(</etc/passwd)

The other option I sometimes use is just set IFS into XIFS, then restore after. See also Sorpigal's answer which does not need to bother with this

Suppress mkdir warning while still executing the OR option?

You should replace:

mkdir MYLOCK

by:

mkdir MYLOCK 2>/dev/null

C# Sort a list from the values of another list

I agree with the comments above, use a single list of full paths and use the System.Io.Path.FileName method to order by file name regardless of directory.

        var list2 = new List<string>() { @"C:\Directory1\B.txt", @"C:\Directory2\A.txt" };
var orderedList = list2.OrderBy(System.IO.Path.GetFileName);
//orderedList[0] is @"C:\Directory2\A.txt"
//orderedList[1] is @"C:\Directory1\B.txt"


Related Topics



Leave a reply



Submit